<template>
	<div>
		<common-merchant ref="common" @asynCallBack="asynCallBack"></common-merchant>
		<top-header :pageTitle="pageTitle"></top-header>

		<div class="mui-content vongi-kqtj-center vongi-dktz-c">
			<div class="fyy-temper-date margin10">
				<a class="mui-col-xs-12">
					<span @click="selectStartTime">{{listForm.startTime}}</span>
					至
					<span @click="selectEndTime">{{listForm.endTime}}</span>
					<span class="mui-icon mui-icon-arrowdown"></span>
				</a>
				<!-- <a href="" class="mui-col-xs-4">登记人<span class="mui-icon mui-icon-arrowdown"></span></a> -->
			</div>
			<div class="vongi-clasadmin vongi-bagfff">
				<div class="vongi-xzdw-search">
					<input type="search" @keyup.enter="reloadList" v-model="listForm.carNumber" placeholder="请输入车牌号查询">
					<span class="mui-icon mui-icon-search"></span>
				</div>
			</div>
			<div class="vongi-archives">
				<ul class="mui-table-view">
					<li v-for="(item,index) in recordList" class="mui-table-view-cell">
						<router-link class="mui-navigate-right" :to="{name:'MerchantHistoryInfo',query:{id:item.id}}">
							{{item.carNum}}
							<span class="mui-pull-right mui-h6" v-text="item.createTime"></span>
						</router-link>
					</li>
				</ul>
			</div>

			<NullList :remark="'暂无历史记录'" v-if="!recordList.length"></NullList>

		</div>

		<loading :visible="isLoading"></loading>
	</div>
</template>

<script>
	require('$project/assets/js/mui.picker.min.js');
	import * as API_Merchant from '@/apis/merchant'
	import CommonMerchant from '@/components/CommonMerchant.vue'
	import Loading from '$project/components/Loading.vue'
	import TopHeader from '$project/components/TopHeader.vue'
	import isReachBottom from '$project/utils/isReachBottom'
	import NullList from '$project/components/NullList.vue'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import {
		currentTimeStamp,
		parseUnixTime,
		afterTimeStamp
	} from '$project/utils'
	export default {
		name: 'MerchantHistoryList',
		components: {
			CommonMerchant,
			Loading,
			TopHeader,
			NullList
		},
		data() {
			return {
				isLoading: false,

				pageTitle: '历史记录',

				listForm: {
					pageIndex: 1,
					pageSize: 20,
					totalPage: 1,
					startTime: '',
					endTime: '',
					carNumber: ''
				},
				recordList: [],
			}
		},
		created() {
			this.listForm.startTime = parseUnixTime(currentTimeStamp(), '{y}-{m}-{d} 00:00');
			this.listForm.endTime = parseUnixTime(afterTimeStamp(1, this.listForm.startTime), '{y}-{m}-{d} 00:00');
		},
		methods: {
			//获取列表
			getList() {
				this.isLoading = true;
				API_Merchant.discountTicketList(this.listForm).then(response => {
					if (response) {
						if (this.listForm.pageIndex == 1) {
							this.recordList = response.data;
							this.listForm.pageIndex = response.pageNumber;
							this.listForm.totalPage = response.totalPage;
						} else {
							this.recordList = [
								...this.recordList,
								...response.data
							];
						}
					}
					this.listForm.pageIndex++;
					this.isLoading = false;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//下拉事件
			handleScrool() {
				if (isReachBottom()) {
					console.log('到达底部')
					if (this.listForm.pageIndex <= this.listForm.totalPage && this.isLoading == false) {
						this.getList();
					} else {
						return;
					}
				}
			},
			//重载列表
			reloadList() {
				this.listForm.pageIndex = 1;
				this.getList();
			},
			//选择开始时间
			selectStartTime() {
				var _this = this;
				var picker = new mui.DtPicker({
					"type": "datetime",
					"beginYear": 2020,
					"endDate": new Date(),
					"value": _this.listForm.startTime
				});
				picker.show(function(rs) {
					_this.listForm.startTime = rs.text;
					picker.dispose();
					_this.reloadList();
				});
			},
			//选择结束时间
			selectEndTime() {
				var _this = this;
				var picker = new mui.DtPicker({
					"type": "datetime",
					"beginDate": new Date(_this.listForm.startTime),
					"value": _this.listForm.endTime
				});
				picker.show(function(rs) {
					_this.listForm.endTime = rs.text;
					picker.dispose();
					_this.reloadList();
				});
			},
			asynCallBack() {

			},
		},
		mounted() {
			this.getList();
			//监控下拉加载事件
			var _this = this;
			window.addEventListener('scroll', _this.handleScrool);
		},
		destroyed() {
			//销毁监听事件
			var _this = this;
			window.removeEventListener('scroll', _this.handleScrool);
		},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
			})
		},
		//keepalive监控判断
		beforeRouteLeave(to, from, next) {
			console.log(to.name);
			if (['MerchantHistoryInfo'].indexOf(to.name) > -1) {
				this.$store.commit('SET_KEEP_ALIVE_COMPONENTS', ['MerchantHistoryList'])
			} else {
				this.$store.commit('SET_KEEP_ALIVE_COMPONENTS', [])
			}
			next()
		},
	}
</script>

<style scoped src="$project/assets/css/xpwyfyy.css"></style>
<style src="$project/assets/css/iconfont.css"></style>
<style src="$project/assets/css/mui.picker.min.css"></style>
<style>
</style>
