<!-- 检查记录确认-->
<template>
	<view class="page-wrap">
		<view class="card">
			<uni-card margin="0" :is-shadow="true">
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="model" :rules="rules"
					ref="wForm" errorType="border-bottom">
					<view v-if='AUDIT_OPINION' class="label-title-red">驳回信息</view>
					<u-form-item v-if='AUDIT_OPINION' borderBottom>
						<u--textarea disabled autoHeight :value="AUDIT_OPINION" placeholder="请输入检查内容"
							border="surround"></u--textarea>
					</u-form-item>
					<u-form-item label="检查任务" prop="NAME" borderBottom>
						<u--input disabled disabledColor="#ffffff" v-model="model.NAME" border="none" slot="right"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="检查层级" prop="Nav_CheckTypeLevel.Nav_Enums.NAME" borderBottom>
						<u--input disabled disabledColor="#ffffff" v-model="model.Nav_CheckTypeLevel.Nav_Enums.NAME"
							border="none" slot="right" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="检查时间" prop="CREATE_TIME" borderBottom>
						<u--input disabled disabledColor="#ffffff" v-model="model.CREATE_TIME" border="none"
							slot="right" inputAlign="right"></u--input>
					</u-form-item>
					<view class="upload-title">附件</view>
					<full-upload :value="model.Nav_Files" :isShowBtn='true' :listProp='listPropUpload'
						:listPropVal='listPropValUpload'></full-upload>
				</u--form>
			</uni-card>
		</view>

		<u-sticky offset-top="20">
			<view class="sub-form">
				<uni-collapse :border="false" accordion :value="0">
					<uni-card margin="0" spacing="0" padding="0" :is-shadow="true">
						<uni-collapse-item title="上报详情">
							<view class="sub-form-wrap" style="height: 500px; overflow: auto">
								<u--form labelPosition="left" labelWidth="auto" labelAlign="center"
									errorType="border-bottom">
									<!-- <u-swipe-action> -->
										<view style="margin-bottom: 16px;"
											v-for="(item, index) in model.Nav_ListRiskSubmitContent"
											 :key="index">
											<uni-card margin="0" spacing="0" :is-shadow="false"
												style="margin-bottom: 16px;">
												<view class="label-title">检查问题</view>
												<u-form-item prop="CHECKCONTENT" borderBottom>
													<u--textarea disabled autoHeight v-model="item.CHECKCONTENT"
														placeholder="整改建议与措施" border="surround"></u--textarea>
												</u-form-item>
												<u-form-item label="问题等级" prop="QUESTION_LEVEL_SHOW" borderBottom>
													<u--input disabled disabledColor="#ffffff"
														v-model="item.QUESTION_LEVEL_SHOW" border="none"
														inputAlign="right"></u--input>
												</u-form-item>
												<view class="label-title">整改建议与措施</view>
												<u-form-item prop="DEMAND" borderBottom>
													<u--textarea disabled autoHeight v-model="item.DEMAND"
														placeholder="整改建议与措施" border="surround"></u--textarea>
												</u-form-item>
												<view class="label-title">检查依据</view>
												<u-form-item prop="PROOF" borderBottom>
													<u--textarea disabled autoHeight v-model="item.PROOF"
														placeholder="检查依据" border="surround"></u--textarea>
												</u-form-item>

												<u-form-item label="隐患原因" prop="listCheckDetailReason" borderBottom
													@click="handleShowSheet({title: '隐患原因', name: 'listCheckDetailReason', dataIndex: index})">
													<u--input disabledColor="#ffffff"
														v-model="item.listCheckDetailReason" border="none"
														inputAlign="right"></u--input>
													<u-icon style="margin-left: 4px;" slot="right"
														name="arrow-down"></u-icon>
												</u-form-item>
												<u-form-item label="隐患地点" prop="ADDRESS" borderBottom>
													<u--input disabledColor="#ffffff" v-model="item.ADDRESS"
														border="none" inputAlign="right"></u--input>
												</u-form-item>
												<view class="upload-title">附件</view>
												<full-upload v-model="item.Nav_Files" :isShowBtn='true'
													:listProp='listPropSubUpload'
													:listPropVal='item.listPropValSubUpload'></full-upload>
												<u-form-item label="整改期限" prop="LastDateUser" borderBottom
													@click="showCheckDate(index)">
													<u--input disabled disabledColor="#fff" placeholder="请选择整改期限"
														border="none"
														:value="$u.timeFormat(item.LastDateUser, 'yyyy-mm-dd')"
														inputAlign="right"></u--input>
													<u-icon style="margin-left: 4px;" slot="right"
														name="arrow-down"></u-icon>
												</u-form-item>
												<u-form-item label="系统整改期限" prop="LastDateSystem" borderBottom>
													<u--input disabledColor="#fff"
														:value="$u.timeFormat(item.LastDateSystem, 'yyyy-mm-dd')"
														border="none" inputAlign="right"></u--input>
												</u-form-item>
												<u-form-item label="整改实施单位" prop="Nav_DepartmentDeal.NAME" borderBottom
													@click="handleShowSheet({title: '整改实施单位', name: 'departmentName', dataIndex: index})">
													<u--input disabledColor="#fff"
														v-model="item.Nav_DepartmentDeal.NAME" border="none"
														inputAlign="right"></u--input>
													<u-icon style="margin-left: 4px;" slot="right"
														name="arrow-down"></u-icon>
												</u-form-item>
												<u-form-item label="整改责任人" prop="Nav_UserDeal.NAME" borderBottom
													@click="handleChange(index,'userDetailer')">
													<u--input disabledColor="#fff" v-model="item.Nav_UserDeal.NAME"
														border="none" inputAlign="right"></u--input>
													<u-icon style="margin-left: 4px;" slot="right"
														name="arrow-down"></u-icon>
												</u-form-item>
												<u-form-item label="验收人" prop="Nav_UserCheck.NAME" borderBottom
													@click="handleChange(index,'usercheck')">
													<u--input disabledColor="#fff" v-model="item.Nav_UserCheck.NAME"
														border="none" inputAlign="right"></u--input>
													<u-icon style="margin-left: 4px;" slot="right"
														name="arrow-down"></u-icon>
												</u-form-item>
											</uni-card>
										</view>
									<!-- </u-swipe-action> -->
								</u--form>

							</view>
						</uni-collapse-item>
					</uni-card>
				</uni-collapse>
			</view>
		</u-sticky>

		<query-selector :show="selectorInfo.showPopup" :title="selectorInfo.title" :lists="selectorInfo.dataLists"
			@search="handleSearch" @select="handleSelected" @close="selectorInfo.showPopup=false" />

		<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			@close="closePicker" @cancel="closePicker" keyName="NAME"></u-picker>

		<u-datetime-picker :show="dateTimePickerInfo.showCheckDate" mode="date"
			v-model='dateTimePickerInfo.defaultDateTime' @confirm="handleCheckDate"
			@close="dateTimePickerInfo.showCheckDate = false;"
			@cancel="dateTimePickerInfo.showCheckDate = false;"></u-datetime-picker>

		<view class="bottom-button">
			<button type="primary" v-if='isLoadOK&&tableKey==1' @click="FullCheckUpdate">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		guid,
		initFilter,
		extendRule,
		extendInclude,
		extendGroupRule,
		initFilterGroup,
		extendFilterGroup,
		extendFilterGroupGroupRules
	} from '../../../../utils/common'

	import {
		GetBack,
		GetSubmitContent,
		FullUpdateBS006
	} from '../../../../services/apply/subPages/BS/riskSubmit'

	import {
		orderPagedRiskReason,
	} from '../../../../services/apply/subPages/BS/safeCheck'
	// import '@/utils/showMsg.js'
	import {
		data
	} from '../../../../uni_modules/uview-ui/libs/mixin/mixin'

	import {
		OrderPagedUseful2,
		OrderPagedUseful,
		GetDpartMentNameHeader
	} from '../../../../services/common'

	import {
		auditOptionShow
	} from '../../../../services/common'

	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				listPropUpload: ['RISK_SUBMIT_ID'],
				listPropValUpload: [],
				listPropSubUpload: ['RISK_SUBMIT_ID', 'RISK_SUBMIT_CONTENT_ID'],
				AUDIT_OPINION: '',
				model: {
					ID: '',
					TaskID: '',
					Nav_CheckType: {},
					Nav_CheckTypeLevel: {
						Nav_Enums: {
							NAME: ''
						}
					},
					Nav_Files: [],
					Nav_ListRiskSubmitContent: [{
						Nav_Department: {
							NAME: ''
						},
						Nav_DepartmentDeal: {
							NAME: ''
						},
						Nav_UserCheck: {
							NAME: ''
						},
						Nav_UserDeal: {
							NAME: ''
						},
						LastDateUser: '',
						departmentName: '',
						userChecker: '',
						userDetailer: '',
						listPropValSubUpload: [],
						Nav_Files: []
					}]
				},
				isLoadOK: false,
				lists: [],
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
					value: ''
				},

				selectorInfo: {
					showPopup: false,
					title: '人员选择',
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: ''
				},

				rules: {
					'activityTopic': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					}
				},
			}
		},
		onLoad(option) {
			this.model.ID = option.ID
			this.model.TaskID = option.taskID
			this.TaskID = option.taskID
			this.tableKey = option.tableKey
			this.loadData()

		},
		methods: {
			handleTab(t) {
				this.currentTab = t
			},

			//显示时间控件
			showCheckDate(dataIndex) {
				if (this.model.Nav_ListRiskSubmitContent[dataIndex].LastDateUser == null) {
					this.model.Nav_ListRiskSubmitContent[dataIndex].LastDateUser = uni.$u.timeFormat(new Date(),
						'yyyy-mm-dd')
				}

				this.dateTimePickerInfo = {
					showCheckDate: true,
					dataIndex: dataIndex,
					defaultDateTime: this.model.Nav_ListRiskSubmitContent[dataIndex].LastDateUser
				}
			},

			//隐藏控件 显示时间
			handleCheckDate(e) {
				let v = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
				const {
					name,
					dataIndex
				} = this.dateTimePickerInfo
				this.dateTimePickerInfo.showCheckDate = false
				this.model.Nav_ListRiskSubmitContent[dataIndex].LastDateUser = uni.$u.timeFormat(e.value,
					'yyyy-mm-dd')
			},

			FullCheckUpdate() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					// const orgId = uni.getStorageSync('orgId')
					this.model.SUBMIT_STATUS = 20
					this.model.TaskID = this.TaskID
					const orgId = uni.getStorageSync('orgId')

					this.model.Nav_Check = null
					this.model.Nav_CheckType = null
					this.model.Nav_CheckTypeLevel = null

					if (!this.model.Nav_ListRiskSubmitContent)
						uni.$showErrorInfo('上报明细不能为空！')
					// debugger
					for (let i = 0; i < this.model.Nav_ListRiskSubmitContent.length; i++) {
						this.model.Nav_ListRiskSubmitContent[i].Nav_UserCheck = null
						this.model.Nav_ListRiskSubmitContent[i].Nav_UserDeal = null
						this.model.Nav_ListRiskSubmitContent[i].Nav_User = null
						this.model.Nav_ListRiskSubmitContent[i].Nav_Department = null
						this.model.Nav_ListRiskSubmitContent[i].Nav_DepartmentDeal = null

						this.model.Nav_ListRiskSubmitContent[i].Nav_SafeCheckDetail = null
						this.model.Nav_ListRiskSubmitContent[i].Nav_CheckMain = null
						this.model.Nav_ListRiskSubmitContent[i].Nav_Question = null


						this.model.Nav_ListRiskSubmitContent[i].RISK_SUBMIT_NOTICE_ID = null //重新触发通知

						if (this.model.Nav_ListRiskSubmitContent[i].Nav_ListRiskQuestionReason == null || this
							.model
							.Nav_ListRiskSubmitContent[i].Nav_ListRiskQuestionReason.length < 1) {
							uni.$showErrorInfo('子表【' + i + '】隐患原因不能为空！')
						}
						for (let j = 0; j < this.model.Nav_ListRiskSubmitContent[i].Nav_ListRiskQuestionReason
							.length; j++) {
							this.model.Nav_ListRiskSubmitContent[i].Nav_ListRiskQuestionReason[j].Nav_RiskReason =
								null;
						}

						// this.model.Nav_ListRiskSubmitContent[i].LastDateUser = this.model.Nav_ListRiskSubmitContent[i].LastDateUser
						this.model.Nav_ListRiskSubmitContent[i].ORG_ID = orgId
					}

					FullUpdateBS006(this.model).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							if (this.tableKey == null || this.tableKey == 0) {
								uni.navigateTo({
									url: 'safeCheckIndex'
								})
							} else {
								uni.navigateBack()
							}
						}, 'success', 1000)
					})
				}).catch(err => {
					// console.log('校验失败', err)
					uni.$showErrorInfo('校验失败！')
				})
			},

			loadData() {
				var that = this
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId)
				// let that = this
				// this.listPropValUpload.push(orgId)
				// this.listPropValUpload.push(this.model.ID)
				extendRule(json, 'ID', 1, this.model.ID);


				extendInclude(json, "Nav_Check")
				extendInclude(json, "Nav_CheckType")
				extendInclude(json, "Nav_CheckTypeLevel.Nav_Enums")
				extendInclude(json, "Nav_Files.Nav_ImgFile")
				extendInclude(json, "Nav_ListRiskSubmitContent")
				extendInclude(json, "Nav_ListRiskSubmitContent.Nav_CheckMain")
				extendInclude(json, "Nav_ListRiskSubmitContent.Nav_SafeCheckDetail")
				extendInclude(json, "Nav_ListRiskSubmitContent.Nav_ListRiskQuestionReason.Nav_RiskReason")
				extendInclude(json, "Nav_ListRiskSubmitContent.Nav_User")
				extendInclude(json, "Nav_ListRiskSubmitContent.Nav_Department")
				extendInclude(json, "Nav_ListRiskSubmitContent.Nav_Files.Nav_ImgFile")

				extendInclude(json, "Nav_ListRiskSubmitContent.Nav_DepartmentDeal")
				extendInclude(json, "Nav_ListRiskSubmitContent.Nav_UserDeal")
				extendInclude(json, "Nav_ListRiskSubmitContent.Nav_UserCheck")

				json.IgnoreDataRule = true
				GetBack(json).then(res => {
					this.listPropValUpload.push(res.ID)
					let listCheckDetailReason = ''
					if (res.Nav_ListRiskSubmitContent != null && res.Nav_ListRiskSubmitContent.length > 0) {
						res.Nav_ListRiskSubmitContent.forEach(e => {
							listCheckDetailReason = ''
							if (e.Nav_ListRiskQuestionReason != null && e.Nav_ListRiskQuestionReason
								.length > 0) {
								e.Nav_ListRiskQuestionReason.forEach(es => {
									listCheckDetailReason += es.Nav_RiskReason.NAME
								})
							}
							e.listCheckDetailReason = listCheckDetailReason
						})
					}
					// if (res2[i].Nav_ListRiskQuestionReason != null && res2[i]
					// 	.Nav_ListRiskQuestionReason.length > 0) {
					// 	reasons = res2[i].Nav_ListRiskQuestionReason[0].Nav_RiskReason.NAME
					// 	for (let j = 1; j < res2[i].Nav_ListRiskQuestionReason.length; j++) {
					// 		reasons = res2[i].Nav_ListRiskQuestionReason[j].Nav_RiskReason.NAME
					// 	}
					// 	res2[i].listCheckDetailReason = reasons
					// }

					this.model = res
					this.isLoadOK = true
					// listCheckDetailReason
					if (this.model.Nav_ListRiskSubmitContent[0].RiskContentState == 12) {
						auditOptionShow(this.model.Nav_ListRiskSubmitContent[0].RiskContentState, 12,
							this.model.Nav_ListRiskSubmitContent[0].APPROVE_ID).then(res2 => {
							this.AUDIT_OPINION = res2
						})
					}

				})
			},

			//下拉数据加载
			async handleShowSheet(p) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0)
				let dataSelect = []
				if (p.name === 'listCheckDetailReason') {
					// extendOrder(json, "NUM", "0")
					// NAME
					dataSelect = await orderPagedRiskReason(json).then(res => {
						return res
					})
				} else if (p.name === 'departmentName') {
					extendInclude(json, "Nav_User")
					dataSelect = await GetDpartMentNameHeader(json).then(res => {
						return res
					})
				} else if (p.name === 'userDetailer' || p.name === 'userChecker') {
					dataSelect = await OrderPagedUseful(json).then(res => {
						return res
					})
				}

				if (dataSelect.length) {
					this.comPickerInfo = {
						showSheet: true,
						title: p.title,
						name: p.name,
						dataIndex: p.dataIndex,
						columns: [dataSelect]
					}
				} else {
					// 暂无数据
				}
			},
			// 整改实施单位 departmentName Nav_DepartmentDeal  整改责任人 Nav_UserDeal  userDetailer  验收人Nav_UserCheck userChecker

			onConfirmPicker(e) {
				const {
					name,
					dataIndex
				} = this.comPickerInfo

				if (name == 'listCheckDetailReason') {
					this.model.Nav_ListRiskSubmitContent[dataIndex].listCheckDetailReason = e.value[0].NAME
					let selectReason = e.value[0]
					selectReason.RISK_REASON_ID = selectReason.ID
					selectReason.ID = guid()
					selectReason.CHECK_DETAIL_ID = this.model.Nav_ListRiskSubmitContent[dataIndex].ID;
					if (this.model.Nav_ListRiskSubmitContent[dataIndex].Nav_ListCheckDetailReason == undefined) {
						this.model.Nav_ListRiskSubmitContent[dataIndex].Nav_ListCheckDetailReason = []
						this.model.Nav_ListRiskSubmitContent[dataIndex].Nav_ListCheckDetailReason.push(selectReason)
					} else {
						this.model.Nav_ListRiskSubmitContent[dataIndex].Nav_ListCheckDetailReason[0] = selectReason
					}
				} else if (name == 'userChecker') {
					//验收人
					this.model.Nav_ListRiskSubmitContent[dataIndex].Nav_UserCheck = e.value[0]
					this.model.Nav_ListRiskSubmitContent[dataIndex].userChecker = e.value[0].NAME
					this.model.Nav_ListRiskSubmitContent[dataIndex].CHECK_USER_ID = e.value[0].ID
				} else if (name == 'userDetailer') {
					//整改责任人
					this.model.Nav_ListRiskSubmitContent[dataIndex].Nav_UserDeal = e.value[0]
					this.model.Nav_ListRiskSubmitContent[dataIndex].userDetailer = e.value[0].NAME
					this.model.Nav_ListRiskSubmitContent[dataIndex].DEAL_USER_ID = e.value[0].ID
				} else if (name == 'departmentName') {
					//整改实施单位
					this.model.Nav_ListRiskSubmitContent[dataIndex].Nav_DepartmentDeal = e.value[0]
					this.model.Nav_ListRiskSubmitContent[dataIndex].departmentName = e.value[0].NAME
					this.model.Nav_ListRiskSubmitContent[dataIndex].DEAL_DEPARTMENT_ID = e.value[0].ID
				}

				this.comPickerInfo.showSheet = false
			},


			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},


			//显示组件
			handleChange(val, name) {
				this.selectorInfo.showPopup = true
				this.selectorInfo.title = '人员选择'
				this.selectorInfo.name = name
				this.selectorInfo.index = val
				this.handleSearch()
			},

			//数据加载
			handleSearch(val) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "NAME")
				if (val) {
					extendRule(json, 'NAME', 9, val);
				}
				OrderPagedUseful2(json).then(res => {
					if (res.IsSuccessful) {
						this.selectorInfo.dataLists = res.Data.map(i => {
							return {
								...i,
								name: i.NAME,
								code: i.CODE
							}
						})
					}
				})
			},

			//选择信息
			handleSelected(e) {
				this.selectorInfo.showPopup = false
				if (this.selectorInfo.name === 'usercheck') {
					this.model.Nav_ListRiskSubmitContent[this.selectorInfo.index].Nav_UserCheck = e
					this.model.Nav_ListRiskSubmitContent[this.selectorInfo.index].userChecker = e.NAME
					this.model.Nav_ListRiskSubmitContent[this.selectorInfo.index].CHECK_USER_ID = e.ID
				} else if (this.selectorInfo.name === 'userDetailer') {
					this.model.Nav_ListRiskSubmitContent[this.selectorInfo.index].Nav_UserDeal = e
					this.model.Nav_ListRiskSubmitContent[this.selectorInfo.index].userDetailer = e.NAME
					this.model.Nav_ListRiskSubmitContent[this.selectorInfo.index].DEAL_USER_ID = e.ID
				}
			}
		},
		computed: {},
	}
</script>


<style scoped>
	@import url("../../../../style/css/editTemplate.css");
	/* 
	.page-wrap {
		padding: 16px;
	}

	.card {
		margin-bottom: 18px;
	}

	.sub-form {
		margin-bottom: 16px;
	}

	.upload-title {
		color: #303133;
		font-size: 15px;
		line-height: 22px;
		padding: 8px 0;
	}

	.page-wrap>>>.u-upload__button {
		margin-bottom: 0;
	}

	.btnEnable {
		background: #E6E6E6;
		color: #303133;
	}

	.label-title {
		color: #303133;
		font-size: 15px;
		line-height: 22px;
		padding-top: 8px;
	} */
</style>