<template>
	<view >
		<view>
			<view style="display: flex;justify-content: center;margin: 30rpx;font-size:30rpx ;align-items: center;">
				<u-icon name="arrow-left-double" style="font-size: 40rpx;margin-right: 30rpx;" @click="previous"></u-icon>
				<div @click="show = true">
					{{"第"+weekNum+'周('+weeList[0]+"至"+weeList[weeList.length-1]+')' }}
				</div>
				<u-icon name="arrow-right-double" style="font-size: 40rpx;margin-left: 30rpx;" @click="next"></u-icon>
			</view>
			<u-cell-group>
				<u-cell icon="setting-fill" :title="v.fullName" size="large" @click="kaoqin(v)" v-for="v in list" :key="v.userId">
					<image slot="icon" src="/static/wjw-user.png" mode="widthFix" style="width: 60rpx;margin-right: 20rpx;"></image>
					<view slot="value" style="font-weight: 500;" :style="{color:v.isSave=='1'?'#19be6b':'#fa3534'}">
						{{v.isSave=='1'?'已打卡':'未打卡'}}
					</view>
				</u-cell>
			</u-cell-group>
		</view>
		 <u-datetime-picker
		                :show="show"
		                v-model="today"
		                mode="date"
						@confirm="confirm"
						@cancel="show=false"
		        ></u-datetime-picker>
	</view>
</template>

<script>
	import {getWeekInYear,getNowWeekList} from "@/utils/wjw.js"
	export default {
		data() {
			return {
				list:[],//人员列表
				nowWeekNum:null,//当前周数
				//当前周数
				weekNum:null,
				weeList:[],//周日期列表
				nowWeeList:[],//当前周日期列表
				xiugaiDay:null,//可修改日期
				today:null,//当前日期
				show:false,//日历展示
				xgType:true,//是否能修改
				day:null,//当前日期
				mounth:null,//当前月
				year:null,//当前年
			}
		},
		onLoad(){
			this.today = new Date().getTime()
			this.nowWeekNum = getWeekInYear(uni.$u.timeFormat(new Date().toLocaleDateString(), 'yyyy-mm-dd'))
			this.weekNum = getWeekInYear(uni.$u.timeFormat(new Date().toLocaleDateString(), 'yyyy-mm-dd'))
			this.weeList = getNowWeekList(uni.$u.timeFormat(new Date().toLocaleDateString(), 'yyyy-mm-dd'))
			this.nowWeeList = getNowWeekList(uni.$u.timeFormat(new Date().toLocaleDateString(), 'yyyy-mm-dd'))
			this.getPeople()
			this.getModifyDay()
		},
		onShow(){
			uni.$once('query', (query) => {
				if(query.a === 1){
					this.getPeople()
				}
			});
		},
		onPullDownRefresh(){
			this.getPeople()
		},
		methods: {
			//跳转考勤详情页
			kaoqin(v){
				uni.navigateTo({
					url:`/pages/kqdetail/kqdetail?name=${v.fullName}&id=${v.userId}&weekNumber=${this.weekNum}&deptId=${v.deptId}&type=${this.xgType}`
				})
			},
			//查询考勤是否还能修改
			getModifyDay(){
				uni.$u.http.get('/system/config/configKey/attendanceEditDay').then(res=>{
					this.xiugaiDay = Number(res.msg)
				})
			},
			//点击上一周
			previous(){
				uni.showLoading({
					title:"加载中...",
					mask:true
				})
				this.today = this.today - 7*86400000
				this.weekNum = getWeekInYear(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.weeList = getNowWeekList(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.isXiugai(this.weekNum,this.weeList)
				this.getPeople()
			},
			//点击下一周
			next(){
				this.today = this.today + 7*86400000
				this.weekNum = getWeekInYear(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.weeList = getNowWeekList(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.isXiugai(this.weekNum,this.weeList)
				this.getPeople()
			},
			//日历确认
			confirm(e){
				this.today = e.value
				this.weekNum = getWeekInYear(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.weeList = getNowWeekList(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.show = false
				this.isXiugai(this.weekNum,this.weeList)
				this.getPeople()
			},
			//获取列表人员
			getPeople(){
				uni.$u.http.get(`/oa/attendance/dept/all/app/${this.weekNum}`).then(res=>{
					this.list = res.rows
					uni.hideLoading()
					setTimeout(()=>{
						uni.stopPullDownRefresh()
					},2000)
				})
			},
			//判断能否修改
			isXiugai(zs,list){
				if(zs == this.nowWeekNum){
					if(list[0] == this.nowWeeList[0]){
						this.xgType = true
					}else{
						this.xgType = false
					}
				}else{
					let day = uni.$u.timeFormat(new Date().toLocaleDateString(), 'd')
					if(this.xiugaiDay<day){
						let mounth = new Date(uni.$u.timeFormat(list[6],'yyyy-mm')).getTime()
						let dqmounth = new Date(uni.$u.timeFormat(this.nowWeeList[0],'yyyy-mm')).getTime()
						if(dqmounth == mounth){
							this.xgType = true
						}else{
							this.xgType = false
						}
					}else{
						if(list[0]>this.nowWeeList[6]){
							this.xgType = false
						}else{
							let mounth = new Date(uni.$u.timeFormat(list[6],'yyyy-mm')).getTime()
							let dqmounth = new Date(uni.$u.timeFormat(new Date(),'yyyy-mm')).getTime()
							if((dqmounth-mounth)/86400000 <= 31){
								this.xgType = true
							}else{
								this.xgType = false
							}
						}
					}
				}
				console.log(this.xgType)
			}
		}
	}
</script>

<style>

</style>
