<template>
	<div>
		<common @asynCallBack="asynCallBack"></common>
		<top-header :pageTitle="pageTitle" :rightLink="rightLink" :doRightLink="doRightLink"></top-header>

		<div class="mui-content">
			<div class="fyy-temper-date">
				<a class="mui-col-xs-5" v-text="listForm.startTime" @click="selectStartTime"></a>
				<span class="mui-col-xs-2">至</span>
				<a class="mui-col-xs-5" v-text="listForm.endTime" @click="selectEndTime"></a>
			</div>
			<div class="mui-content-padded">
				<ul class="mui-table-view openfyy-list">
					<li v-for="(item,index) in recordList" @click="goToPage(item.id)" class="mui-table-view-cell mui-media flew-sp">
						<div class="mui-h4">
							<span v-text="item.recordTime"></span>
							<!-- <button>设备测温</button> -->
						</div>
						<span class="mui-navigate-right">
							<p class="color05c8af">
								{{item.fever?'异常':'正常'}}
								<!-- <span v-text="item.temperature"></span> -->
							</p>
						</span>
					</li>
				</ul>
			</div>
		</div>

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

<script>
	require('$project/assets/js/mui.picker.min.js');
	import * as API_Health from '@/apis/Common/health'
	import Common from '$project/components/Common.vue'
	import Loading from '$project/components/Loading.vue'
	import TopHeader from '$project/components/TopHeader.vue'
	import isReachBottom from '$project/utils/isReachBottom'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import {
		currentTimeStamp,
		parseUnixTime,
		unixTimeStamp
	} from '$project/utils'
	export default {
		name: 'CommonHealthTemperatureRecord',
		components: {
			Common,
			Loading,
			TopHeader
		},
		data() {
			return {
				pageTitle: '测温统计',

				isLoading: false,

				rightLink: {
					show: true,
					icon: 'icon-baojing',
					style: 'font-size:14px',
					title: '异常记录',
				},

				recordList: [],

				listForm: {
					pageIndex: 1,
					pageSize: 20,
					token: '',
					fever: '',
					totalPage: 1,
					startTime: '',
					endTime: '',
					personId: this.$route.query.personId
				},
			}
		},
		created() {
			if (this.$route.query.fever) {
				this.listForm.fever = this.$route.query.fever;
			}
			this.listForm.startTime = this.listForm.endTime = parseUnixTime(currentTimeStamp(), '{y}-{m}-{d}');
		},
		methods: {
			goToPage(id) {
				this.$router.push({
					name: 'CommonHealthTemperatureInfo',
					query: {
						id: id
					}
				})
			},
			//获取列表
			getList() {
				this.isLoading = true;
				API_Health.getRecordList(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;
					}
				}
			},
			//右上角点击事件
			doRightLink() {
				this.listForm.pageIndex = 1;
				this.listForm.fever = this.listForm.fever == 1 ? '' : 1;
				this.getList();
			},
			selectStartTime() {
				var _this = this;
				var picker = new mui.DtPicker({
					"type": "date",
					"beginYear": 2020,
					"endYear": 2040,
					"endDate": new Date(),
					"value": _this.listForm.startTime
				});
				picker.show(function(rs) {
					_this.listForm.startTime = rs.text;
					if (unixTimeStamp(_this.listForm.startTime) > unixTimeStamp(_this.listForm.endTime)) {
						_this.listForm.endTime = _this.listForm.startTime;
					}
					_this.listForm.pageIndex = 1;
					_this.getList();
					picker.dispose();
				});
			},
			selectEndTime() {
				var _this = this;
				var picker = new mui.DtPicker({
					"type": "date",
					"beginYear": 2020,
					"endYear": 2040,
					"beginDate": new Date(_this.listForm.startTime),
					"endDate": new Date(),
					"value": _this.listForm.endTime
				});
				picker.show(function(rs) {
					_this.listForm.endTime = rs.text;
					_this.listForm.pageIndex = 1;
					_this.getList();
					picker.dispose();
				});
			},
			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',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
			})
		},
		watch: {
			'listForm.fever': function(newValue, oldValue) {
				if (newValue == 1) {
					this.rightLink.title = '全部记录';
				} else {
					this.rightLink.title = '异常记录';
				}
			}
		},
		//keepalive监控判断
		beforeRouteLeave(to, from, next) {
			console.log(to.name);
			if (['CommonHealthTemperatureInfo'].indexOf(to.name) > -1) {
				this.$store.commit('SET_KEEP_ALIVE_COMPONENTS', ['CommonHealthTemperatureRecord'])
			} else {
				this.$store.commit('SET_KEEP_ALIVE_COMPONENTS', [])
			}
			next()
		},
	}
</script>

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