<!-- 检查记录确认 -->
<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="Nav_CheckType.NAME" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.Nav_CheckType.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="#fff" v-model="model.Nav_CheckTypeLevel.Nav_Enums.NAME"
							border="none" slot="right" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="检查时间" prop="CHECKTIME" borderBottom> <!-- CREATE_TIME -->
						<u--input disabled disabledColor="#fff" v-model="model.CHECKTIME" border="none" slot="right"
							inputAlign="right"></u--input> <!-- CREATE_TIME -->
					</u-form-item>

					<view class="label-title">检查区域</view>
					<u-form-item prop="RiskAreaNAME" borderBottom>
						<u--textarea disabled autoHeight v-model="model.RiskAreaNAME" placeholder="请输入检查内容"
							border="surround"></u--textarea>
					</u-form-item>
					<!-- <view class="label-title">检查项目分类</view>
					<u-form-item prop="CheckProjectCategoryNAME" borderBottom>
						<u--textarea disabled autoHeight v-model="model.CheckProjectCategoryNAME" placeholder="请输入检查内容"
							border="surround"></u--textarea>
					</u-form-item> -->
					<view class="upload-title">附件</view>
					<full-upload v-model="model.Nav_Files" :isShowBtn='false'></full-upload>
				</u--form>

			</uni-card>
		</view>

		<!-- ToDo -->
		<!-- 这4个有问题  确认 检查区域  检查项目 退回 -->

		<u-sticky offset-top="20">
			<view class="sub-form">
				<view class="sub-form-wrap">
					<view class="sub-form-btns" v-if="model.Nav_ListSafeCheckDetail.length>0">
						<view class="sub-form-btn" @click="handleCheck">
							<u-icon class="icon" name="checkbox-mark" color="#2979ff" size="14"></u-icon>
							<u--text type="primary" text="批量确认"></u--text>
						</view>
					</view>

					<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom"
						ref="sForm">
						<u-collapse :border="false" accordion>
							<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
								:options="options" v-for="(item, index) in model.Nav_ListSafeCheckDetail">
								<uni-collapse-item :title="index + 1 + '. ' + item.CHECKCONTENT"  :thumb='item.QUESTION_LEVEL==null||item.QUESTION_LEVEL==0 ?null:"../../../../static/warm.png"'>
									<view slot="value" class="row-action">
										<!-- <text v-if="!item.Nav_ListCheckDetailUser[0].ISCHECK" -->
										<text v-if="!item.ISCHECK" @click.stop="subCheckOK(item)" class="enableTrue">
											<u--text type="primary" class="btnCheck" text="确认"></u--text>
										</text>
										<text v-else class="enableFalse">
											<u--text type="primary" class="btnCheck" text="确认"></u--text>
										</text>
									</view>

									<u-form-item label="检查区域" prop="AreaNAME" style="margin-left: 2px" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.AreaNAME" border="none"
											inputAlign="right"></u--input>
									</u-form-item>

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

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

									<view class="label-title">检查问题描述</view>
									<u-form-item prop="DESCREPTION" borderBottom>
										<u--textarea disabled autoHeight v-model="item.DESCREPTION"
											placeholder="请输入检查问题描述" border="surround"></u--textarea>
									</u-form-item>

									<u-form-item label="问题等级" prop="QUESTION_LEVEL_DESCRIPTION" borderBottom>
										<u--input disabled disabledColor="#fff"
											v-model="item.QUESTION_LEVEL_DESCRIPTION" border="none"
											inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="整改建议与措施" prop="CHECKRESULT_DESCRIPTION" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.CHECKRESULT_DESCRIPTION"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="检查人员" prop="checkerNames" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.checkerNames"
											border="none" inputAlign="right"></u--input>
									</u-form-item>

									<u-form-item label="隐患地点" prop="ADDRESS" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.ADDRESS" border="none"
											inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="备注" prop="REMARK" borderBottom>
										<u--input disabledColor="#fff" placeholder='备注' v-model="item.REMARK"
											inputAlign="right"></u--input>
									</u-form-item>
									<view class="upload-title">附件</view>
									<full-upload v-model="item.Nav_Files" :isShowBtn='false'></full-upload>

									<u-form-item borderBottom style="text-align: center;">
										<button type="primary" class="btnSubDel" style="width: 35%;"
											@click="handleback(item)" :disabled="item.ISCHECK">退回</button>
										<!-- :disabled="item.Nav_ListCheckDetailUser[0].ISCHECK">退回</button> -->
									</u-form-item>
								</uni-collapse-item>
							</uni-card>
						</u-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>

		<!-- <query-selector :show="showPopup" :lists="userLists" @close="handleClosePopup" @search="handleSearchUser"
			@select="handleSelectedUser" /> -->
		<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			@close="closePicker" @cancel="closePicker" keyName="NAME"></u-picker>
		<!--<button type="primary" v-if='isLoadOK' @click="FullCheckUpdate"
			 style="position: fixed; bottom: 5px; width: 92%;">提交</button> -->
		<!-- <button type="primary" @click="submit">提交</button> -->


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



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

	import {
		GetUserCheckInfoBS034,
		FullCheckUpdateBS034
	} from '../../../../services/apply/subPages/BS/safeCheck'
	// import '@/utils/showMsg.js'

	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				deis: false,
				model: {
					ID: '',
					TaskID: '',
					Nav_Files: [],
					Nav_CheckType: {
						NAME: ''
					},
					Nav_CheckTypeLevel: {
						Nav_Enums: {
							NAME: ''
						}
					},

					Nav_ListCheckRiskArea: [{
						Nav_RiskArea: {
							NAME: ''
						}
					}],
					Nav_ListCheckProjectCategory: [{
						Nav_CheckProjectCategory: {
							NAME: ''
						}
					}],
					CHECKTIME: '',
					RiskAreaNAME: '',
					// CheckProjectCategoryNAME: '',
					Nav_ListSafeCheckDetail: {
						ID: '',
						SAFE_CHECK_ID: '',
						Nav_Files: [],
						Nav_CheckArea: {
							NAME: '',
							ID: ''
						},
						Nav_CheckProject: {
							NAME: ''
						},
						ProjectNAME: '',
						AreaNAME: '',
						checkerNames: '',
						CHECKCONTENT: '',
						CHECKRESULT_DESCRIPTION: '',
						DESCREPTION: '',
						QUESTION_LEVEL_DESCRIPTION: '',
						DEMAND: '',
						ADDRESS: '',
						ISCHECK: false,
						Nav_ListCheckDetailUser: [{
							NAME: '',
							ISSAMEMAINCHECKOK: false,
							ISCHECK: false
						}]

					} //子表
				},
				isLoadOK: false,
				lists: [],
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				showCheckDate: false,
				showPopup: false,

				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()
		},

		//页面加载 退回 反过来 都要加载信息
		onShow() {
			this.loadData()
		},
		methods: {
			handleTab(t) {
				this.currentTab = t
			},
			FullCheckUpdate() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					const orgId = uni.getStorageSync('orgId')
					this.model.TaskID = this.TaskID
					this.model.Nav_ListSafeCheckDetail[0].Nav_ListCheckDetailUser[0].SAFECHECKSTATE = 80
					FullCheckUpdateBS034(this.model).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							if (this.tableKey == null || this.tableKey == 0) {
								uni.navigateTo({
									url: 'safeCheckIndex'
								})
							} else {
								uni.navigateBack()
							}
						}, 'success', 1000)
						// if (res) {
						//   message.success('提交成功', 2000)
						// }
					})
				}).catch(err => {
					console.log('校验失败', err)
				})
			},

			loadData() {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "CODE", 1)
				extendRule(json, 'ID', 1, this.model.ID);
				extendInclude(json, "Nav_CheckType");
				extendInclude(json, "Nav_CheckTypeLevel.Nav_Enums");
				extendInclude(json, "Nav_ListCheckRiskArea");
				extendInclude(json, "Nav_ListCheckRiskArea.Nav_RiskArea");
				// extendInclude(json, "Nav_ListCheckProject");
				// extendInclude(json, "Nav_ListCheckProject.Nav_CheckProject");
				// extendInclude(json, "Nav_ListCheckProjectCategory");
				// extendInclude(json, "Nav_ListCheckProjectCategory.Nav_CheckProjectCategory");
				extendInclude(json, "Nav_Files.Nav_ImgFile.Nav_File");
				extendInclude(json, "Nav_ListSafeCheckDetail");
				extendInclude(json, "Nav_ListSafeCheckDetail.Nav_CheckArea");
				extendInclude(json, "Nav_ListSafeCheckDetail.Nav_CheckProject");
				extendInclude(json, "Nav_ListSafeCheckDetail.Nav_CheckQuestion");
				extendInclude(json, "Nav_ListSafeCheckDetail.Nav_ListCheckDetailQuestion.Nav_Question");
				extendInclude(json, "Nav_ListSafeCheckDetail.Nav_Files.Nav_ImgFile.Nav_File");

				json.IgnoreDataRule = true
				GetUserCheckInfoBS034(json).then(res => {

					if (res.Nav_ListSafeCheckDetail != undefined && res.Nav_ListSafeCheckDetail.length > 0) {
						let checkerNames = ''
						let ProjectNAME = ''
						let AreaNAME = ''
						let DESCREPTION = ''
						for (let i = 0; i < res.Nav_ListSafeCheckDetail.length; i++) {
							checkerNames = ''
							ProjectNAME = ''
							AreaNAME = ''
							DESCREPTION = ''
							res.Nav_ListSafeCheckDetail[i].ISCHECK = false

							for (let j = 0; j < res.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser
								.length; j++) {
								checkerNames += ' ' + res.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser[j]
									.Nav_User.NAME

								if (j == 0) {
									res.Nav_ListSafeCheckDetail[i].ISCHECK = res.Nav_ListSafeCheckDetail[i]
										.Nav_ListCheckDetailUser[j].ISCHECK
								}
							}
							res.Nav_ListSafeCheckDetail[i].checkerNames = checkerNames.substring(1)

							if (res.Nav_ListSafeCheckDetail[i].Nav_CheckProject) {
								ProjectNAME += ' ' + res.Nav_ListSafeCheckDetail[i].Nav_CheckProject.NAME
							}
							res.Nav_ListSafeCheckDetail[i].ProjectNAME = ProjectNAME.substring(1)

							if (res.Nav_ListSafeCheckDetail[i].Nav_CheckArea) {
								AreaNAME += ' ' + res.Nav_ListSafeCheckDetail[i].Nav_CheckArea.NAME
							}
							res.Nav_ListSafeCheckDetail[i].AreaNAME = AreaNAME.substring(1)

							//修改不兼容
							if (res.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser == null && res
								.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser.length > 0) {
								res.Nav_ListSafeCheckDetail[i].ISCHECK = res.Nav_ListSafeCheckDetail[i]
									.Nav_ListCheckDetailUser[0].ISCHECK
							} else {
								res.Nav_ListSafeCheckDetail[i].ISCHECK = false
							}

							if (res.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailQuestion != null && res
								.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailQuestion.length > 0) {
								res.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailQuestion.forEach(e => {
									DESCREPTION += ' ' + e.Nav_Question.DESCREPTION
								})
							}
							res.Nav_ListSafeCheckDetail[i].DESCREPTION = DESCREPTION
							if (res.Nav_ListSafeCheckDetail[i].CHECKRESULT == 10 || res.Nav_ListSafeCheckDetail[i]
								.CHECKRESULT == 15)
								res.Nav_ListSafeCheckDetail[i].CHECKRESULT_DESCRIPTION = ''
						}
					}

					this.model = res
					//数据组装
					let strRiskAreaNAME = ''
					if (res.Nav_ListCheckRiskArea && res.Nav_ListCheckRiskArea.length > 0) {
						for (let i = 0; i < res.Nav_ListCheckRiskArea.length; i++) {
							strRiskAreaNAME += (strRiskAreaNAME.length > 0 ? " " : "") + res.Nav_ListCheckRiskArea[
								i].Nav_RiskArea.NAME;
						}
					}
					// let CheckProjectCategoryNAME = ''
					// if (res.Nav_ListCheckProjectCategory && res.Nav_ListCheckProjectCategory.length > 0) {
					// 	for (let i = 0; i < res.Nav_ListCheckProjectCategory.length; i++) {
					// 		CheckProjectCategoryNAME += (CheckProjectCategoryNAME.length > 0 ? " " : "") + res
					// 			.Nav_ListCheckProjectCategory[i].Nav_CheckProjectCategory.NAME;
					// 	}
					// }

					this.model.RiskAreaNAME = strRiskAreaNAME
					// this.model.CheckProjectCategoryNAME = CheckProjectCategoryNAME
					this.isLoadOK = true
				})
			},
			//确认
			subCheckOK(detail) {
				if (detail.Nav_ListCheckDetailUser.length > 0) {

					detail.ISCHECK = true;

					detail.Nav_ListCheckDetailUser[0].ISCHECK = true;
					detail.Nav_ListCheckDetailUser[0].ISSAMEMAINCHECKOK = true;

				}
			},
			handleCheck() {
				this.model.Nav_ListSafeCheckDetail.forEach(item => {

					if (item.Nav_ListCheckDetailQuestion != null && item.Nav_ListCheckDetailQuestion.length > 0) {
						for (let j = 0; j < item.Nav_ListCheckDetailQuestion.length; j++) {
							if (item.Nav_ListCheckDetailQuestion[j].IS_DELETED != undefined && item
								.Nav_ListCheckDetailQuestion[j].IS_DELETED != null && !item
								.Nav_ListCheckDetailQuestion[j].IS_DELETED) {
								if (item.Nav_ListCheckDetailQuestion[j].Nav_Question?.ID.indexOf(
										"5555-555555555555") > 0 || item.Nav_ListCheckDetailQuestion[j]
									.Nav_Question?.ID.indexOf(
										"666-666666666666") > 0) {
									item.Nav_ListCheckDetailUser[0].ISCHECK = true
									item.Nav_ListCheckDetailUser[0].ISSAMEMAINCHECKOK = true
									item.ISCHECK = true
									break
								}
							}
						}
					}
				})
			},
			//退回
			handleback(detail) {
				uni.navigateTo({
					url: 'safeCheckCheckBackInfo?ID=' + detail.ID + '&SAFE_CHECK_ID=' + detail.SAFE_CHECK_ID
				})
			}
		},
		computed: {

		}
	}
</script>


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

	.enableFalse>>>.u-text__value--primary {
		color: gray;
	}
</style>