<!-- 延期整改申请 -->
<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"
					errorType="border-bottom" ref="wForm" :rules="rules">

					<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 required v-if="TaskID" label="隐患通知" prop="NAME" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.NAME" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item required v-else label="隐患通知" prop="NAME" borderBottom @click="handleChange()">
						<u--input disabledColor="#fff" v-model="model.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="QUESTION_LEVEL_SHOW" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.QUESTION_LEVEL_SHOW" border="none"
							slot="right" inputAlign="right"></u--input>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>
		<u-sticky offset-top="20">
			<view class="sub-form">
				<view class="sub-form-wrap">
					<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom">
						<!-- ref="sForm" :rules="subRules" :model="model.Nav_ListRiskSubmitContent" -->
						<uni-collapse :border="false" accordion value="1">
							<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
								v-if="!item.IS_DELETED" v-for="(item, index) in model.Nav_ListApplyDetail">
								<uni-collapse-item :title="item.ROW_NO  + '. ' + item.Nav_SubmitContent.DESCREPTION"
									name="1">
									<!-- <u-collapse-item :title="index + 1 + '. ' + item.Nav_SubmitContent.DESCREPTION"
									name="1"> -->
									<view class="label-title">检查问题描述</view>
									<u-form-item prop="DESCREPTION" borderBottom>
										<u--textarea disabled autoHeight v-model="item.Nav_SubmitContent.DESCREPTION"
											placeholder="检查问题描述" border="surround"></u--textarea>
									</u-form-item>

									<!-- <view class="label-title">延期理由</view> -->
									<w-view-require class="label-title" title='延期理由'></w-view-require>
									<u-form-item prop="REASON" borderBottom>
										<u--textarea v-model="item.REASON" placeholder="延期理由"
											border="surround"></u--textarea>
									</u-form-item>
									<view class="label-title">临时整改建议与措施</view>
									<u-form-item prop="TEMPDEMAND" borderBottom>
										<u--textarea v-model="item.TEMPDEMAND" placeholder="临时整改建议与措施"
											border="surround"></u--textarea>
									</u-form-item>
									<u-form-item label="原整改期限" prop="LastDate" borderBottom>
										<u--input disabled disabledColor="#fff"
											:value="$u.timeFormat(item.LastDate, ' yyyy-mm-dd')" border="none"
											inputAlign="right"></u--input>
									</u-form-item>

									<u-form-item label="延期后整改期限" required style="margin-left: 10px;"
										prop="LastDateApply" borderBottom @click="showCheckDate(index)">
										<u--input disabledColor="#fff" v-model="item.LastDateApply"
											placeholder="请选择延期后整改期限" 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-if="isLoadOK" -->
										<!-- <button :disabled="!isLoadOK" type="primary"
											@click="item.IS_DELETED=true">删除</button> -->
										<!-- <button class="btnSubDel" 
											:disabled="!isLoadOK&&model.Nav_ListRiskSubmitContent&&model.Nav_ListRiskSubmitContent.length>0"
											type="primary" @click="DetailDel(item)">删除</button> -->
										<!-- <button class="btnSubDel"
											:disabled="isLoadOK&&ListApplyDetail&&ListApplyDetail.length>0"
											type="primary" @click="DetailDel(item)">删除</button> -->
										<button class="btnSubDel" type="primary" @click="DetailDel(item)">删除</button>
									</u-form-item>
								</uni-collapse-item>
							</uni-card>
						</uni-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>

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

		<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" @click="FullCheckUpdate">提交</button>
		</view>
	</view>
</template>

