<template>
	<view class="todo-page">
		<view class="background"></view>
		<!-- <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="生产单元：" border-bottom>
						<u--input v-model="(model.Nav_ProductionUnit||{}).NAME" disabled disabledColor="#ffffff" border="none" inputAlign="right" fontSize="14px">
						</u--input>

					</u-form-item>
					<u-form-item label="辨识区域" prop="CHECKSTANDARD" border-bottom>
						<u--input v-model="(model.Nav_Area||{}).NAME" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
						</u--input>
					</u-form-item>
					<u-form-item label="风险名称" prop="CHECKSTANDARD" border-bottom>
						<u--input v-model="model.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="model.RISK_DESCRIPTION" disabled autoHeight maxlength="-1"></u--textarea>
					<u-form-item label="风险类别" prop="HIDDEN_LEVEL" border-bottom>
						<u--input v-model="(model.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="(model.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="(model.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="(model.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="model.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="model.EVALUATE_LEVELName" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
						</u--input>
					</u-form-item>
					<u-form-item label="应急处置" prop="EMERGENCY">

					</u-form-item>
					<u--textarea v-model="model.EMERGENCY" disabled autoHeight maxlength="-1"></u--textarea>
				<!-- 	<u-form-item label="是否有效" prop="HIDDEN_PLACE" border-bottom>
						<u--input v-model="model.ENABLE_STATUSName" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
						</u--input>
					</u-form-item> -->
					<uni-collapse class="collapse" ref="collapse">

						<uni-collapse-item title="管控层级" name="Variety components">
							<view v-if="model.Nav_Departs&&model.Nav_Departs.length!==0">
								<view v-for="(item1,index1) in model.Nav_Departs" :key="index1" style="margin: 5px 5px;padding: 0px 10px;">
									<u-form-item borderBottom>
										<view style="display: flex;flex-direction: row;justify-content: space-between;align-items: center;flex: 1;">
											<view style="flex: 1;display: flex;flex-direction: row;align-items: center;">
												<view style="flex: 1;">
													{{index1+1+'.管控部门 : ' +item1.Nav_Department.NAME}}
												</view>
											</view>
										</view>
									</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>
						</uni-collapse-item>
						<uni-collapse-item title="管控措施" name="Docs guide">
							<view v-if="model.Nav_Details&&model.Nav_Details.length!==0">
								<view v-for="(item1,index1) in model.Nav_Details" :key="index1" style="margin: 5px 5px;padding: 0px 10px;">
									<u-form-item :label="index1+1+'.管控措施'" prop="MEASURES_NAME">

									</u-form-item>
									<u--textarea v-model="item1.MEASURES_NAME" disabled autoHeight maxlength="-1"></u--textarea>
									<u-form-item label=" + 检查明细" prop="MEASURES_NAME" borderBottom @click="gotoCheck(item1)">
										<u-icon name="eye" size="22" slot="right"></u-icon>
									</u-form-item>
								</view>
							</view>
						</uni-collapse-item>
						<uni-collapse-item title="辨识岗位" name="Numerous tools">
							<view v-if="model.Nav_Posts&&model.Nav_Posts.length!==0">
								<view v-for="(item1,index1) in model.Nav_Posts" :key="index1" style="margin: 5px 5px;padding: 0px 10px;">
									<u-form-item>
										<view style="display: flex;flex-direction: row;justify-content: space-between;align-items: center;flex: 1;">
											<view style="flex: 1;display: flex;flex-direction: row;align-items: center;">
												<view style="flex: 1;">
													{{index1+1+'.辨识部门 : ' +item1.Nav_Department.NAME}}
												</view>
											</view>
										</view>
									</u-form-item>
									<u-form-item borderBottom label="辨识岗位" prop="HIDDEN_PLACE" border-bottom>
									</u-form-item>
									<u--textarea v-model="item1.Nav_PostDetailsName" disabled autoHeight maxlength="-1"></u--textarea>
								</view>
							</view>
						</uni-collapse-item>
					</uni-collapse>
				</u--form>

			</view>
		</view>
	</view>
</template>

<script>
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		extendIgnoreDataRule,
		guid,
		initFilter,
		initFilterGroup,
		transformData
	} from '../../../../utils/common'
	import {
		FMProductionUnit,
		CheckPeriods,
		CheckLevels
	} from '../../../../utils/enums.js'
	import {
		GetSKEnterpriseLibraryRecord,
	} from '../../../../services/apply/subPages/SK/SKServices.js'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	import stepTitle from '@/components/custom/step-title.vue'
	import {
		getEnum,
		GetEnumAnyOrder
	} from '../../../../services/common';
	export default {
		components: {
			stepTitle,
		},
		data() {
			return {
				showDelModalIndex: undefined,
				showDelModalIndex1: undefined,
				subsectionList: ['无隐患', '有隐患', '不涉及'],
				curNow: 1,
				isAudit: false,
				isLoadOK: false,
				tableKey: 0,
				RiskLevel: ['一般', '重大'],
				showPopupUnit: false,
				UnitDefaultIndex: [0],
				TypeLists: [],
				showPopupType: false,
				TypeDefaultIndex: [0],
				PeriodLists: [],
				showPopupPeriod: false,
				PeriodDefaultIndex: [0],
				LevelLists: [],
				showPopupLevel: false,
				LevelDefaultIndex: [0],
				showPopupDate: false,
				listPropUpload: ['NOTIFY_ID'],
				listPropValUpload: [],
				ORG_ID: uni.getStorageSync('orgId'),
				model: {
					CHECK_TYPE_ID: '',
					PLANCHECKFREQUENCY: '',
					DEPARTMENT_TYPE: 0,
					CHECK_TIME: '',
					ORG_ID: '',
					Nav_CheckRecordDetails: [{
						RISK_AREA_ID: '',
						CHECK_CONTENTS_ID: '',
						CHECKPROOF: '',
						CHECKSTANDARD: '',
						Nav_CheckNoticeDetailUsers: {
							USER_ID: ''
						},
						Nav_RiskArea: {
							NAME: ''
						},
						Nav_Contents: {
							CHECKCONTENT: ''
						},
						Nav_Question: {
							DESCREPTION: ''
						},

					}],

				},
				peopleSelectOption: {
					showSelector: false,
					value: null,
					index: 0,
					defaultChecked: []
				},
				dateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
				stepsText: ['发起', '归档', ],
				stepsPage: 0,
				Pu: '',
				Check_Type_ID: '',
				TypeListres: {},
				TaskID: '',
				poparr: '',
				enumsData: {}, // 存储所有枚举数据（键：枚举名称，值：处理后的枚举列表）
				enumsText: {} // 存储枚举文本列表（用于下拉选择等场景）

			}
		},
		onLoad(option) {
			this.TaskID = option.taskID ? option.taskID : '';
			this.model.ID = option.ID ? option.ID : '';
			this.tableKey = option.tableKey ? option.tableKey : '0'
			this.isAudit = option.isAudit
			this.fetchEnums(['SKProductionUnit', 'SKEvaluateLevelEnum', 'FMEnableStatusEnum',
				'SKDepartmentTypeEnum', 'SKPLANCHECKFREQUENCYEnum'
			]);
		},
		methods: {
			fetchEnums(enumNames) {
				var dataParm = {
					'name': enumNames.join('|')
				}
				GetEnumAnyOrder(dataParm).then(res => {
					let newd = transformData(res, enumNames)
					this.enumsData = newd[0];
					// 生成文本列表（如需要）
					this.enumsText = newd[1]
					this.loadData();
				})
			},
			loadData() {
				const json1 = initFilter(this.ORG_ID)
				extendRule(json1, 'ID', 1, this.model.ID);
				getRequest(json1, "/SK/SKRiskIdentifyResult/GetDetail").then(res => {
					const orgId = uni.getStorageSync('orgId')
					const json = initFilter(orgId, "", "")
					extendRule(json, 'PRODUCTION_UNIT_ID', 1, res.PRODUCTION_UNIT_ID);
					extendRule(json, 'AREA_ID', 1, res.AREA_ID);
					extendRule(json, 'TYPE_ID', 1, res.TYPE_ID);
					extendRule(json, 'RISK_NAME', 1, res.RISK_NAME);
					extendRule(json, 'RISK_DESCRIPTION', 1, res.RISK_DESCRIPTION);

					extendIgnoreDataRule(json)
					GetSKEnterpriseLibraryRecord(json).then(res => {
						this.stepLoad = false
						this.model = res;
						this.model.ID = this.ID
						this.model.EVALUATE_LEVELName = res.EVALUATE_LEVEL !== undefined ? this.enumsData['SKEvaluateLevelEnum'].find(item => item.code === res.EVALUATE_LEVEL).name : '';
						// this.model.ENABLE_STATUSName = res.ENABLE_STATUS !== undefined ? this.enumsData['FMEnableStatusEnum'].find(item => item.code === res.ENABLE_STATUS).name : '';
						if (this.model.Nav_Departs && this.model.Nav_Departs.length > 0) {
							this.model.Nav_Departs.map(item => {
								item.DEPARTMENT_TYPEName = item.DEPARTMENT_TYPE !== undefined ? this.enumsData['SKDepartmentTypeEnum'].find(item1 => item1.code === item.DEPARTMENT_TYPE).name : '';
							})
						}
						if (this.model.Nav_Posts && this.model.Nav_Posts.length > 0) {
							this.model.Nav_Posts.map(item1 => { //辨识岗位
								if (item1.Nav_PostDetails && item1.Nav_PostDetails.length > 0) {
									let areaShow = ''
									item1.Nav_PostDetails.map(o => {
										areaShow += o.Nav_Post.NAME + ' '
									})
									item1['Nav_PostDetailsName'] = areaShow
								} else {
									item1['Nav_PostDetailsName'] = ''
								}
							})
						}
						if (this.model.Nav_Details && this.model.Nav_Details.length > 0) {
							this.model.Nav_Details.map(item => {
								if (item.Nav_DetailContents && item.Nav_DetailContents.length > 0) {
									item.Nav_DetailContents.map(item1 => {
										if (item1.Nav_DetailBasics && item1.Nav_DetailBasics.length > 0) {
											let areaShow = ''
											item1.Nav_DetailBasics.map(o => {
												areaShow += o.Nav_Law.NAME + ' '
											})
											item1['Nav_DetailBasicsName'] = areaShow
										} else {
											item1['Nav_DetailBasicsName'] = ''
										}
										if (item1.Nav_DetailHiddens && item1.Nav_DetailHiddens.length > 0) {
											item1.Nav_DetailHiddens.map(item2 => {
												item2.HIDDEN_LEVELName = item2.HIDDEN_LEVEL !== undefined ? (item2.HIDDEN_LEVEL == 0 ? '一般' : '重大') : '';
											})
										}
										if (item1.Nav_DetailDeparts && item1.Nav_DetailDeparts.length > 0) {
											item1.Nav_DetailDeparts.map(item2 => {
												const checkTypeItem = item2.CHECK_TYPE !== undefined ?
													this.enumsData['SKDepartmentTypeEnum'].find(item => item.code === item2.CHECK_TYPE) :
													null;
												item2.CHECK_TYPEName = checkTypeItem ? checkTypeItem.name : '';

												const cycleItem = item2.CHECK_CYCLE !== undefined ?
													this.enumsData['SKPLANCHECKFREQUENCYEnum'].find(item => item.code === item2.CHECK_CYCLE) :
													null;
												item2.CHECK_CYCLEName = cycleItem ? cycleItem.name : '';
												if (item2.Nav_DetailPosts && item2.Nav_DetailPosts.length > 0) {
													let areaShow = ''
													item2.Nav_DetailPosts.map(o => {
														areaShow += o.Nav_Post.NAME + ' '
													})
													item2['Nav_DetailPostsName'] = areaShow
												} else {
													item2['Nav_DetailPostsName'] = ''
												}
											})
										}
									})
								}


							})
						}

						this.stepsPage = res.STATUS == 0 ? 0 : 1

					})
					this.isLoadOK = true
				})
				// const json = {}

			},
			gotoCheck(detailData) {
				uni.setStorageSync('detailData', detailData);
				uni.navigateTo({
					url: 'enterprise/checkDetail'
				})
			},
		},

		computed: {
			formattedCheckUsers() {
				// 收集所有检查人员并去重
				const userMap = new Map();

				// 遍历所有检查内容
				(this.model.Nav_CheckRecordDetails || []).forEach(detail => {
					// 遍历当前检查内容的检查人员
					(detail.Nav_CheckRecordDetailUsers || []).forEach(user => {
						// 过滤已删除的用户
						if (!user.IS_DELETED && user.Nav_User?.NAME) {
							// 使用用户名作为去重依据，也可以根据实际情况使用ID
							if (!userMap.has(user.Nav_User.NAME)) {
								userMap.set(user.Nav_User.NAME, user.ISCHECK);
							}
						}
					});
				});

				// 生成带颜色样式的HTML
				let html = '';
				userMap.forEach((isCheck, userName) => {
					const color = isCheck === true ? '#000000' : '#ff0000';
					html += `<span style="color: ${color}; margin-right: 10px;">${userName}</span>`;
				});

				return html;
			}
		}



	}
</script>

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

	.todo-page>>>.u-steps-item__line {
		width: 187.6px !important;
	}
</style>