<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="showDetail(item.ID)">
					<view class="content">
						<view class="field">
							<text class="label">编号:</text>
							<text class="value">{{item.CODE}}</text>
						</view>
						<view class="field">
							<text class="label">状态:</text>
							<text class="status">{{item.DEALSITUATION_SHOW}}</text>
						</view>
						<view class="field">
							<text class="label">整改截止时间:</text>
							<text class="value">{{item.LastDateUser}}</text>
						</view>
						<view class="field">
							<text class="label">验收时间:</text>
							<text class="value">{{item.CHCECKTIME}}</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 {
		initFilter,
		extendRule,
		extendInclude
	} from '../../../../utils/common'

	import {
		ContentDetailOrderPaged
	} from '../../../../services/apply/subPages/BS/riskSubmit'

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

				extendInclude(json, "Nav_SubmitContent")
				if (this.keyword) {
					extendRule(json, 'NAME', 9, this.keyword);
				}

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

				ContentDetailOrderPaged(json).then(res => {
					this.total = res.TotalCount
					const newRes = (res.Data || []).map(i => {
						return {
							...i,
							title: i.NAME,
							LastDateUser: i.Nav_SubmitContent.LastDateUser != null ? i.Nav_SubmitContent
								.LastDateUser.split(' ')[0] : ''
						}
					})
					if (paginate) {
						this.lists = this.lists.concat(newRes)
					} else {
						this.lists = newRes
					}
				})
			},

			showDetail(id) {
				uni.navigateTo({
					url: 'riskSumbitDealAudit?ID=' + id
				})
			},

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

					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");
</style>