<template>
	<!-- 查询框 检查记录 列表 -->
	<view class="risk-record-page">

		<u-sticky>
			<view class="filter-bar" style="display: flex;flex-direction: column;">
				<!-- <view class="filter filter-date-range" @click="handleShowPicker({title: '时间区间', name: 'dateRange'})">
					<text>{{dateFilterTxt}}</text>
					<u-icon size="14" style="margin-left: 4px;" name="arrow-down"></u-icon>
				</view> -->
				<uni-search-bar class="search-bar" radius="100" v-model="keyword" @confirm="handleSearch" @cancel="handleCancelSearch" @clear="handleCancelSearch" placeholder="搜索风险描述" cancel-button="none">
					<uni-icons slot="searchIcon" color="#999999" size="16" type="search" class="search-slot" />
				</uni-search-bar>
				<!-- <view class="filter filter-status" @click="handleShowPicker({title: '状态', name: 'status'})">
					<text>{{statusFilterTxt}}</text>
					<u-icon size="14" style="margin-left: 4px;" name="arrow-down"></u-icon>
				</view> -->
			</view>
		</u-sticky>
		<view class="main">
			<view class="content-list">
				<common-card :dataSource="item" v-for="item in lists" @click="showDetail(item.ID,item.TaskID)">
					<view class="content">
						<!-- 	<view class="field">
							<text class="label">检查任务：</text>
							<text class="value">{{item.NAME}}</text>
						</view> -->
						<view class="field">
							<text class="left">生产单元：</text>
							<text class="value">{{item.MineType}}</text>
						</view>
						<view class="field">
							<text class="left">风险名称：</text>
							<text class="value">{{item.RISK_NAME}}</text>
						</view>
						<view class="field">
							<text class="left">风险描述：</text>
							<text class="value">{{item.RISK_DESCRIPTION}}</text>
						</view>
						<view class="field">
							<text class="left">风险类别：</text>
							<text class="value">{{item.Nav_Type?item.Nav_Type.NAME:''}}</text>
						</view>
						<view class="field">
							<text class="left">风险等级：</text>
							<text class="value">{{item.EVALUATE_LEVEL}}</text>
						</view>
						<view class="field">
							<text class="left">入库时间：</text>
							<text class="value">{{item.CREATE_TIME}}</text>
						</view>

					</view>
				</common-card>
			</view>
			<view style="padding: 10px 16px;display: flex;flex-direction: row;" class="bottom-button">
				<u-button type="primary" @click="goCheckList" color="#3d4b70" :plain="true" style="margin-right: 5px;">检查清单</u-button>
				<u-button type="primary" @click="goRiskList" color="#3d4b70">岗位风险清单</u-button>
				<u-button type="primary" @click="goSummary" color="#3d4b70" :plain="true" style="margin-left: 5px;">汇总</u-button>
			</view>
		</view>

		<u-picker :show="comPickerInfo.showPicker" :columns="comPickerInfo.columns" @confirm="onConfirmPicker" @cancel="closePicker"></u-picker>
	</view>
</template>

