<!--
 * @Title: 
 * @Description: 维修任务填报提交页
 * @Author: swp
 * @Date: 2022-08-24 10:49:21
 * @LastEditors: 
 * @LastEditTime: 2022-08-24 10:49:21
-->
<template>
	<view class="container">
		<uni-nav-bar dark :fixed="true" backgroundColor="#3F9EFF" statusBar="false" left-icon="left" left-text="返回"
			@clickLeft="toBack">
			<view style="width: 100%;display: flex;flex-direction: row;justify-content: center;align-items: center;">
				<text style="color: white;font-size: 1rem;">{{title}}</text>
			</view>
		</uni-nav-bar>
		<uni-forms ref="baseForm" :model="formData">
			<uni-section title="故障现象描述" ftitleFontSize="0.8rem" type="circle">
				<view style="padding-top: 10px;padding-bottom: 0px;padding-left: 15px;padding-right: 15px;">
					<uni-forms-item name='networkPayer' required>
						<uni-easyinput :styles="styles" type="textarea" v-model="formData.failureCheckDesc"
							placeholder="录入故障现象描述" />
					</uni-forms-item>
				</view>
			</uni-section>
			<uni-section title="解决措施说明" ftitleFontSize="0.8rem" type="circle">
				<view style="padding-top: 10px;padding-bottom: 10px;padding-left: 15px;padding-right: 15px;">
					<uni-forms-item required>
						<uni-easyinput type="textarea" v-model="formData.processDesc" placeholder="请录入解决方案说明" />
					</uni-forms-item>
					<!-- 					<uni-card title="拍摄照片" extra="点击下方按钮开始拍摄" :is-shadow="false">
						<view class="view-flex-rs-flex-wrap">
							<view v-for="(item, index) in failureProcessPhotos" :key="index" class="view-flex-cc"
								style="width: 80px;">
								<view class="img-container">
									<view class="note-image-box">
										<view class="note-image-item">
											<view class="close-icon" @click="onPhotoDel(index)">
												<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
											</view>
											<view class="image-box">
												<image :src="toOss(item)" mode="widthFix">
												</image>
											</view>
										</view>
									</view>
								</view>
							</view>
							<view class="view-flex-cc" style="width: 80px;">
								<view class="img-container">
									<view class="note-image-box">
										<view class="note-image-item" @click="onCheckPhotoAdd()">
											<view class="image-box">
												<uni-icons type="plusempty" size="50" color="#eee"></uni-icons>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</uni-card> -->
				</view>
			</uni-section>


			<uni-section title="拍摄照片" ftitleFontSize="0.8rem" type="circle">
				<template v-slot:right>
					<view style="color:grey">
						点击下方按钮开始拍摄
					</view>
				</template>
				<view style="padding-top: 10px;padding-bottom: 10px;padding-left: 15px;padding-right: 15px;">
					<!-- 				<uni-forms-item required>
						<uni-easyinput type="textarea" v-model="formData.processDesc" placeholder="请录入解决方案说明" />
					</uni-forms-item> -->
					<!-- <uni-card title="拍摄照片" extra="点击下方按钮开始拍摄" :is-shadow="false"> -->
					<view class="view-flex-rs-flex-wrap">
						<view v-for="(item, index) in failureProcessPhotos" :key="index" class="view-flex-cc"
							style="width: 80px;">
							<view class="img-container">
								<view class="note-image-box">
									<view class="note-image-item">
										<view class="close-icon" @click="onPhotoDel(index)">
											<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
										</view>
										<view class="image-box">
											<image :src="toOss(item)" mode="widthFix">
											</image>
										</view>
									</view>
								</view>
							</view>
						</view>
						<view class="view-flex-cc" style="width: 80px;">
							<view class="img-container">
								<view class="note-image-box">
									<view class="note-image-item" @click="onCheckPhotoAdd()">
										<view class="image-box">
											<uni-icons type="plusempty" size="50" color="#eee"></uni-icons>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<!-- 	</uni-card> -->
				</view>
			</uni-section>
			<!-- 			<uni-section v-if="orderType ==1" title="费用申报" ftitleFontSize="0.8rem" type="circle">

				<view style="padding-top: 10px;padding-bottom: 10px;padding-left: 15px;padding-right: 15px;">
					<uni-forms-item label="是否产生费用" label-width="130px" required>
						<uni-data-checkbox v-model="formData.costsReport"
							:localdata="costsReportItems.items"></uni-data-checkbox>
					</uni-forms-item>
					<uni-forms-item v-if="formData.costsReport==2">
						<uni-easyinput type="textarea" v-model="formData.costsReportDesc" placeholder="请录入费用说明" />
					</uni-forms-item>
				</view>
			</uni-section> -->
		</uni-forms>
		<view class="footer">
			<view class="control">
				<view class="view-flex-rc">
					<view class="block"
						style="width: 160px;height:32px;margin-left: 10px;margin-right: 10px;border-radius:7px;background-color:lightsalmon"
						@click="toSubmit()">
						<view class="view-flex-cc">
							<uni-icons class="input-uni-icon" type="checkmarkempty" size="18" color="#999" />
						</view>
						<view class="view-flex-cc" style="padding-left:5px;">
							<text style="color:black;font-size:0.7rem">立即提交</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import http from '@/http/api.js';
	import {
		pathToBase64,
		base64ToPath
	} from '@/js_sdk/mmmm-image-tools/index.js';
	import {
		oss,
		devUrl,
		prodUrl
	} from '@/common/setting';
	export default {
		components: {},
		data() {
			return {
				styles: {
					color: '#333',
					borderColor: '#e5e5e5',
					disableColor: '#FFFFFF'
				},
				rules: {
					failureCheckDesc: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}]
					},
					failureSolutionDesc: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}, ]
					},
				},
				skipPage: 1,
				costsApprove: 0,
				orderId: 0,
				orderType: 1,
				formData: {
					failureCheckDesc: '',
					processDesc: '',
					costsReport: 1,
					costsReportDesc: '',
				},
				failureProcessPhotos: [],
				pattern: {
					color: '#7A7E83',
					backgroundColor: '#fff',
					selectedColor: '#007AFF',
					buttonColor: '#007AFF',
					iconColor: '#fff'
				},
				baseURL: '',
				baseOSS: '',
				costsReportItems: {
					items: [{
						'value': 1,
						'text': '否'
					}, {
						'value': 2,
						'text': '是'
					}],
					current: 1,
				},
				title: '工单处理',
				desc: '',
				failureCheckTitle: '',
				failureCheckPlaceholder: '',
			}
		},
		computed: {},
		onLoad(options) {
			this.skipPage = options.skipPage;
			//this.costsApprove = options.costsApprove;
			//this.orderProcessId = options.orderProcessId;
			this.orderId = options.orderId;
			// this.orderType = options.orderType;
			// console.log("order type "+this.orderType);
			// if (this.orderType == 1) {
			//this.failureCheckTitle = '故障问题描述';
			//this.failureCheckPlaceholder = '录入故障现象描述';
			// } else if (this.orderType == 2) {
			// 	this.failureCheckTitle = '服务内容描述';
			// 	this.failureCheckPlaceholder = '请录入服务内容';
			// }
			this.baseURL = process.env.NODE_ENV === 'development' ? devUrl : prodUrl;
			this.baseOSS = oss;
			//if (this.costsApprove == '2') {
			this.getDetail();
			//}
		},
		onReady() {
			console.log("onReady++++++++++++++")
			// 需要在onReady中设置规则
			this.$refs.baseForm.setRules(this.rules)
		},
		onShow() {},
		methods: {
			toBack() {
				uni.navigateBack({
					delta: Number(this.skipPage)
				})
			},
			toOss(path) {
				let url = this.baseOSS + path;
				//console.log("tooss " + url)
				return url;
			},
			getDetail() {
				let that = this;
				let postData = {};
				postData['id'] = this.orderId;
				http.request({
					url: '/galaxy-business/order/detail',
					method: 'GET',
					data: postData
				}).then(res => {
					console.log(res)
					if (res.data != null) {
						//that.orderInfo = res.data;
						that.formData.failureCheckDesc = res.data.orderDesc;
					}
				}).catch(err => {
					console.log(err)
				})

				// let that = this;
				// let postData = {};
				// postData['orderId'] = this.orderId;
				// http.request({
				// 	url: '/galaxy-business/order/process/last/report',
				// 	method: 'GET',
				// 	data: postData
				// }).then(res => {
				// 	if (res.data != null) {
				// 		console.log(JSON.stringify(res.data))
				// 		that.formData.processDesc = res.data.processDesc;
				// 		that.formData.failureCheckDesc = res.data.processDesc;
				// 		if (res.data.failureProcessPhotoList != null) {
				// 			that.failureProcessPhotos = res.data.failureProcessPhotoList;
				// 		}
				// 	}
				// }).catch(err => {
				// 	console.log(err)
				// })
			},
			onCheckPhotoAdd() {
				let that = this;
				uni.chooseImage({
					sourceType: ['album ', 'camera'],
					sizeType: ['compressed'],
					success: (res) => {
						const len = res.tempFilePaths.length;
						if (len === 1) {
							res.tempFilePaths.forEach(path => {
								that.uploadPhoto(path);
							})
						} else {
							uni.showModal({
								content: '只能选择一张图片，请确认！',
								showCancel: false
							});
						}
					}
				})
			},
			uploadPhoto(imagePath) {
				let that = this;
				uni.showLoading({
					title: '上传中'
				});
				uni.uploadFile({
					url: this.baseURL + '/galaxy-resource/oss/endpoint/put-file-attach?Blade-Auth=' +
						uni.getStorageSync('accessToken'),
					fileType: 'image',
					filePath: imagePath,
					name: 'file',
					success: (uploadFileRes) => {
						if (uploadFileRes.statusCode == 200) {
							let data = JSON.parse(uploadFileRes.data);
							if (data.success) {
								let path = data.data['name'];
								that.failureProcessPhotos.push(path);
							}
						}
					},
					fail: (err) => {
						console.log(err);
						reject('err')
					},
					complete() {
						uni.hideLoading()
					}
				});
			},
			onPhotoDel(index) {
				this.failureProcessPhotos.splice(index, 1);
			},
			toSubmit() {
				console.log('表单提交');
				let that = this;
				this.$refs.baseForm.validate().then(res => {
					that.submitData();
				}).catch(err => {
					console.log('表单错误信息：', err);
					uni.showModal({
						content: "内容填报错误，请根据提示信息检查录入内容!",
						showCancel: false,
						success(res) {
							if (res.confirm) {
								//that.$refs.baseForm.clearValidate();
							}
						}
					});
				})
			},
			submitData() {
				let formdata = this.formData;
				formdata['orderId'] = this.orderId;
				if (this.failureProcessPhotos.length > 0) {
					let photos = '';
					for (let i = 0; i < this.failureProcessPhotos.length; i++) {
						if (photos.length > 0) {
							photos = photos + ',';
						}
						photos = photos + this.failureProcessPhotos[i];
					}
					formdata['failureProcessPhotos'] = photos;
				}
				let that = this;
				http.request({
					url: '/galaxy-business/order/process/xjgd/save',
					method: 'POST',
					data: formdata
				}).then(res => {
					if (res.success) {
						uni.showModal({
							content: '已成功提交',
							showCancel: false,
							success(res) {
								if (res.confirm) {
									that.toBack();
								}
							}
						});
					} else {
						uni.showModal({
							content: '' + res.msg,
							showCancel: false
						});
					}
				}).catch(err => {
					console.log(err)
				})
			},
			formReset: function(e) {
				console.log('清空数据')
			},
		}
	}
