<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="退回人" class="label-title-red" prop="Nav_Reject.Nav_User.NAME" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.Nav_Reject.Nav_User.NAME" border="none"
							slot="right" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="退回原因" class="label-title-red" prop="Nav_Reject.REASON" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.Nav_Reject.REASON" border="none"
							slot="right" inputAlign="right"></u--input>
					</u-form-item>
					<view class="upload-title">附件</view>
					<full-upload v-model="model.Nav_Reject.Nav_Files" :isShowBtn='false'></full-upload>
				</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">
						<uni-collapse :border="false" accordion :accordion="true">
							<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
								v-for="(item, index) in list">
								<uni-collapse-item title="检查详情" style="font-size: 26px;" :open="true">

									<u-form-item label="检查编号" prop="Nav_SafeCheck.CODE" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="model.Nav_SafeCheck.CODE"
											border="none" slot="right" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="检查类型" prop="Nav_SafeCheck.Nav_CheckType.NAME" borderBottom>
										<u--input disabled disabledColor="#fff"
											v-model="model.Nav_SafeCheck.Nav_CheckType.NAME" border="none" slot="right"
											inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="检查层级" prop="Nav_SafeCheck.Nav_CheckTypeLevel.Nav_Enums.NAME"
										borderBottom>
										<u--input disabled disabledColor="#fff"
											v-model="model.Nav_SafeCheck.Nav_CheckTypeLevel.Nav_Enums.NAME"
											border="none" slot="right" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="检查时间" prop="Nav_SafeCheck.CHECKTIME" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="model.Nav_SafeCheck.CHECKTIME"
											border="none" slot="right" inputAlign="right"></u--input>
									</u-form-item>

									<u-form-item label="检查项目" prop="Nav_CheckProject.NAME" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="model.Nav_CheckProject.NAME"
											border="none" slot="right" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="检查区域" prop="Nav_CheckArea.NAME" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="model.Nav_CheckArea.NAME"
											border="none" slot="right" inputAlign="right"></u--input>
									</u-form-item>

									<view class="label-title">检查内容</view>
									<u-form-item borderBottom>
										<u--textarea disabled disabledColor="#fff" v-model="model.CHECKCONTENT"
											placeholder="检查内容" border="surround" autoHeight></u--textarea>
									</u-form-item>

									<u-form-item label="检查问题描述" prop="DESCREPTION" borderBottom
										@click="handleShowSheet({title: '检查问题描述', name: 'DESCREPTION',dataItem: model })">
										<u--input disabledColor="#fff" v-model="model.DESCREPTION" 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="BSQuestionLevelEnum" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="model.BSQuestionLevelEnum"
											border="none" slot="right" inputAlign="right"></u--input>
									</u-form-item>

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

									<u-form-item label="隐患地点" prop="ADDRESS" borderBottom>
										<u--input disabledColor="#fff" v-model="model.ADDRESS" slot="right"
											inputAlign="right"></u--input>
									</u-form-item>
									
									<u-form-item label="隐患原因" prop="listCheckDetailReason" borderBottom
										@click="handleChange(model)">
										<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>
										
								</uni-collapse-item>
							</uni-card>
						</uni-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>

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

		<query-selector multiple :show="selectorInfo.showPopup" :title="selectorInfo.title"
			:lists="selectorInfo.dataLists" @search="handleSearch" @select="handleSelected"
			@close="selectorInfo.showPopup=false" />
			
		<view class="bottom-button">
			<button type="primary" class="bottom" v-if='isLoadOK&&tableKey==1' @click="FullCheckUpdate">提交</button>
		</view>
	</view>

