<template>
	<view class="todo-page">
		<step-title :stepPage="stepsPage" :stepText="stepsText"></step-title>
		<view class="allview">
			<view class="card">
				<u--form labelWidth="auto" :model="model" ref="wForm" class="demo-ruleForm">
					<view style="position: relative;">
						<view style="position: absolute;left: -10px;color: #3d4b70;top: -3px;">*</view>
					</view>

					<view style="font-size: 15px; font-weight: bold;color: #3d4b70;margin-bottom: 10px;">基本信息填写</view>
					<u-form-item label="发起时间：" prop="Product_Unit">
						<u--input v-model="model.CREATE_TIME" disabled disabledColor="#ffffff" border="none" inputAlign="right" fontSize="14px">
						</u--input>

					</u-form-item>
					<u-form-item label="发起部门：" prop="Product_Unit">
						<u--input v-model="model.Nav_ApplyDepartment.NAME" disabled disabledColor="#ffffff" border="none" inputAlign="right" fontSize="14px">
						</u--input>

					</u-form-item>
					<u-form-item label="发起人：" prop="Product_Unit">
						<u--input v-model="model.Nav_ApplyUser.NAME" disabled disabledColor="#ffffff" border="none" inputAlign="right" fontSize="14px">
						</u--input>

					</u-form-item>
					<u-form-item label="评估年度：" prop="YEAR">
						<u--input v-model="model.YEAR" disabled disabledColor="#ffffff" border="none" inputAlign="right" fontSize="14px">
						</u--input>
					</u-form-item>
					<!-- <u-form-item label="辨识与评估内容:">
					</u-form-item>
					<u--textarea v-model="model.CONTENTS" placeholder="请输入内容" autoHeight></u--textarea> -->
					<u-form-item label="开始时间:">
						<u--input v-model="model.START_DATE" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
						</u--input>
					</u-form-item>
					<u-form-item label="结束时间:">
						<u--input v-model="model.END_DATE" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
						</u--input>
					</u-form-item>
					<u-form-item label="评价要求:">
					</u-form-item>
					<u--textarea v-model="model.Nav_Require.NAME" disabled autoHeight v-if="model.Nav_Require"></u--textarea>
					<view class="upload-title">附件：</view>
					<full-upload v-model="model.Nav_Files" :isShowBtn='false' :listProp='listPropUpload' :listPropVal='listPropValUpload'></full-upload>

				</u--form>
				<u--form labelWidth="auto" :model="model" ref="wForm" class="sub-form">


					<view style="position: relative;">

						<view style="position: absolute;left: -10px;color: #3d4b70;top: -3px;">*</view>
					</view>

					<view style="font-size: 15px; font-weight: bold;color: #3d4b70;margin-bottom: 10px;margin-top: 20px;">辨识内容</view>
					<view v-for="(item,index) in model.Nav_Details" :key="index" style="border-bottom: #ceccca 1px solid;margin-bottom: 10px;padding-bottom: 10px;">
						<u-form-item prop="Nav_Contents" :label="index+1+'.辨识区域：'">

							<view style="display: flex;flex-direction: row;justify-content: space-between;align-items: center;flex: 1;">
								<u--input v-model="item.Nav_Area.NAME" disabled disabledColor="#ffffff" border="none" fontSize="14px">
								</u--input>
								<view class="action" @click.stop style="display: flex;flex-direction: row;align-items: center;justify-content: space-around;">
									<u-icon name="arrow-up" size="12" v-if="item.showPack" @click="handlePack(index)"></u-icon>
									<u-icon name="arrow-down" size="12" v-if="!item.showPack" @click="handlePack(index)"></u-icon>
								</view>
							</view>

						</u-form-item>
						<view v-if="item.showPack==true">
							<u-form-item label="生产单元：" border-bottom>
								<u--input v-model="item.MineTypeName" disabled disabledColor="#ffffff" placeholder="请选择生产单元" border="none" inputAlign="right" fontSize="14px">
								</u--input>
							</u-form-item>
							<u-form-item label="风险名称" prop="CHECKSTANDARD" border-bottom>
								<u--input v-model="item.RISK_NAME" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
								</u--input>
							</u-form-item>
							<u-form-item label="风险描述" prop="HIDDEN_DESCRIPTION">
							</u-form-item>
							<u--textarea v-model="item.RISK_DESCRIPTION" disabled autoHeight maxlength="-1"></u--textarea>
							<u-form-item label="风险类别" prop="HIDDEN_LEVEL" border-bottom>
								<u--input v-model="(item.Nav_Type||{}).NAME" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
								</u--input>
							</u-form-item>
							<u-form-item label="风险评价L" border-bottom>
								<u--input v-model="(item.Nav_SCOREL||{}).SCORE" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
								</u--input>
							</u-form-item>
							<u-form-item label="风险评价E" border-bottom>
								<u--input v-model="(item.Nav_SCOREE||{}).SCORE" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
								</u--input>
							</u-form-item>
							<u-form-item label="风险评价C" border-bottom>
								<u--input v-model="(item.Nav_SCOREC||{}).SCORE" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
								</u--input>
							</u-form-item>
							<u-form-item label="分值D" border-bottom>
								<u--input v-model="item.EVALUATE_SCORE" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
								</u--input>
							</u-form-item>
							<u-form-item label="风险等级" prop="HIDDEN_PLACE" border-bottom>
								<u--input v-model="item.EVALUATE_LEVELName" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
								</u--input>
							</u-form-item>
							<u-form-item label="应急处置" prop="EMERGENCY" borderBottom>
								<u--input disabledColor="#ffffff" disabled v-model="item.EMERGENCY" border="none" inputAlign="right"></u--input>
							</u-form-item>
							<u-form-item label="管控信息" prop="HIDDEN_PLACE" @click="changeControl(index)" v-if="item.Nav_DetailMeasures.length!==0 ||item.Nav_DetailDeparts.length!==0 ">
								<u-icon slot="right" name="plus-circle" size="18" v-if="!item.isShowControl">
								</u-icon>
								<u-icon slot="right" name="minus-circle" size="18" v-if="item.isShowControl">
								</u-icon>
							</u-form-item>
							<view v-if="item.isShowControl">
								<u-subsection :list="childList" :current="item.curNow" @change="sectionChange(index,$event)"></u-subsection>
								<view v-if="item.curNow==0&&item.Nav_DetailMeasures.length!==0">
									<view v-for="(item1,index1) in item.Nav_DetailMeasures" :key="index1" style="margin: 5px 5px;padding: 0px 10px;">
										<u-form-item :label="index1+1+'.顺序'" prop="NUM" borderBottom>
											<u-number-box integer disabled v-model="item1.NUM" :min="-10000" :max="10000"></u-number-box>
										</u-form-item>
										<u-form-item label="管控措施" prop="MEASURES_NAME" borderBottom>
											<u--input disabledColor="#ffffff" disabled v-model="item1.MEASURES_NAME" border="none" inputAlign="right"></u--input>
										</u-form-item>
										<!-- <u-form-item label="应急处置" prop="EMERGENCY" borderBottom>
											<u--input disabledColor="#ffffff" disabled v-model="item1.EMERGENCY" border="none"  inputAlign="right"></u--input>
										</u-form-item> -->
									</view>

								</view>

								<view v-if="item.curNow==1&&item.Nav_DetailDeparts.length!==0">
									<view v-for="(item1,index1) in item.Nav_DetailDeparts" :key="index1" style="margin: 5px 5px;padding: 0px 10px;">
										<u-form-item :label="index1+1+'.管控部门:'" borderBottom>
											<u--input disabledColor="#ffffff" disabled v-model="item1.Nav_Department.NAME" border="none" inputAlign="right"></u--input>
										</u-form-item>
										<u-form-item borderBottom label="管控层级：" prop="Check_Level">
											<u--input v-model="item1.DEPARTMENT_TYPEName" disabled disabledColor="#ffffff" border="none" inputAlign="right" fontSize="14px">

											</u--input>
										</u-form-item>
										<u-form-item borderBottom label="管控责任人" prop="HIDDEN_PLACE" border-bottom>
											<u--input v-model="item1.Nav_User.NAME" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
											</u--input>
										</u-form-item>
									</view>
								</view>

							</view>
						</view>
					</view>
				</u--form>
			</view>
			<check-action v-if="isLoadOK && tableKey === '1'&&isAudit==='true' " :okApi="okApi" :refuseApi="refuseApi" :dataSource="$store.state.auditModel"></check-action>
		</view>
	</view>