</script>

<style>
	/* page {
		background-color: rgb(240, 242, 244);
	} */
</style>

<style lang="scss" scoped>
	.cell-hover-class {
		background-color: rgb(235, 237, 238);
	}

	.view-flex-rs {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
	}

	.view-flex-rc {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.view-flex-re {
		display: flex;
		flex-direction: row;
		justify-content: flex-end;
		align-items: center;
	}

	.view-flex-cs {
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
	}

	.view-flex-cc {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.view-flex-ce {
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
	}

	.line-body {
		padding-left: 10px;
		padding-right: 10px;
	}

	.required-star {
		color: #FF0000;
		font-size: 15px;
		width: 20px;
		text-align: center;
		padding-left: 0px;
		padding-right: 2px;
	}

	.item-title {
		color: #909399;
		font-size: 15px;
	}

	.mline-text {
		padding-left: 2px;
		width: 100%;
		/* background-color:#909399; */
		height: 70px;
		border: 1px solid #FF5A5F;
		border-radius: 5px;
	}

	.footer {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 1;
		height: 100rpx;
		padding: 20rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		background-color: whitesmoke;

		.ipt {
			width: 380rpx;
			height: 77rpx;
			background: #f7f7f7;
			border-radius: 38px;
			padding: 0 37rpx;
			box-sizing: border-box;
			margin-right: 20rpx;
		}

		.control {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: flex-end;

			.block {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
			}

			.icon {
				height: auto;
			}

			.c {
				width: 41rpx;
				margin-right: 10rpx;
			}

			.s {
				width: 36rpx;
			}

			.t {
				width: 31rpx;
			}
		}
	}

	.container {
		padding: 0 0 100rpx;
	}

	.img-container {
		margin-bottom: 0px;
		width: 80px;
		height: 80px;

		.note-image-box {
			margin-top: 5px;
			display: flex;
			flex-wrap: wrap;
			padding: 0px;

			.note-image-item {
				position: relative;
				width: 100%;
				height: 0;
				padding-top: 100%;
				box-sizing: border-box;
				// background-color: #18B566;

				.close-icon {
					display: flex;
					justify-content: center;
					align-items: center;
					position: absolute;
					right: 0px;
					top: 0px;
					width: 22px;
					height: 22px;
					border-radius: 50%;
					background-color: #d5d5d5;
					z-index: 2;
				}

				.image-box {
					display: flex;
					justify-content: center;

					align-items: center;
					position: absolute;
					top: 0px;
					right: 0px;
					border: 0px;
					left: 0px;
					border: 1px #eee solid;
					border-radius: 5px;
					overflow: hidden;
					width: 98%;
					height: 98%;

				}


			}
		}
	}

	.input-body {
		background-color: #fff;
		padding: 10px;
	}

	.mb-10 {
		margin-top: 0px;
		margin-bottom: 0px;
	}

	.decoration {
		width: 6px;
		height: 6px;
		margin-right: 4px;
		border-radius: 50%;
		background-color: cadetblue;
	}
</style>