<template>
	<view class="risk-record-page">
		<u-sticky>
			<view class="filter-bar">
				<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="handleToEdit">
					<view class="content">
						<view class="field">
							<text class="label">检查任务：</text>
							<text class="value">{{item.Nav_Check ? item.Nav_Check.NAME : ''}}</text>
						</view>
						<view class="field">
							<text class="label">检查时间：</text>
							<text class="value">{{item.CHECKTIME}}</text>
						</view>
						<view class="field">
							<text class="label">上报人：</text>
							<text class="value">{{item.Nav_User ? item.Nav_User.NAME : ''}}</text>
						</view>
					</view>
				</common-card>
			</view>
		</view>
		<u-picker :show="comPickerInfo.showPicker" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			@cancel="closePicker"></u-picker>
	</view>
</template>

<script>
	import {
		getRiskReportData
	} from '../../../services/safe'
	import {
		extendInclude,
		extendRule,
		initFilter
	} from '../../../utils/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: [],

				keyword: '',
				searchStartTime: '',
				filterStatus: null,
				dateFilterTxt: '时间区间',
				statusFilterTxt: '检查层级',
				comPickerInfo: {
					showPicker: false,
					columns: [],
					title: '',
					name: ''
				}
			}
		},
		onLoad() {
			this.loadData()
		},
		methods: {
			handleSearch(obj) {
				this.keyword = obj.value
			},
			handleToEdit(obj) {
				//查看  BS006_VIEW
				uni.navigateTo({
					url: '/pages/apply/subPages/BS/riskSumbitView?ID=' + obj.ID
				})

				// if (obj.SUBMIT_TYPE == 10) {//自动隐患上报 BS006_VIEW  编辑 BS061
				// } else if (obj.SUBMIT_TYPE == 20) {//手动隐患上报 BS006_VIEW  编辑 BS060

				// 	uni.navigateTo({
				// 		url: '/pages/apply/subPages/riskReport?id=' + obj.ID
				// 	})

			},
			// handleCancelSearch() {
			// 	this.keyword = ''
			// },
			loadData() {
				this.pageIndex++
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "CHECKTIME", 1, this.pageIndex)
				extendInclude(json, "Nav_Check")
				extendInclude(json, "Nav_User")
				if (this.searchStartTime) {
					extendRule(json, 'CHECKTIME', 6, this.searchStartTime)
				}
				if (this.keyword) {
					extendRule(json, 'NAME', 9, this.keyword)
				}

				if (this.filterStatus !== null) {
					extendRule(json, 'Nav_CheckTypeLevel.Nav_Enums.NAME', 1, this.filterStatus)
				}

				getRiskReportData(json).then(res => {
					this.total = res.TotalCount
					const newRes = (res.Data || []).map(i => {
						return {
							...i,
							title: i.NAME,
						}
					})
					if (this.pageIndex !== 1) {
						this.lists = this.lists.concat(newRes)
					} else {
						this.lists = newRes
					}
				})
			},
			handleShowPicker(p) {
				let column = []
				if (p.name === 'dateRange') {
					column = ['全部', '当天', '最近三天', '最近一周', '最近一月']
				}
				if (p.name === 'status') {
					column = ['全部', '班组级', '车间级', '部门级', '公司级']
				}

				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') {
					// if (e.indexs[0] === 1) {
					// 	this.filterStatus = 10
					// } else if (e.indexs[0] === 2) {
					// 	this.filterStatus = 20
					// } else if (e.indexs[0] === 3) {
					// 	this.filterStatus = 30
					// } else if (e.indexs[0] === 4) {
					// 	this.filterStatus = 40
					// } else if (e.indexs[0] === 5) {
					// 	this.filterStatus = 50
					// }

					if (e.indexs[0] === 0) {
						// 全部
						this.filterStatus = null
						this.statusFilterTxt = '检查层级'
					} else {
						this.filterStatus = e.value[0]
						this.statusFilterTxt = e.value[0]
					}
				}
				this.closePicker()
			},
			closePicker() {
				this.comPickerInfo = {
					showPicker: false,
					columns: [],
					title: '',
					name: ''
				}
			},
		},
		onReachBottom() {
			// this.pageIndex++
			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()
			}
		}
		// ,
		// watch: {
		// 	pageIndex(n, o) {
		// 		this.handleGetRiskReportData()
		// 	},
		// 	keyword(n, o) {
		// 		this.pageIndex = 1
		// 		this.keyword = n
		// 		this.handleGetRiskReportData()
		// 	},
		// 	searchStartTime(n, o) {
		// 		this.pageIndex = 1
		// 		this.searchStartTime = n
		// 		this.handleGetRiskReportData()
		// 	},
		// }
	}
</script>

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