<template>
	<!-- 新增安全问题 -->
	<view class="addIssue">
		<headers :showBack="true">
			<view class="headerName">
				{{pageTitle}}
			</view>
		</headers>
		<view class="content">
			<view class="type flex">
				<view class="name"><text class="star">*</text>检查性质：</view>
				<picker @change="(e)=>bindPickerChange(e,1)" :value="natureIndex" class="picker" :range="natureList"
					range-key="name">
					<view class="uni-input uni-select cl" v-if="natureIndex==null">
						请选择 <image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
					<view class="uni-input uni-select" v-else>{{form.checkNature}}
						<image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
				</picker>
			</view>
			<view class="type flex">
				<view class="name"><text class="star">*</text>检查项：</view>
				<picker @click="$refs.locationTree._show()" :disabled="true" class="picker">
					<view class="uni-input uni-select cl" v-if="form.checkPoints==''">
						请选择 <image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
					<view class="uni-input uni-select flex2" v-else>
						<view style="width: 93%;" class="ellipsis">{{form.checkPoints}}</view>
						<image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
				</picker>
			</view>
			<view class="type flex3">
				<view class="name"><text class="star">*</text>检查内容：</view>
				<textarea class="textarea" maxlength="-1" placeholder-class="cl" name="inspectContent"
					@input='supplementTextarea' :value="form.checkContent" placeholder="请输入"></textarea>
			</view>
			<view class="type flex3">
				<view class="name">备注：</view>
				<textarea class="textarea" maxlength="-1" placeholder-class="cl" name="inspectContent"
					@input='claimTextarea' :value="form.remarks" placeholder="请输入"></textarea>
			</view>
			<view class="type flex">
				<view class="name"><text class="star">*</text>检查时间：</view>
				<view class="flex2" style="width: calc(70%);">
				<picker @change="(e)=>bindPickerChange(e,2)" mode="date" :value="form.checkTime" class="picker" style="width:48%">
					<view class="uni-input uni-select cl" v-if="form.checkTime===''">
						请选择 <image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
					<view class="uni-input uni-select" v-else>
							{{form.checkTime}}
						<!-- <image class="icon-down" src="/static/icon-down-black.png"></image> -->
					</view>
				</picker>
				<picker @change="(e)=>bindPickerChange(e,4)" mode="time" :value="time" class="picker" style="width:48%">
					<view class="uni-input uni-select cl" v-if="time===''">
						请选择 <image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
					<view class="uni-input uni-select" v-else>
						{{time}}
						<image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
				</picker>
				</view>
			</view>
			<view class="type flex3">
				<view class="name"><text class="star">*</text>附件：</view>
				<view class="uni-form-input imgBox_wrap">
					<view class="imgBox" v-show="imgFileList.length>0" v-for="(item,index) in imgFileList" :key="index">
						<image :src="url_config+'image/'+item.url" class="img"
							@click="previewImage(url_config+'image/'+item.url)"></image>
						<uni-icons @click="deleteImg(item,1)" class="deleteImg" type="clear" size="24"></uni-icons>
					</view>
					<view class="addImgBox" @click="uploadImg" v-if="imgFileList.length<5">
						<image src="/static/icon-add.png" class="icon-add"></image>
					</view>
				</view>
			</view>
			<view class="type flex">
				<view class="name">是否要整改：</view>
				<view class="flex">
					<text :class="form.isRectify==1?'status':''">否</text>
					<switch :checked="form.isRectify==0" @change="switchChange"
						style="margin: 0 10px;transform: scale(0.74,0.7)" />
					<text :class="form.isRectify==0?'status':''">是</text>
				</view>
			</view>
			<view class="type flex" v-if="form.isRectify==0">
				<view class="name"><text class="star">*</text>整改人：</view>
				<picker @change="(e)=>bindPickerChange(e,3)" :value="changePeopleIndex" class="picker"
					:range="personList" range-key="workerName">
					<view class="uni-input uni-select cl" v-if="changePeopleIndex==null">
						请选择 <image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
					<view class="uni-input uni-select" v-else>{{personList[changePeopleIndex].workerName}}
						<image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
				</picker>
			</view>
			<view class="type flex">
				<view class="name">通知人：</view>
				<picker @click="$refs.informTree._show()" :disabled="true" class="picker">
					<view class="uni-input uni-select cl" v-if="notifier===''">
						请选择 <image class="icon-down" src="/static/icon-down-black.png"></image>
					</view>
					<view class="uni-select flex2" v-else>
						<view style="line-height:20px; width:80%; padding:6px 0;">{{notifier}}</view>
						<image class="icon-down" style="width:13px; height:13px;" src="/static/icon-down-black.png">
						</image>
					</view>
				</picker>
			</view>
			<view class="type flex3"  v-if="form.isRectify==0">
				<view class="name"><text class="star">*</text>整改要求：</view>
				<textarea class="textarea" maxlength="-1" placeholder-class="cl" name="inspectContent"
					@input='rectifyRequireTextarea' :value="form.rectifyRequire" placeholder="请输入"></textarea>
			</view>
			<view class="addSaveBtn" @click="addSaveBtn">新增保存</view>
		</view>
		<!-- 检查项 -->
		<tki-tree ref="locationTree" :multiple="true" @confirm="(val)=>checkPointBtn(val,1)" idKey='id'
			:range="checkPointList" rangeKey="checkname" confirmColor="#4e8af7" />
		<!-- 通知人 -->
		<tki-tree ref="informTree" :multiple="true" @confirm="(val)=>checkPointBtn(val,3)" idKey='id'
			:range="personList" rangeKey="workerName" confirmColor="#4e8af7" />


	</view>