</template>

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

	import {
		GetDetailRejectModel,
		GetRejectCheckDetailDESCREPTION,
		orderPagedRiskReason,
		RejectUpdate
	} from '../../../../services/apply/subPages/BS/safeCheck'


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

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

	export default {
		data() {
			return {
				isLoadOK: false,
				tableKey: '0',
				TaskID: '',
				AUDIT_OPINION: '',
				model: {
					TaskID: '',
					Nav_Files: [],
					Nav_Reject: {
						Nav_User: {}
					},
					UserNames: '',
					DEMAND: '',
					DESCREPTION: '',
					QUESTION_LEVEL: null,
					CHECKRESULT: 10,
					ADDRESS: '',
					Nav_ListCheckDetailQuestion: [{
						ID: null,
						SAFE_CHECK_ID: null,
						SAFE_CHECK_DETAIL_ID: null,
						SAFE_CHECK_QUESTION_ID: null
					}]
				},
				list: [],
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					dataItem: {},
					name: ''
				},
				selectorInfo: {
					showPopup: false,
					title: '隐患原因',
					itemData: {},
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: ''
				},
				rules: {
					'DESCREPTION': {
						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: {
			loadData() {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId)

				extendRule(json, 'ID', 1, this.model.ID)
				//extendGroupRule(json, 'ID', 1, this.model.ID)

				extendInclude(json, "Nav_Reject.Nav_User")
				extendInclude(json, "Nav_Reject")
				extendInclude(json, "Nav_Reject.Nav_Files.Nav_ImgFile.Nav_File")
				extendInclude(json, "Nav_SafeCheck")
				extendInclude(json, "Nav_SafeCheck.Nav_CheckType")
				extendInclude(json, "Nav_SafeCheck.Nav_CheckTypeLevel.Nav_Enums")
				extendInclude(json, "Nav_CheckProject")
				extendInclude(json, "Nav_CheckArea")
				extendInclude(json, "Nav_ListCheckDetailQuestion")
				extendInclude(json, "Nav_ListCheckDetailQuestion.Nav_Question")
				extendInclude(json, "Nav_ListCheckDetailUser")
				extendInclude(json, "Nav_ListCheckDetailUser.Nav_User")
				json.IgnoreDataRule = true

				GetDetailRejectModel(json).then(res => {
					this.model = res
					if (this.model.Nav_ListCheckDetailUser != null && this.model.Nav_ListCheckDetailUser.length >
						0) {
						this.model.UserNames = ''
						this.model.Nav_ListCheckDetailUser.forEach(e => {
							if (e.Nav_User) {
								this.model.UserNames += ' ' + e.Nav_User.NAME
							}
						})
					}

					this.model.DEMAND = ''
					this.model.DESCREPTION = ''
					this.model.BSQuestionLevelEnum = ''
					if (this.model.Nav_ListCheckDetailQuestion != null && this.model.Nav_ListCheckDetailQuestion
						.length > 0) {
						this.model.Nav_ListCheckDetailQuestion.forEach(e => {
							if (e.Nav_Question != null && e.Nav_Question.DEMAND) {
								this.model.DEMAND += e.Nav_Question.DEMAND
							}
							if (e.Nav_Question != null && e.Nav_Question.DESCREPTION) {
								this.model.DESCREPTION += e.Nav_Question.DESCREPTION
							}
							if (e.Nav_Question != null && e.Nav_Question.DESCREPTION) {
								this.model.BSQuestionLevelEnum += e.Nav_Question.QUESTION_LEVEL_SHOW
							}
						})
					}

					this.isLoadOK = true
					this.list.push(this.model)
				})
			},
			FullCheckUpdate() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {

					if (this.model.CHECKRESULT == 20 && (!this.model.ADDRESS || this.model.ADDRESS.length < 1)) {
						uni.$showErrorInfo('请输入隐患地点！')
						return false
					}

					this.model.TaskID = this.TaskID
					this.model.Nav_Reject = null

					RejectUpdate(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('校验失败！')
				})
			},

			handleShowSheet(p) {
				const json = initFilter(uni.getStorageSync('orgId'))
				let dataSelect = []
				extendInclude(json, "Nav_Main")
				extendRule(json, 'ID', 1, this.model.ID);
				extendFilterGroupGroupRules(json, 'ID', 1, this.model.ID)
				//await
				dataSelect = GetRejectCheckDetailDESCREPTION(json).then(res => {
					//数据组装
					if (res != undefined && res.length > 0) {
						for (let i = 0; i < res.length; i++) {
							res[i].NAME = res[i].DESCREPTION
							res[i].CODE = i
						}
					}

					this.comPickerInfo = {
						showSheet: true,
						title: p.title,
						name: p.name,
						dataItem: p.dataItem,
						columns: [res],
						formIndex: p.formIndex
					}
				})

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

				dataItem.DEMAND = e.value[0].DEMAND
				dataItem.DESCREPTION = e.value[0].DESCREPTION
				dataItem.BSQuestionLevelEnum = e.value[0].QUESTION_LEVEL_SHOW
				dataItem.Nav_ListCheckDetailQuestion[0].SAFE_CHECK_QUESTION_ID = e.value[0].ID
				// dataItem.Nav_ListCheckDetailQuestion = []
				// dataItem.Nav_ListCheckDetailQuestion.push(e.value[0])
				// dataItem.SAFE_CHECK_QUESTION_ID = e.value[0].ID
				// 无 10 检查问题一致 20 其他 30
				if (e.value[0].QUESTION_LEVEL == null || e.value[0].QUESTION_LEVEL > 0) {
					dataItem.QUESTION_LEVEL = e.value[0].QUESTION_LEVEL
					dataItem.CHECKRESULT = 20
				} else {
					dataItem.QUESTION_LEVEL = null
					dataItem.ADDRESS = ''
					dataItem.CHECKRESULT = 10
				}
				this.comPickerInfo.showSheet = false
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},
			
			//显示组件
			handleChange(itemData) {
				this.selectorInfo.showPopup = true
				this.selectorInfo.title = '隐患原因'
				// this.selectorInfo.name = name
				// this.selectorInfo.index = val
				this.selectorInfo.itemData = itemData
				this.handleSearch()
			},
			
			//数据加载
			handleSearch(val) {
				const orgId = uni.getStorageSync('orgId')
				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
				let listReason = []
				let reasonShow = ''
				if (e.length > 0) {
					e.forEach((item, i) => {
						if (i > 0) {
							reasonShow += ' '
						}
						listReason.push({
							ID:guid(),
							CHECK_DETAIL_ID:this.selectorInfo.itemData.ID,
							RISK_REASON_ID:item.ID,
							ORG_ID:item.ORG_ID
						})
						reasonShow += item.NAME
					})
				}
				this.selectorInfo.itemData.Nav_ListCheckDetailReason = listReason
				this.selectorInfo.itemData.listCheckDetailReason = reasonShow
			}
		},
		computed: {

		}
	}
</script>

<style>
	@import url("../../../../style/css/editTemplate.css");

	/* 
	.todo-page {
		padding: 16px;
	}

	.todo-page .status-bar {
		height: 120px;
		background-image: linear-gradient(to right, #CFECFF, #6EB6FF);
		position: absolute;
		top: 0;
		left: 0;
		z-index: -10;
	}

	.todo-page .main {
		padding: 62px 8px;
		position: relative;
		z-index: 10;
		background-size: 100% 120px;
		background-repeat: no-repeat;
	}

	.todo-page .swiper-wrap {
		margin-bottom: 16px;
	}

	.tabs-bar {
		display: flex;
		flex-direction: row;
	}

	.tabs-bar .tab {
		display: flex;
		align-items: center;
		position: relative;
		margin-right: 20px;
		padding-bottom: 6px;
	}

	.tabs-bar .tab .t {
		position: relative
	}

	.tabs-bar .tab .t .name {
		font-size: 18px;
		line-height: 22px;
		color: #999;
	}

	.tabs-bar .tab .bottom-widget {
		width: 20px;
		height: 3px;
		background-color: #004F9D;
	}

	.content-list {
		padding: 10px 0;
	}

	.content-list .card {
		border-radius: 8px;
		margin-bottom: 12px;
		font-size: 14px;
		overflow: hidden;
		padding: 14px;
		box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.1);
		border: 1px solid rgba(0, 0, 0, 0.1);
	}

	.content-list .card .card-body {
		display: flex;
		flex-direction: row;
	}

	.content-list .card .card-body .left {
		margin-right: 4px;
		width: 20px;
		padding-top: 4px;
	}

	.content-list .card .card-body .left .todo-icon {
		width: 16px;
		height: 16px;
	}

	.content-list .card .card-body .contain {
		flex: 1;
	}

	.content-list .card .card-body .content {
		margin-bottom: 6px;
		display: flex;
		flex-direction: row;
	}

	.content-list .card .card-body .content-text {
		color: #333333;
		line-height: 24px;
		font-weight: bold;
		font-size: 16px;
		flex: 1;
	}

	.content-list .card .card-body .content-bottom {
		display: flex;
		flex-direction: row;
		align-items: flex-end;
	}

	.content-list .card .card-body .date-time {
		flex: 1
	}

	.content-list .card .card-body .content-status {
		width: 56px;
	}

	.content-list .card .card-body .status {
		background: rgba(255, 87, 51, 0.2);
		color: rgba(255, 87, 51, 1);
		text-align: center;
		font-size: 12px;
		width: 56px;
		height: 18px;
		line-height: 18px;
		border-radius: 2px;
	}

	.content-list .card .card-body .status.normal {
		background: rgba(0, 161, 64, 0.2);
		color: rgba(0, 161, 64, 1);
	}

	.content-list .card .card-body .status.warn {
		background: rgba(202, 158, 3, 0.2);
		color: rgba(202, 158, 3, 1);
	}

	.content-list .card .card-body .time {
		font-weight: 400;
		line-height: 18px;
		color: #333333;
		display: flex;
		flex-direction: row;
		margin-top: 4px;
		align-items: center;
	}

	.content-list .card .card-body .time .point {
		width: 8px;
		height: 8px;
		border-radius: 50%;
		margin-right: 4px;
	}

	.content-list .card .card-body .time .point.green {
		background: #00A140;
	}

	.content-list .card .card-body .time .point.red {
		background: #EE0A24;
	}

	.content-list .card .card-body .label {
		color: #666666;
		font-size: 12px;
	}

	.content-list .card .card-body .value {
		font-size: 12px;
	}

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