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

		<div class="mui-content">
			<div class="mui-content-padded vongi-work">
				<!-- <h5>2020-09-28</h5> -->
				<ul class="mui-table-view">
					<li v-for="(item,index) in recordList" class="mui-table-view-cell">
						{{item.recordTime}}
						<div class="mui-media-body">
							<button type="button" class="mui-btn mui-btn-primary" v-if="item.result=='1' && item.status=='1'" @click="goToInfo(item.supplementWorkId)">{{item.type=='1'?'已外勤打卡':'已补卡'}}</button>
							<button type="button" class="mui-btn mui-btn-primary" v-if="item.result=='0' && item.status=='3'" @click="goToInfo(item.supplementWorkId)">已拒绝</button>
							<button type="button" class="mui-btn mui-btn-primary" v-if="item.result=='0' && item.status=='0'" @click="goToInfo(item.supplementWorkId)">审核中</button>
							<button type="button" class="mui-btn mui-btn-primary" v-if="item.result=='0' && item.status===null" @click="applybk(item.id,item.recordTime)">申请补卡</button>
							<button type="button" class="mui-btn mui-btn-primary" v-if="item.result=='0' && item.status==='6'" @click="applybk(item.id,item.recordTime)">申请补卡</button>
							
							<span :style="'color:'+statusColor[item.result]" v-text="status[item.result]">缺卡</span>
						</div>
					</li>
				</ul>
			</div>
		</div>
		<NullList :remark="'暂无考勤记录'" v-if="!recordList.length"></NullList>
		<!--弹窗-->

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

<script>
	import * as API_Attendance from '@/apis/Master/attendance'
	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 NullList from '$project/components/NullList.vue'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	export default {
		name: 'MasterAttendanceList',
		components: {
			Common,
			Loading,
			TopHeader,
			NullList
		},
		data() {
			return {
				//pageTitle: '考勤记录',

				isLoading: false,

				listForm: {
					pageIndex: 1,
					pageSize: 20,
					totalPage: 1,
					result: 0,
				},
				recordList: [],
				status: ['缺卡', '正常', '迟到', '早退', '请假','出差'],
				statusColor: ['#fe616c', '#05c8af', '#f6f448', '#f6f448', '#f6f448', '#f6f448'],

				rightLink: {
					show: true,
					icon: 'icon-shijian',
					style: 'font-size:14px;color:#000;',
					title: '缺卡记录'
				},
			}
		},
		created() {
			this.listForm.openId = this.openId;
			this.listForm.result = this.$route.query.result != null ? this.$route.query.result : 0;
		},
		methods: {
			//申请补卡
			applybk(id,time) {
				this.$router.push({
					name: 'MasterAttendanceApply',
					query: {
						id: id,
						time:time
					}
				})
			},
			//跳转申请补卡详情
			goToInfo(id) {
				this.$router.push({
					name: 'XsyApprovalInfo',
					query: {
						id: id,
						formId:'applyWork'
					}
				})
			},
			//获取列表
			getList() {
				this.isLoading = true;
				API_Attendance.pageList(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.result = this.listForm.result == 1 ? 0 : 1;
				this.getList();
			},
			asynCallBack() {

			},
		},
		mounted() {
			this.getList();
			//监控下拉加载事件
			var _this = this;
			window.addEventListener('scroll', _this.handleScrool);
		},
		destroyed() {
			//销毁监听事件
			var _this = this;
			window.removeEventListener('scroll', _this.handleScrool);
		},
		computed: {
			pageTitle: {
				// getter
				get: function() {
					if (this.listForm.result) {
						return '缺卡记录';
					} else {
						return '考勤记录';
					}
				},
				// setter
				set: function(newValue) {
					console.log(newValue)
				}
			},
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
			})
		},
		watch: {
			'listForm.result': function(newVal, oldVal) {
				if (newVal == 0) {
					this.rightLink.title = '缺卡记录';
				} else {
					this.rightLink.title = '考勤记录';
				}
			}
		},
		//keepalive监控判断
		beforeRouteLeave(to, from, next) {
			console.log(to.name);
			if (['MasterAttendanceInfo', 'MasterAttendanceApply'].indexOf(to.name) > -1) {
				this.$store.commit('SET_KEEP_ALIVE_COMPONENTS', ['MasterAttendanceList'])
			} 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 scoped>
</style>