</template>

<script>
	import tkiTree from "@/components/tki-tree/tki-tree.vue"
	export default {
		components: {
			tkiTree
		},
		data() {
			return {
				pageTitle: '新增检查记录',
				projectSn: '',
				natureIndex: null,
				natureList: [{
						name: '周检',
						value: '周检'
					},
					{
						name: '月检',
						value: '月检'
					},
					{
						name: '其它',
						value: '其它'
					},
				],
				form: {
					checkContent: '',
					checkNature: '',
					checkPoints: '',
					fileUrl: '',
					isRectify: 0,
					notifier: '',
					rectifyPeople: '',
					rectifyRequire: '',
					remarks: '',
					checkTime: '',
				},
				time:'',
				checkPointList: [], //获取检查项数据
				imgFileList: [], //附件
				personList: [], //人员列表  -- 整改人、通知人
				changePeopleIndex: null, //整改人
				notifier: '', //通知人
			}
		},
		onLoad(option) {
			this.pageTitle = option.type == 'add' ? '新增检查记录' : '编辑检查记录';
			this.projectSn = JSON.parse(uni.getStorageSync('projectDetail')).projectSn;
			this.getCheckPointList();
			this.getPersonList();
		},


		methods: {



			//下拉选择回显
			bindPickerChange(e, type) {
				console.log(e)
				//type ==1检查性质 ; 2 检查时间--日期;3 整改人; 4检查时间--时间
				if (type == 1) {
					this.natureIndex = e.detail.value;
					this.form.checkNature = this.natureList[e.detail.value].value;
				} else if (type == 2) {
					this.form.checkTime = e.detail.value;
					let date = new Date();
					let h = date.getHours();
					let m = date.getMinutes();
					this.time = h+':'+m;
				} else if (type == 3) {
					this.changePeopleIndex = e.detail.value;
					this.form.rectifyPeople = this.personList[e.detail.value].userId
					this.form.rectifyPeopleAme = this.personList[e.detail.value].workerName
				}else if(type==4){
					this.time = e.detail.value;
				}

			},
			//获取检查部位 列表
			getCheckPointList() {
				this.sendRequest({
					url: 'xmgl/securityCheckPoints/list',
					method: 'post',
					data: {
						projectSn: this.projectSn
					},
					success: res => {
						if (res.code == 200) {
							this.checkPointList = res.result;
						}
					}
				})
			},

			//tree 树形选择器  选中--回显  
			checkPointBtn(val, type) {
				console.log(val)
				// type==1 检查项	3 通知人
				if (type == 1) {
					let arr = [];
					val.forEach((element, index) => {
						if (element.parents.length > 0) {
							element.parents.forEach((item, itemIndex) => {
								let name = element.checkname + '/' + item.checkname;
								arr.push(name)
							})
						} else {
							arr.push(element.checkname)
						}
					})
					this.form.checkPoints = arr.join(',');

				}else if (type == 3) {

					this.form.notifier = this.notifier = val.map((item, index) => {
						return item.workerName
					}).join(',');
					this.form.notifyPerson = val.map((item, index) => {
						return item.id
					}).join(',')
				}

			},
			//检查内容
			supplementTextarea(e) {
				this.form.checkContent = e.detail.value
			},
			//备注
			claimTextarea(e) {
				this.form.remarks = e.detail.value
			},
			//整改要求
			rectifyRequireTextarea(e) {
				this.form.rectifyRequire = e.detail.value
			},

			//获取人员列表---、整改人、通知人
			getPersonList() {
				this.sendRequest({
					url: 'xmgl/systemUser/getProjectChilderSystemUserList',
					method: 'post',
					data: {
						projectSn: this.projectSn
					},
					success: res => {
						console.log(1,res)
						if (res.result.length > 0) {
							this.personList = res.result;
						}
					}
				})
			},


			//是否需要整改
			switchChange(e) {
				// console.log(e.detail.value)
				if (e.detail.value) {
					this.form.isRectify = 0;
				} else {
					this.form.isRectify = 1;
				}
			},



			//上传附件
			uploadImg() {
				var that = this
				uni.chooseImage({
					count: 5 - that.imgFileList.length,
					success(res) {
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							url: that.url_config + 'upload/image', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'files',
							success: (uploadFileRes) => {
								let data = {
									name: JSON.parse(uploadFileRes.data).data[0].filename,
									url: JSON.parse(uploadFileRes.data).data[0].imageUrl
								};
								that.imgFileList.push(data)
							}
						});
					}
				})
			},
			//删除附件
			deleteImg(val, type) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除该附件吗?',
					success: function(res) {
						if (res.confirm) {
							if (type == 1) {
								let fileArr = JSON.parse(JSON.stringify(that.imgFileList));
								fileArr.forEach((item, index) => {
									if (item.url == val.url) {
										fileArr.splice(index, 1)
									}
								})
								that.imgFileList = fileArr;
							} else if (type == 2) {
								let fileArr = JSON.parse(JSON.stringify(that.videoFileList));
								fileArr.forEach((item, index) => {
									if (item.url == val.url) {
										fileArr.splice(index, 1)
									}
								})
								that.videoFileList = fileArr;
							}

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},

			//预览图片
			previewImage(url) {
				uni.previewImage({
					urls: [url]
				})
			},


			//新增保存
			addSaveBtn() {
				// console.log(this.form)
				this.form.fileUrl = this.imgFileList.length > 0 ? JSON.stringify(this.imgFileList) : '';
				let data = JSON.parse(JSON.stringify(this.form));
				if (data.checkNature == '') {
					uni.showToast({
						title: '请选择检查性质',
						icon: 'none'
					})
					return
				}

				if (data.checkPoints == '') {
					uni.showToast({
						title: '请选择检查项',
						icon: 'none'
					})
					return
				}
				if (data.checkContent == '') {
					uni.showToast({
						title: '请选择检查内容',
						icon: 'none'
					})
					return
				}
				if (data.checkTime == '') {
					uni.showToast({
						title: '请选择检查时间',
						icon: 'none'
					})
					return
				}
				
				if (data.fileUrl == '') {
					uni.showToast({
						title: '请上传附件',
						icon: 'none'
					})
					return
				}
				if (data.isRectify ==1 && data.rectifyPeople=='') {
					uni.showToast({
						title: '请选择整改人',
						icon: 'none'
					})
					return
				}
				if (data.isRectify ==1 && data.rectifyRequire == '') {
					uni.showToast({
						title: '请输入整改要求',
						icon: 'none'
					})
					return
				}
				data.checkTime = data.checkTime+'　'+this.time;
				data.projectSn = this.projectSn;
				data.inspector = JSON.parse(uni.getStorageSync('userInfo')).userId;
				console.log(1, data)
				this.sendRequest({
					url: 'xmgl/securityManage/add',
					method: 'post',
					data: data,
					success: (res) => {
						// console.log(res)
						if (res.code == 200) {
							uni.showToast({
								title: '保存成功'
							})
							setTimeout(() => {
								uni.navigateBack({})
							}, 1200)
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
						}
					}
				})
			},

		}
	}
