<template>
	<view :class="isUps == true ? 'prevent' : ''">
		<cu-custom bgColor="bg-gradual-blueness" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">开始巡检</block>
		</cu-custom>
		<scroll-view scroll-y="false" >
		<view class="form">
			<view class="cu-form-group">
				<view class="title">巡检人</view>
				<input name="input" v-model="pd.INSPECTOR" placeholder="请输入巡检人"></input>
			</view>
			<view class="cu-form-group bb">
				<view class="title">巡检时间</view>
				<picker mode="date" :value="pd.INSPECTIONTIME" @change="changeDate">
					<view class="picker">
						{{pd.INSPECTIONTIME?pd.INSPECTIONTIME:'请选择'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-textarea margin-top">
				<view class="cu-form-title">巡检内容</view>
				<textarea maxlength="-1" v-model="pd.INSPECTIONCONTENT" placeholder="(必填项)"></textarea>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">是否有故障</view>
				<radio-group class="selected">
					<view class="group mr20">
						<radio class='radio' value="是" :checked="pd.ISFAULT=='是'" @click="radioType('是')"></radio>
						<text>是</text>
					</view>
					<view class="group">
						<radio class='radio' value="否" :checked="pd.ISFAULT=='否'" @click="radioType('否')"></radio>
						<text>否</text>
					</view>
				</radio-group>
			</view>

			<view class="cu-bar bg-white  margin-top" v-if="pd.ISFAULT=='是'">
				<view class="action">
					故障照片
				</view>
				<view class="action">
					{{imgList.length}}/4
				</view>
			</view>
			<view class="cu-form-group" v-if="pd.ISFAULT=='是'">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage(imgList[index].filePath)"  data-type="0" :data-url="imgList[index].filePath">
						<image  :src="imgList[index].filePath" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg"  data-type="0" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage(0)" v-if="imgList.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>

			<view class="cu-bar bg-white margin-top" v-if="pd.ISFAULT=='是'">
				<view class="action">
					故障处理后照片
				</view>
				<view class="action">
					{{imgList1.length}}/4
				</view>
			</view>
			<view class="cu-form-group" v-if="pd.ISFAULT=='是'">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList1" :key="index" @tap="ViewImage(imgList1[index].filePath)" data-type="1" :data-url="imgList1[index].filePath">
						<image  :src="imgList1[index].filePath" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg"  data-type="1" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage(1)" v-if="imgList1.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>

			<view class="cu-bar bg-white margin-top" >
				<view class="action">
					巡检图片
				</view>
				<view class="action">
					{{imgList2.length}}/4
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList2" :key="index" @tap="ViewImage(imgList2[index].filePath)" data-type="1" :data-url="imgList2[index].filePath">
						<image  :src="imgList2[index].filePath" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg"  data-type="2" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage(2)" v-if="imgList2.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
		</view>
		<view class="padding flex flex-direction">
			<button class="cu-btn bg-blue margin-tb-sm lg" @click="$noMultipleClicks(goSubmit)">提交</button>
		</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		basePath,corpinfoId,deptId,loginUser,formatDate,loginSession,baseImgPath
	} from '@/common/tool.js';
	import tkiTree from "@/components/select-tree/select-tree.vue"
	import gcoord from '@/common/gcoord.js'
	export default {
		components: {
			tkiTree
		},
		data() {
			return {
				isUps:false,
				msg:'add',
				riskId:'',
				itemId:'',
				index:'',
				noClick:true,
				pd:[],
				imgList: [],
				imgList1: [],
				imgList2: [],
			}
		},
		onLoad(event){
			this.pd.SPECIALEQUIPMENT_ID = event.SPECIALEQUIPMENT_ID;
			this.pd.CORPINFO_ID = corpinfoId;
			this.pd.CREATOR = loginUser.NAME;
			loginSession();
		},
		methods: {
			changeDate(e) {
				this.pd.INSPECTIONTIME = e.detail.value
				this.$forceUpdate();//强制刷新
			},
			radioType(e){
				this.pd.ISFAULT = e
				this.$forceUpdate();//强制刷新
			},
			//图片上传
			ChooseImage(e) {
				var ss=4
				if(e==0) {
					ss=4-this.imgList.length;
				}else if(e==1){
					ss=4-this.imgList1.length;
				}else if(e==2){
					ss=4-this.imgList2.length;
				}
				var ss=4-this.imgList.length;
				uni.chooseImage({
					count: ss, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
					sourceType: ['camera','album'], //从相册选择
					success: (res) => {
            if(e==0) {
              for (let i = 0; i < res.tempFilePaths.length; i++) {
                let img={};
                img.id='';
                img.filePath=res.tempFilePaths[i];
                this.imgList.push(img)
              }
            }else if(e==1){
              for (let i = 0; i < res.tempFilePaths.length; i++) {
                let img = {};
                img.id = '';
                img.filePath = res.tempFilePaths[i];
                this.imgList1.push(img)
              }
            }else if(e==2){
              for (let i = 0; i < res.tempFilePaths.length; i++) {
                let img = {};
                img.id = '';
                img.filePath = res.tempFilePaths[i];
                this.imgList2.push(img)
              }
            }
					}
				});
			},
			ViewImage(url) {
				let files = [];
				files.push(url)
				uni.previewImage({
					urls: files,
					current: 0
				});
			},
			DelImg(e) {
				var _this = this;
				let i=e.currentTarget.dataset.index
				uni.showModal({
					title: '操作提示',
					content: '确定要删除这张图片吗?',
					cancelColor:"#000000",
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							if(e.currentTarget.dataset.type==0) {
								this.imgList.splice(e.currentTarget.dataset.index, 1)
							}else if(e.currentTarget.dataset.type==1){
								this.imgList1.splice(e.currentTarget.dataset.index, 1)
							}else if(e.currentTarget.dataset.type==2){
								this.imgList2.splice(e.currentTarget.dataset.index, 1)
							}
						}
					}
				})
			},
			goSubmit() {
				var _this = this;
				if (!_this.pd.INSPECTOR) {
					uni.showToast({
						icon: 'none',
						title: '请填巡检人',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.INSPECTIONTIME) {
					uni.showToast({
						icon: 'none',
						title: '请填巡检时间',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.INSPECTIONCONTENT) {
					uni.showToast({
						icon: 'none',
						title: '请选巡检内容',
						duration: 1500
					});
					return;
				}
				if (!_this.pd.ISFAULT) {
					uni.showToast({
						icon: 'none',
						title: '请选择是否有故障',
						duration: 1500
					});
					return;
				}
				if (_this.pd.ISFAULT == '是' && _this.imgList.length <= 0) {
					uni.showToast({
						icon: 'none',
						title: '请上传故障照片',
						duration: 1500
					});
					return;
				}
				if (_this.pd.ISFAULT == '是' && _this.imgList1.length <= 0) {
					uni.showToast({
						icon: 'none',
						title: '请上传故障处理后照片',
						duration: 1500
					});
					return;
				}
				if (_this.imgList2.length <= 0) {
					uni.showToast({
						icon: 'none',
						title: '请上传巡检图片',
						duration: 1500
					});
					return;
				}
				uni.showLoading({
					title:"提交中..."
				});//加载中动画
				var fileList = [];
				var typeList = [];

				const formData = {}
				Object.keys(this.pd).map(key => {
					formData[key]=this.pd[key]
				})

				if (this.pd.ISFAULT === '是') {
					this.uploadImgFaults(fileList,typeList)
					this.uploadImgHandles(fileList,typeList)
				}
				this.uploadImgInspections(fileList,typeList)

				formData.TYPES = typeList.join(",")
				formData.CORPINFO_ID = loginUser.CORPINFO_ID
				formData.USER_ID = loginUser.USER_ID
				uni.uploadFile({
					url: basePath + '/app/specialequipmentaqxj/add',
					files: fileList,
					formData:formData,
					success: (res) => {
						var data = JSON.parse(res.data)
						if ("success" == data.result) {
							uni.showToast({
								icon: 'none',
								title: '提交成功',
								duration: 1500
							});
							setTimeout(function () {
								var pages = getCurrentPages(); // 获取当前页面栈
								var prePage = pages[pages.length - 2]; // 上一个页面
								prePage.$vm.initflag = true; // A 页面 init方法 为true
								uni.navigateBack({});
								uni.hideLoading();
							}, 1500);
						}else{
							uni.showToast({
								title: '错误',
								duration: 2000
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '错误',
							duration: 2000
						});
					}
				})
			},
			uploadImgFaults(fileList,typeList) {
				this.imgList.map(item => {
					var img = {}
					img.name = 'file'+(fileList.length)
					img.uri = item.filePath
					fileList.push(img)
					typeList.push(24)
				})
			},
			uploadImgHandles(fileList,typeList) {
				this.imgList1.map(item => {
					var img = {}
					img.name = 'file'+(fileList.length)
					img.uri = item.filePath
					fileList.push(img)
					typeList.push(25)
				})
			},
			uploadImgInspections(fileList,typeList) {
				this.imgList2.map(item => {
					var img = {}
					img.name = 'file'+(fileList.length)
					img.uri = item.filePath
					fileList.push(img)
					typeList.push(26)
				})
			},

		}
	}
</script>

<style>
	.prevent {
			width: 100%;
			height: 100%;
			position: fixed;
			top: 0;
			left: 0;
			overflow: hidden;
		}
	.cu-form-title{
		padding: 20upx 0;
	}
	.cu-form-textarea{
		background-color: #ffffff;
		padding: 1upx 30upx 20upx;
		min-height: 100upx;
	}
	.cu-form-textarea textarea {
		height: 4.6em;
		width: 100%;
		line-height: 1.2em;
		flex: 1;
		font-size: 28upx;
		padding: 0;
	}
	.selected{
		display: flex;
		align-items: center;
		height: 100upx;
	}
	.selected .radio{
		 transform:scale(0.8);
		 margin-right: 10upx;
	}
	.group{
		display: flex;
		align-items: center;
	}
	.cu-form-group .title{
		font-size: 28upx;
		font-weight: bold;
	}
	.cu-bar .action:first-child {
	    font-size: 28upx;
	}
	.cu-form-group .picker{
		color: #808080;
	}
	.picker-tree{
		color: #808080;
	}
</style>