</template>

<script>
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		extendIgnoreDataRule,
		guid,
		initFilter,
		initFilterGroup,
		handleOriginalArray
	} from '../../../../utils/common'
	import {
		FMProductionUnit,
		SKEffective,
		SKMarkEnum,
		SKLevelEnum,
		CheckPeriods,
		CheckLevels,
		MineTypeIndex,
		MineTypeName
	} from '../../../../utils/enums.js'
	import {
		EvaluationGet,
		EvaluationFullUpdate,
		getUserLists,
		GetSKRequestInfo,
		GetRiskAreaInfo,
		GetRiskTypeInfo,
		GetDepartmentInfo
	} from '../../../../services/apply/subPages/SK/SKServices.js'
	import {
		getEnum
	} from '../../../../services/common';
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	import stepTitle from '@/components/custom/step-title.vue'
	import swipeActionItem from '../../../../uni_modules/uview-ui/libs/config/props/swipeActionItem';
	export default {
		components: {
			stepTitle,
		},
		data() {
			return {
				isAudit: false,
				isLoadOK: false,
				tableKey: 0,
				listPropUpload: ['NOTIFY_ID'],
				listPropValUpload: [],
				ORG_ID: uni.getStorageSync('orgId'),
				stepLoad: true,
				model: {
					CREATE_TIME: '',
					Nav_ApplyDepartment: {
						NAME: ''
					},
					Nav_ApplyUser: {
						NAME: ''
					},
					YEAR: '',
					START_DATE: '',
					END_DATE: '',
					Nav_Require: {
						NAME: ''
					},
					RequireName: '',
					Nav_Files: [],
					Nav_Details: [{
						showPack: false,
						MineType: '',
						Nav_Area: {
							NAME: ''
						},
						RISK_NAME: '',
						Nav_RiskName: {
							NAME: ''
						},
						RISK_DESCRIPTION: '',
						Nav_Type: {
							NAME: ''
						},
						EVALUATE_L: 0,
						EVALUATE_E: 0,
						EVALUATE_C: 0,
						EVALUATE_SCORE: 0,
						EVALUATE_LEVEL: '',
						Nav_DetailDeparts: [],
						Nav_DetailMeasures: [],
						isShowControl: false,
					}]
				},
				dateRecitifyTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
				stepsText: ['发起', '审批', '归档'],
				stepsPage: 0,
				stepsPageList: 0,
				TaskID: '',
				nowName: '',
				nowIndex: 0,
				childList: ['管控措施', '管控层级'],
				curNow: 0,
				enumsData: {}, // 存储所有枚举数据（键：枚举名称，值：处理后的枚举列表）
				enumsText: {} // 存储枚举文本列表（用于下拉选择等场景）

			}
		},
		onLoad(option) {
			this.TaskID = option.taskID ? option.taskID : '';
			this.ID = option.ID ? option.ID : '';
			this.tableKey = option.tableKey ? option.tableKey : '0'
			this.isAudit = option.isAudit
			this.okApi = option.okApi
			this.refuseApi = option.refuseApi
			this.auditModel = {
				...this.$store.state.auditModel,
				TaskID: option.taskID ? option.taskID : ''
			}
			this.fetchEnums(['SKProductionUnit', 'SKEvaluateLevelEnum', 'SKDepartmentTypeEnum']);
		},
		methods: {
			async fetchEnums(enumNames) {
				try {
					// 存储所有枚举请求的Promise
					const enumPromises = enumNames.map(name => {
						return getEnum({
							name
						}).then(res => {
							// 统一处理枚举格式：添加name/code字段，便于后续映射
							return {
								enumName: name,
								data: res.map(item => ({
									...item,
									name: item.NAME, // 统一显示名称字段
									code: item.ID // 统一值字段
								}))
							};
						});
					});

					// 等待所有枚举请求完成
					const results = await Promise.all(enumPromises);
					// 存储枚举结果到data中（键为枚举名称，值为处理后的数据）
					results.forEach(({
						enumName,
						data
					}) => {
						this.enumsData[enumName] = data;
						// 生成文本列表（如需要）
						this.enumsText[enumName] = data.map(item => item.name);
					});
					// 所有枚举获取完成后，执行loadData
					this.loadData();
				} catch (error) {
					console.error('枚举请求失败:', error);
					uni.showToast({
						title: '枚举数据加载失败',
						icon: 'none'
					});
				}
			},
			loadData() {
				// const json = {}
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "")
				extendRule(json, 'ID', 1, this.ID);
				extendInclude(json, 'Nav_ApplyUser');
				extendInclude(json, 'Nav_ApplyDepartment');
				extendInclude(json, 'Nav_Require');
				extendInclude(json, 'Nav_Details');
				extendInclude(json, 'Nav_Details.Nav_Area');
				extendInclude(json, 'Nav_Details.Nav_Type');
				extendInclude(json, 'Nav_Details.Nav_RiskName');
				extendInclude(json, 'Nav_Details.Nav_DetailMeasures');
				extendInclude(json, 'Nav_Details.Nav_DetailDeparts.Nav_Department');
				extendInclude(json, 'Nav_Details.Nav_DetailDeparts.Nav_User');
				extendInclude(json, 'Nav_Details.Nav_DetailPosts.Nav_Post');
				extendInclude(json, 'Nav_Files');
				extendInclude(json, 'Nav_Files.Nav_ImgFile');
				extendIgnoreDataRule(json)
				// extendRule(json, 'TASK_ID', 1, this.TaskID);
				EvaluationGet(json).then(res => {
					this.model = res;
					this.model.CREATE_TIME = uni.$u.timeFormat(res.CREATE_TIME, 'yyyy-mm-dd')
					this.stepsPage = res.STATUS == 0 ? 0 : res.STATUS == 20 ? 1 : 2
					this.model.START_DATE = uni.$u.timeFormat(res.START_DATE, 'yyyy-mm-dd')
					this.model.END_DATE = uni.$u.timeFormat(res.END_DATE, 'yyyy-mm-dd')
					this.model.Nav_Details.map(item => {
						item['showPack'] = false
						item['isShowControl'] = false
						item['curNow'] = 0
						item.Nav_SCOREL = item.Nav_SCOREL ? item.Nav_SCOREL : {
							SCORE: ''
						}
						item.Nav_SCOREE = item.Nav_SCOREE ? item.Nav_SCOREE : {
							SCORE: ''
						}
						item.Nav_SCOREC = item.Nav_SCOREC ? item.Nav_SCOREC : {
							SCORE: ''
						}
						item.MineTypeName = item.MineType !== undefined ? this.enumsData['SKProductionUnit'].find(item1 => item1.code === item.MineType).name : '';
						item.EVALUATE_LEVELName = item.EVALUATE_LEVEL !== undefined ? this.enumsData['SKEvaluateLevelEnum'].find(item1 => item1.code === item.EVALUATE_LEVEL).name : '';
						item.Nav_DetailDeparts.map(item1 => {
							item1.DEPARTMENT_TYPEName = item1.DEPARTMENT_TYPE !== undefined ? this.enumsData['SKDepartmentTypeEnum'].find(item2 => item2.code === item1.DEPARTMENT_TYPE).name : '';
						})
					})
					this.stepLoad = false

				})
				this.isLoadOK = true
			},
			sectionChange(index, e) {
				this.model.Nav_Details[index].curNow = e
				this.model.Nav_Details = [...this.model.Nav_Details]
			},
			changeControl(index) {
				this.model.Nav_Details[index].isShowControl = this.model.Nav_Details[index].isShowControl == false ? true : false
				this.model.Nav_Details = [...this.model.Nav_Details]
			},
			handlePack(index) {
				this.model.Nav_Details[index].showPack = this.model.Nav_Details[index].showPack == false ? true : false
				this.model.Nav_Details = [...this.model.Nav_Details]

			},


		},
	}
</script>

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

	.sub-form>>>.u-form-item__body__right__content__slot {
		display: flex;
		justify-content: flex-end;
	}
</style>