<script>
	import {
		initFilter,
		extendRule,
		extendInclude,
		extendOrder,
		transformData
	} from '../../../../utils/common'
	import {
		MineTypeIndex,
		MineTypeName
	} from '../../../../utils/enums.js'
	import {
		GetSKEnterpriseLibrary
	} from '../../../../services/apply/subPages/SK/SKServices.js'
	import {
		getEnum,
		GetEnumAnyOrder
	} from '../../../../services/common';
	import UniSearchBar from '../../../../uni_modules/uni-search-bar/components/uni-search-bar/uni-search-bar.vue'
	export default {
		components: {
			UniSearchBar
		},
		data() {
			return {
				pageIndex: 0,
				total: 0,
				lists: [],
				params: {},

				keyword: '',
				searchStartTime: '',
				filterStatus: null,
				dateFilterTxt: '时间区间',
				statusFilterTxt: '状态筛选',
				comPickerInfo: {
					showPicker: false,
					columns: [],
					title: '',
					name: ''
				},
				enumsData: {}, // 存储所有枚举数据（键：枚举名称，值：处理后的枚举列表）
				enumsText: {} // 存储枚举文本列表（用于下拉选择等场景）
			}
		},
		onLoad() {
			this.fetchEnums(['SKProductionUnit', 'SKEvaluateLevelEnum']);
		},

		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();
				})
			},
			handleSearch() {
				this.pageIndex = 0
				this.loadData(false)
			},
			loadData(paginate) {
				this.pageIndex++
				const orgId = uni.getStorageSync('orgId')
				let json = initFilter(orgId, "", "CREATE_TIME", 1, this.pageIndex);

				// extendRule(json, 'STATECHECK', 2, 0);

				extendInclude(json, 'Nav_Area')
				extendInclude(json, 'Nav_Type')
				extendInclude(json, 'Nav_SCOREL')
				extendInclude(json, 'Nav_SCOREE')
				extendInclude(json, 'Nav_SCOREC')
				json.SelectField = [
					"MineType",
					"Nav_Area.NAME",
					"RISK_NAME",
					"RISK_DESCRIPTION",
					"Nav_Type.NAME",
					"Nav_SCOREL.NAME",
					"Nav_SCOREE.NAME",
					"Nav_SCOREC.NAME",
					"EVALUATE_SCORE",
					"EVALUATE_LEVEL",
					"CREATE_TIME",
					"ID",
					"ORG_ID"
				]
				extendOrder(json, 'MineType', '0')
				extendOrder(json, 'AREA_ID', '0')
				json.Sort = 'MineType'
				// extendInclude(json, 'Nav_User')
				if (this.keyword) {
					extendRule(json, 'RISK_DESCRIPTION', 9, this.keyword)
				}

				if (this.searchStartTime && this.searchStartTime.length > 0) {
					extendRule(json, 'CHECKTIME', 6, this.searchStartTime)
				}
				if (this.filterStatus !== null) {
					extendRule(json, 'STATUS', 1, this.filterStatus)
				}

				// json.Orders.push({
				// 	Field: "CREATE_TIME",
				// 	Order: "1"
				// })
				// json.IgnoreDataRule = true
				GetSKEnterpriseLibrary(json).then(res => {
					this.total = res.TotalCount

					// for (let i = 0; i < res.length; i++) {
					// 	res[i].STATUS = this.enumsData['PFStandardStatus'].find(item => item.code === res[i].STATUS ).name;
					// }
					const newRes = (res.Data || []).map(i => {
						return {
							...i,
							title: i.Nav_Area ? i.Nav_Area.NAME : '',
							EVALUATE_LEVEL: i.EVALUATE_LEVEL !== undefined ? this.enumsData['SKEvaluateLevelEnum'].find(item => item.code === i.EVALUATE_LEVEL).name : '',
							MineType: i.MineType !== undefined ? this.enumsData['SKProductionUnit'].find(item => item.code === i.MineType).name : ''
						}
					})
					if (paginate) {
						this.lists = this.lists.concat(newRes)
					} else {
						this.lists = newRes
					}

				})
			},
			goRiskList() {
				uni.navigateTo({
					url: 'enterprise/jobList'
				})
			},
			goCheckList() {
				uni.navigateTo({
					url: 'enterprise/checkList'
				})
			},
			goSummary() {
				uni.navigateTo({
					url: 'enterprise/summary'
				})
			},
			showDetail(id, taskID) {
				uni.navigateTo({
					url: 'SK003Show?ID=' + id + '&taskID=' + taskID
				})
			},


			handleShowPicker(p) {
				let column = []
				if (p.name === 'dateRange') {
					column = ['全部', '当天', '最近三天', '最近一周', '最近一月']
				}
				if (p.name === 'status') {
					column = this.enumsText['PFStandardStatus']
				}

				this.comPickerInfo = {
					showPicker: true,
					title: p.title,
					name: p.name,
					columns: [column]
				}
			},
			onConfirmPicker(e) {
				if (this.comPickerInfo.name === 'dateRange') {
					let currentDate = new Date();
					if (e.indexs[0] === 2) {
						currentDate.setDate(currentDate.getDate() - 3);
					} else if (e.indexs[0] === 3) {
						currentDate.setDate(currentDate.getDate() - 7);
					} else if (e.indexs[0] === 4) {
						currentDate.setMonth(currentDate.getMonth() - 1);
					}
					if (e.indexs[0] === 0) {
						this.searchStartTime = ''
						this.dateFilterTxt = '时间区间'
					} else {
						this.dateFilterTxt = e.value[0]
						this.searchStartTime = uni.$u.timeFormat(currentDate, 'yyyy-mm-dd 00:00:00');
					}
				}
				if (this.comPickerInfo.name === 'status') {
					this.filterStatus = this.enumsData['PFStandardStatus'][e.indexs[0]].code
					this.statusFilterTxt = e.value[0]
					// if (e.indexs[0] === 0) {
					// 	// 全部
					// 	this.filterStatus = null
					// 	this.statusFilterTxt = '状态筛选'
					// } else {
					// 	this.statusFilterTxt = e.value[0]
					// }
				}
				this.closePicker()
			},
			closePicker() {
				this.comPickerInfo = {
					showPicker: false,
					columns: [],
					title: '',
					name: ''
				}
			},
		},
		computed: {

		},

		onReachBottom() {
			if (this.total > 10 * this.pageIndex)
				this.loadData(true)
		},
		watch: {
			keyword(n, o) {
				this.pageIndex = 0
				this.keyword = n
				this.loadData()
			},
			searchStartTime(n, o) {
				this.pageIndex = 0
				this.searchStartTime = n
				this.loadData()
			},
			filterStatus(n, o) {
				this.pageIndex = 0
				this.filterStatus = n
				this.loadData()
			}
		}
	}
</script>


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

	.status {
		background-color: rgba(5, 109, 232, 0.1);
		color: #056DE8;
		padding: 0px 5px 0px 5px;
		border-radius: 5px;
	}

	.bottom-button {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 10px 16px;
		box-sizing: border-box;
		background: #fff;
		z-index: 999;
	}
</style>