</script>

<style scoped lang="scss">
	.flex {
		display: flex;
		align-items: center;
	}

	.flex2 {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.flex3 {
		display: flex;
	}

	.content {
		width: 100%;
		box-sizing: border-box;
		padding: 20px 30rpx;
	}

	.type {
		font-size: 30rpx;
		line-height: 40px;
		margin-bottom: 8px;
		/* border-bottom: 1px solid rgba(194, 194, 194, 0.2); */
	}

	.type .name {
		margin-right: 6px;
		width: 176rpx;
		text-align: right;
		white-space: nowrap;
	}


	.accessory {
		width: 100%;
		height: 70px;
		padding: 0 10px;
		box-sizing: border-box;
	}

	.accessoryImg {
		width: 110rpx;
		height: 60px;
		margin-right: 8px;
	}

	.picker {
		width: 70%;
		border: 1px solid rgba(42, 43, 91, 0.3);
		padding: 0px 30rpx;
		box-sizing: border-box;
		border-radius: 30px;
	}

	.uni-input {
		line-height: 32px;
	}

	.textarea {
		width: calc(70% - 32px);
		border-radius: 10px;
		border: 1px solid rgba(42, 43, 91, 0.3);
		padding: 8px 15px;
		box-sizing: border-box;
		height: 50px;
	}

	.cl {
		font-size: 30rpx;
	}


	.imgBox_wrap {
		display: flex;
		flex-wrap: wrap;
		margin-top: 10px;
		width: 75%;
	}

	.imgBox {
		width: 60px;
		height: 60px;
		display: inline-flex;
		position: relative;
		margin-right: 15px;
		margin-bottom: 8px;

		.img {
			width: 100%;
			height: 100%;
			border-radius: 10px;
		}

		.deleteImg {
			position: absolute;
			right: -10px;
			top: -18px;
		}

	}

	.addImgBox {
		border: 1px solid rgba(42, 43, 91, 0.1);
		background-color: #f6f5f8;
		width: 60px;
		height: 60px;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		border-radius: 10px;
		font-size: 12px;

		.icon-add {
			width: 18px;
			height: 18px;
		}
	}




	.ellipsis {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.status {
		color: #007AFF;
	}

	.addSaveBtn {
		margin: 30px auto;
		width: 60%;
		padding: 10px 0;
		text-align: center;
		background-color: #4181FE;
		color: #fff;
		font-size: 34rpx;
		border-radius: 30px;
		letter-spacing: 2px;
		box-shadow: 0 3px 10px rgba(65, 129, 254, 0.8);
	}

	.addSaveBtn:active {
		background-color: rgba(65, 129, 254, 0.8);
	}
</style>
