<template>
	<view class="todo-page" style="padding-bottom: 10px;">
		<view class="background"></view>

		<view class="card" style="padding-bottom: 20px;">
			<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>
				<uni-collapse :border="false" accordion>
					<view v-for="(item, index) in showData" :key="index">
						<uni-collapse-item title-border="none" :border="false">
							<template v-slot:title>
								<view class="uni-collapse-item__title-box">
									<view class="uni-collapse-item__title-text">
										{{item.Nav_Area?index+1+'.'+item.Nav_Area.NAME:index+1+'.'}}
									</view>
								</view>
							</template>
							<view class="content" style="padding: 0px 0px 0px 10px;">
								<u-form-item label="生产单元" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="item.MINE_NAME" border="none" inputAlign="right"></u--input>

								</u-form-item>
								<u-form-item label="辨识区域" prop="Nav_Contents.CHECKCONTENT" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="(item.Nav_Area||{}).NAME" border="none" inputAlign="right"></u--input>

								</u-form-item>
								<u-form-item label="风险名称" prop="HIDDEN_PLACE" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="item.RISK_NAME" border="none" inputAlign="right"></u--input>
								</u-form-item>
								<u-form-item label="风险描述" prop="HIDDEN_PLACE" borderBottom>
								</u-form-item>
								<u--textarea fontSize="14px" disabled disabledColor="#fff" v-model="item.RISK_DESCRIPTION" border="none" inputAlign="right" autoHeight></u--textarea>
								<u-form-item label="风险类别" prop="HIDDEN_PLACE" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="(item.Nav_Type||{}).NAME" border="none" inputAlign="right"></u--input>
								</u-form-item>
								<u-form-item label="风险等级" prop="HIDDEN_PLACE" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="item.EVALUATE_LEVELName" border="none" inputAlign="right"></u--input>
								</u-form-item>

								<u-form-item label="管控层级" prop="HIDDEN_PLACE" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="item.CONTROLLEVEL" border="none" inputAlign="right"></u--input>
								</u-form-item>
								<u-form-item label="管控部门" prop="HIDDEN_PLACE" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="item.CONTROLDEPT" border="none" inputAlign="right"></u--input>
								</u-form-item>
								<u-form-item label="管控责任人" prop="HIDDEN_PLACE" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="item.CONTROLUSER" border="none" inputAlign="right"></u--input>
								</u-form-item>
								<u-form-item label="管控措施" prop="HIDDEN_PLACE" borderBottom>

								</u-form-item>
								<u--textarea fontSize="14px" disabled disabledColor="#fff" v-model="item.MEASURE" border="none" inputAlign="right" autoHeight></u--textarea>
								<u-form-item label="应急处置" prop="HIDDEN_PLACE" borderBottom>
								</u-form-item>
								<u--textarea fontSize="14px" disabled disabledColor="#fff" v-model="item.EMERGENCY" border="none" inputAlign="right" autoHeight></u--textarea>
								<u-form-item label="检查内容" prop="HIDDEN_PLACE" borderBottom>

								</u-form-item>
								<u--textarea fontSize="14px" disabled disabledColor="#fff" v-model="item.CHECKCONTENTS" border="none" inputAlign="right" autoHeight></u--textarea>
								<u-form-item label="隐患描述" prop="HIDDEN_PLACE" borderBottom>
								</u-form-item>
								<u--textarea fontSize="14px" disabled disabledColor="#fff" v-model="item.HIDDENSCRIPTION" border="none" inputAlign="right" autoHeight></u--textarea>
								<u-form-item label="整改措施" prop="HIDDEN_PLACE" borderBottom>
								</u-form-item>
								<u--textarea fontSize="14px" disabled disabledColor="#fff" v-model="item.RECIFYMEASURE" border="none" inputAlign="right" autoHeight></u--textarea>
								<u-form-item label="检查层级" prop="HIDDEN_PLACE" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="item.CHECKLEVEL" border="none" inputAlign="right"></u--input>
								</u-form-item>

								<u-form-item label="检查类型" prop="HIDDEN_PLACE" borderBottom>
									<u--input fontSize="14px" disabled disabledColor="#fff" v-model="item.CHECKTYPE" border="none" inputAlign="right"></u--input>
								</u-form-item>
							</view>


						</uni-collapse-item>
					</view>
				</uni-collapse>
				<view v-if="loading" class="loading-tip">加载中...</view>
				<view v-else class="loading-tip">已加载全部数据</view>



			</u--form>

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

<script>
	import {
		initFilter,
		extendRule,
		extendInclude
	} from '../../../../../utils/common'
	import {
		MineTypeIndex,
		MineTypeName
	} from '../../../../../utils/enums.js'
	import {
		SKOrderEntities
	} from '../../../../../services/apply/subPages/SK/SKServices.js'
	import {
		getEnum
	} from '../../../../../services/common';
	export default {
		data() {
			return {
				pageIndex: 0,
				pageSize: 20,
				total: 0,
				loading: true,
				model: {
					filterDatas: []
				},
				showData: [],
				enumsData: {}, // 存储所有枚举数据（键：枚举名称，值：处理后的枚举列表）
				enumsText: {} // 存储枚举文本列表（用于下拉选择等场景）
			}
		},
		onLoad() {
			this.fetchEnums(['SKProductionUnit', 'SKEvaluateLevelEnum']);
		},

		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 orgId = uni.getStorageSync('orgId')
				let json = initFilter(orgId);
				SKOrderEntities(json).then(res => {
					this.total = res.TotalCount
					this.model.filterDatas = res.Data
					this.model.filterDatas.map(item => {
						item.MINE_NAME = 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 : '';
					})
					this.reload()
				})
			},
			reload() {

				if (this.showData.length >= this.model.filterDatas.length) {
					this.loading = false
				} else {
					this.loading = true
					const endIndex = this.pageIndex + this.pageSize;
					this.showData = this.model.filterDatas.slice(0, endIndex);
					this.pageIndex = endIndex;
				}

			}
		},
		onReachBottom() {
			this.reload()
		},
	}
</script>


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

	.loading-tip {
		text-align: center;
		padding: 15px 0;
		color: #999;
		font-size: 14px;
	}
</style>