<script>
	import wViewRequire from '@/components/custom/w-view-require.vue'
	import {
		guid,
		initFilter,
		extendRule,
		extendInclude,
		extendGroupRule,
		initFilterGroup,
		extendFilterGroup,
		extendFilterGroupGroupRules
	} from '../../../../utils/common'

	import {
		OrderPagedNoticePerson,
		GetNoticePersonCanDelayContent,
		DelayApplyFullUpdate,
		GetAll
	} from '../../../../services/apply/subPages/BS/delayApply'

	import {
		getUserLists
	} from '../../../../services/safe'

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

	export default {
		components: {
			wViewRequire
		},
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				AUDIT_OPINION: '',
				isLoadOK: false,
				ListApplyDetail: [],
				model: {
					ID: '',
					TaskID: '00000000-0000-0000-0000-000000000000',
					NAME: '',
					// Nav_SubmitNoticePerson: {
					// 	NAME: ''
					// },
					QUESTION_LEVEL_SHOW: '',
					Nav_ListRiskSubmitContent: [{
						// Nav_SubmitContent: {
						// 	DESCREPTION: ''
						// },
						// DESCREPTION: '延期隐患',
						REASON: '',
						TEMPDEMAND: '',
						LastDate: '',
						LastDateApply: ''
					}],
					Nav_ListApplyDetail: [{
						Nav_SubmitContent: {
							DESCREPTION: '延期隐患'
						},
						REASON: '',
						TEMPDEMAND: '',
						LastDate: '',
						LastDateApply: '',
						ROW_NO: 1
					}]
				},
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},

				selectorInfo: {
					showPopup: false,
					title: '隐患通知',
					dataLists: [],
					defaultText: '',
					defaultValue: ''
				},

				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
					value: ''
				},
				showPopup: false,
				showDelModalIndex: undefined,
				rules: {
					'NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					}
				},
				// subRules: {
				// 	'REASON': {
				// 		type: 'string',
				// 		required: true,
				// 		trigger: ['blur', 'change']
				// 	},
				// 	'LastDateApply': {
				// 		type: 'string',
				// 		required: true,
				// 		trigger: ['blur', 'change']
				// 	}
				// }
			}
		},
		onLoad(option) {
			this.model.ID = option.ID
			if (option.taskID) {
				this.model.TaskID = option.taskID ? option.taskID : null
				this.TaskID = this.model.TaskID
				// 数据加载
				this.loadData()
			}
			this.tableKey = option.tableKey ? option.tableKey : '0'
		},

		methods: {
			FullCheckUpdate() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					// ele['sForm'].validate(res2 => {//不行
					// const orgId = uni.getStorageSync('orgId')
					this.model.OPERARTETYPE = 20

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

			//显示时间控件
			showCheckDate(dataIndex) {
				if (this.model.NAME == '') {
					uni.$showErrorInfo('请先选择隐患通知！')
					return false
				}

				if (this.model.Nav_ListApplyDetail[dataIndex].LastDateApply == null) {
					this.model.Nav_ListApplyDetail[dataIndex].LastDateApply = uni.$u.timeFormat(new Date(),
						'yyyy-mm-dd')
				}
				this.dateTimePickerInfo = {
					showCheckDate: true,
					dataIndex: dataIndex,
					defaultDateTime: this.model.Nav_ListApplyDetail[dataIndex].LastDateApply
				}
			},
			//隐藏控件 显示时间
			handleCheckDate(e) {
				let v = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
				const {
					name,
					dataIndex
				} = this.dateTimePickerInfo
				this.dateTimePickerInfo.showCheckDate = false
				this.model.Nav_ListApplyDetail[dataIndex].LastDateApply = uni.$u.timeFormat(e.value,
					'yyyy-mm-dd')
			},

			//显示组件
			handleChange(val) {
				this.selectorInfo.showPopup = true
				this.selectorInfo.title = '隐患通知'
				this.handleSearch(val)
			},

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

			//选择信息
			handleSelected(val) {
				this.selectorInfo.showPopup = false
				this.isLoadOK = true


				this.model.NAME = val.NAME
				this.model.RISK_SUBMIT_NOTICE_PERSON_ID = val.ID
				// this.model.Nav_SubmitNoticePerson.NAME = val.NAME
				this.model.QUESTION_LEVEL = val.QUESTION_LEVEL
				this.model.QUESTION_LEVEL_SHOW = val.QUESTION_LEVEL_SHOW
				this.model.RISK_SUBMIT_ID = val.RISK_SUBMIT_ID
				this.model.CODE = val.CODE
				this.model.NAME = val.NAME

				this.model.ID = guid()
				this.model.ORG_ID = uni.getStorageSync('orgId')
				this.model.USER_ID = uni.getStorageSync('appInfo').User.ID
				this.model.USER_NAME = uni.getStorageSync('appInfo').User.NAME
				this.model.APPLY_DEPARTMENT_ID = uni.getStorageSync('appInfo').User.DEPARTMENT_ID

				let json = initFilter(val.ORG_ID, this.model.RISK_SUBMIT_NOTICE_PERSON_ID);

				GetNoticePersonCanDelayContent(json).then(res => {
					if (res.IsSuccessful) {
						this.model.Nav_ListApplyDetail = res.Data
						// ROW_NO
						this.model.Nav_ListApplyDetail.forEach((e, i) => {
							e.ROW_NO = (++i)
						})
						this.ListApplyDetail = this.model.Nav_ListApplyDetail
						this.isLoadOK = true
					}
				})

			},

			//数据加载
			loadData() {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId)
				extendRule(json, 'ID', 1, this.model.ID);
				extendInclude(json, "Nav_SubmitNoticePerson");
				extendInclude(json, "Nav_User");
				extendInclude(json, "Nav_DepartmentApply");
				extendInclude(json, "Nav_ListApplyDetail");
				extendInclude(json, "Nav_ListApplyDetail.Nav_SubmitContent");
				json.IgnoreDataRule = true
				GetAll(json).then(res => {
					this.model = res
					if (this.model.Nav_ListApplyDetail != null && this.model.Nav_ListApplyDetail.length > 0) {
						this.model.Nav_ListApplyDetail.forEach((e, i) => {
							if (e.LastDateApply != null && e.LastDateApply.length > 0) {
								e.LastDateApply = e.LastDateApply.split(' ')[0]
							}
							e.ROW_NO = (++i)
						})
					}

					auditOptionShow(this.model.OPERARTETYPE, 50, this.model.APPROVE_ID).then(res2 => {
						this.AUDIT_OPINION = res2
						this.isLoadOK = true
					}).catch((error) => {
						uni.$showErrorInfo(error)
					})
				})
			},
			//手动删除信息 编号修改
			DetailDel(item) {
				item.IS_DELETED = true
				let indexNotDel = 1
				this.ListApplyDetail = []
				this.model.Nav_ListApplyDetail.forEach((e, i) => {
					if (!e.IS_DELETED) {
						e.ROW_NO = indexNotDel
						this.ListApplyDetail.push(e)
						indexNotDel++
					}
				})
				this.isLoadOK = true
			}
		},
		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 16px 72px;
	}

	.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;
	}

	.row-action {
		display: flex;
	}

	.row-action text {
		margin-left: 10px;
	}

	.sub-form-btns {
		padding: 4px 2px;
	}

	.sub-form-btn {
		display: flex;
	}

	.sub-form-btn .icon {
		margin-right: 4px;
	}

	.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>