<!-- 指定整改落实人-->
<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">
					<u-form-item label="编号" prop="CODE" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.CODE" border="none" slot="right"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="名称" prop="NAME" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.NAME" border="none" slot="right"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="整改期限" prop="LastDateUser" borderBottom>
						<u--input disabled :value="$u.timeFormat(model.LastDateUser, ' yyyy-mm-dd')" border="none"
							slot="right" disabledColor="#fff"
							inputAlign="right"></u--input><!-- v-model="model.LastDateUser" -->
					</u-form-item>

					<view class="label-title">检查问题描述</view>
					<u-form-item borderBottom prop="DESCREPTION">
						<u--textarea disabled disabledColor="#fff" autoHeight v-model="model.DESCREPTION"
							placeholder="检查问题描述" border="surround"></u--textarea>
					</u-form-item>
					<view class="label-title">整改建议与措施</view>
					<u-form-item borderBottom prop="DEMAND">
						<u--textarea disabled disabledColor="#fff" autoHeight v-model="model.DEMAND"
							placeholder="整改建议与措施" border="surround"></u--textarea>
					</u-form-item>

					<!-- // TODO: 附件 不显示 -->

					<view class="upload-title">附件</view>
					<full-upload :value="model.Nav_Files" :isShowBtn='false' slot="right"></full-upload>

					<!-- <view class="upload-title">附件</view>
					<full-upload v-model="model.Nav_Files" :isShowBtn='false' slot="right"></full-upload> -->

					<u-form-item label="整改部门" prop="Nav_UserDeal.Nav_Department.NAME" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.Nav_UserDeal.Nav_Department.NAME"
							border="none" slot="right" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="整改责任人" prop="Nav_UserDeal.NAME" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.Nav_UserDeal.NAME" border="none"
							slot="right" inputAlign="right"></u--input>
					</u-form-item>


					<u-form-item  v-if="tableKey==1" label="隐患原因" prop="listCheckDetailReason" borderBottom
						@click="handleChange('listCheckDetailReason')">
						<u--input disabledColor="#fff" v-model="model.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  v-else label="隐患原因" prop="listCheckDetailReason" borderBottom >
						<u--input disabledColor="#fff" v-model="model.listCheckDetailReason" border="none"
							inputAlign="right"></u--input>
					</u-form-item>

					<!-- @click="handleShowSheet({title: '整改落实人', name: 'Nav_UserActualDeal.NAME'})" -->
					<u-form-item v-if="tableKey==1" label="整改落实人" required prop="Nav_UserActualDeal.NAME" borderBottom
						@click="handleChange('ActualDealNAME')"><!-- @click="handleChange(0)" -->
						<u--input disabledColor="#fff" v-model="model.Nav_UserActualDeal.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 v-else label="整改落实人"   prop="Nav_UserActualDeal.NAME" borderBottom ><!-- @click="handleChange(0)" -->
						<u--input disabledColor="#fff" v-model="model.Nav_UserActualDeal.NAME" border="none"
							inputAlign="right">
						</u--input>
					</u-form-item>

				</u--form>
			</uni-card>
		</view>

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


		<!--<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			@close="closePicker" @cancel="closePicker" keyName="NAME"></u-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 {
		GetBS049,
		UpdateActualDeal
	} from '../../../../services/apply/subPages/BS/riskSubmit'

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

	import {
		orderPagedRiskReason
	} from '../../../../services/apply/subPages/BS/safeCheck'

	// import {
	// 	data
	// } from '../../../../uni_modules/uview-ui/libs/mixin/mixin'

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


	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				dicRiskInfo: [],
				model: {
					ID: '',
					TaskID: '',
					listCheckDetailReason: '',
					Nav_UserDeal: {
						NAME: '',
						Nav_Department: {
							NAME: ''
						}
					},
					Nav_ListRiskQuestionReason: [],
					Nav_UserActualDeal: {
						NAME: ''
					},
					Nav_Files: []
				},
				isLoadOK: false,
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				selectorInfo: {
					name: '',
					isMultiple: false,
					showPopup: false,
					title: '人员选择',
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					defaultChecked: []
				},
				rules: {
					'Nav_UserActualDeal.NAME': {
						type: 'string',
						required: true,
						// message: '请选择整改落实人!',
						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
			},

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

					// if (this.model.Nav_UserActualDeal.NAME)
					// 	uni.$showErrorInfo('整改详情不能为空！')

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

					// if (err == null || err.length < 1 || err.length > 1) {
					// 	uni.$showErrorInfo('校验失败！')
					// } else {
					// 	const {
					// 		message
					// 	} = err[0]
					// 	uni.$showErrorInfo(message)
					// }
				})
			},

			loadData() {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "CODE", 1)
				extendRule(json, 'ID', 1, this.model.ID)
				extendInclude(json, "Nav_UserDeal")
				extendInclude(json, "Nav_UserActualDeal")
				extendInclude(json, "Nav_UserDeal.Nav_Department")
				extendInclude(json, "Nav_Files.Nav_ImgFile")
				extendInclude(json, "Nav_ListRiskQuestionReason.Nav_RiskReason")
				json.IgnoreDataRule = true

				GetBS049(json).then(res => {
					if (res.RiskContentState < 32 && res.ACTUAL_DEAL_USER_ID != null && res.ACTUAL_DEAL_USER_ID ==
						res.DEAL_USER_ID) {
						res.Nav_UserActualDeal.NAME = ''
					}
					var listCheckDetailReason = ''
					if (res.Nav_ListRiskQuestionReason != null && res.Nav_ListRiskQuestionReason.length > 0) {
						res.Nav_ListRiskQuestionReason.forEach(e => {
							if (e.Nav_RiskReason.NAME) {
								this.dicRiskInfo.push({
									key: e.ID,
									val: e.RISK_REASON_ID
								})
								listCheckDetailReason += ' ' + e.Nav_RiskReason.NAME
							}
						})
					}
					res.listCheckDetailReason = listCheckDetailReason
					this.model = res
					// this.Nav_ListRiskQuestionReasonFirst = res.Nav_ListRiskQuestionReasonFirst
					this.isLoadOK = true
				})
			},

			//下拉数据加载
			async handleShowSheet(p) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0)
				let dataSelect = []
				if (p.name === 'Nav_UserActualDeal.NAME') {
					dataSelect = await OrderPagedUseful(json).then(res => {
						return res
					})
				}

				if (dataSelect.length) {
					this.comPickerInfo = {
						showSheet: true,
						title: p.title,
						name: p.name,
						columns: [dataSelect]
					}
				} else {
					// 暂无数据
				}
			},
			onConfirmPicker(e) {
				const {
					name,
				} = this.comPickerInfo
				if (name == 'Nav_UserActualDeal.NAME') {
					//验收人
					this.model.Nav_UserActualDeal = e.value[0]
					this.model.ACTUAL_DEAL_USER_ID = e.value[0].ID
				}
				this.comPickerInfo.showSheet = false
			},

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


			//显示组件
			handleChange(name) {
				// ActualDealNAME  隐患原因
				this.selectorInfo.name = name
				if (name == 'ActualDealNAME') {
					this.selectorInfo.title = '人员选择'
					this.selectorInfo.isMultiple = false
					this.selectorInfo.defaultChecked = null
				} else {
					this.selectorInfo.title = '隐患原因'
					this.selectorInfo.isMultiple = true

					var defaultChecked = []
					if (this.model.Nav_ListRiskQuestionReason != null && this.model.Nav_ListRiskQuestionReason.length >
						0 && this.model
						.Nav_ListRiskQuestionReason[0].Nav_RiskReason != null) {
						this.model.Nav_ListRiskQuestionReason.forEach(e => {
							if (e.Nav_RiskReason != null && e.Nav_RiskReason.NAME != null && e.Nav_RiskReason.NAME
								.length > 0)
								defaultChecked.push(e.Nav_RiskReason)
						})
					}

					// if (defaultChecked.length < 1)
					// 	defaultChecked = null
					this.selectorInfo.defaultChecked = defaultChecked
				}
				this.selectorInfo.showPopup = true
				this.selectorInfo.index = name

				this.handleSearch()
			},

			//数据加载
			handleSearch(val) {
				const orgId = uni.getStorageSync('orgId')
				if (this.selectorInfo.name == 'ActualDealNAME') {
					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
								}
							})
						}
					})
				} else {

					const json = initFilter(orgId, "", "", 0)
					if (val) {
						extendRule(json, 'NAME', 9, val);
					}

					orderPagedRiskReason(json).then(res => {
						// if (res.IsSuccessful) {
						this.selectorInfo.dataLists = res.map(i => {
							return {
								...i,
								name: i.NAME,
								code: i.CODE
							}
						})
						// }
					})
				}

			},

			//选择信息
			handleSelected(e) {
				this.selectorInfo.showPopup = false
				if (this.selectorInfo.name == 'ActualDealNAME') {
					this.model.Nav_UserActualDeal = e
					this.model.ACTUAL_DEAL_USER_ID = e.ID
				} else {
					let listReason = []
					let reasonShow = ''
					if (e.length > 0) {
						e.forEach((item, i) => {
							if (i > 0) {
								reasonShow += ' '
							}
							var ReasonID = ''
							if (this.dicRiskInfo != null && this.dicRiskInfo.length > 0) {
								this.dicRiskInfo.forEach(dick => {
									if (dick.val == item.ID && ReasonID.length < 1) {
										ReasonID = dick.key
									}
								})
							}
							if (ReasonID == undefined || ReasonID.length < 1)
								ReasonID = guid()
							listReason.push({
								ID: ReasonID,
								RISK_SUBMIT_CONTENT_ID: this.model.ID,
								RISK_REASON_ID: item.ID,
								Nav_RiskReason: item,
								ORG_ID: item.ORG_ID
							})
							reasonShow += item.NAME
						})
					}
					this.model.Nav_ListRiskQuestionReason = listReason
					this.model.listCheckDetailReason = reasonShow
				}
			}
		},
		computed: {},
		// onNavigationBarButtonTap(e) {
		// 	this.subForms.push({
		// 		title: '检查详情' + (this.subForms.length + 1),
		// 		Nav_ListCheckDetailReason: '',
		// 	})
		// }
	}
</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;
	}

	.bottom-button {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 10px 16px;
		box-sizing: border-box;
		background: #fff;
		z-index: 999;
	} */
</style>