<template>
  <view class="page">
    <u-navbar class="u-navbar" title="考勤打卡" placeholder @leftClick="leftClick" leftIconColor="#fff"  bgColor="#00337A"  :titleStyle="{ color: '#FFF', fontSize: '32rpx' }"/>
	<scroll-view class="content" scroll-y="true">
		<view class="view-box">
			<view class="view-item">{{userName}}</view>
			<view class="view-item">{{proName}}</view>
			<view class="view-item">
				<view>{{timeStr}}</view> 
			</view>
			<view class="view-item" v-if="timeList.length>=1">
					<view>上班：</view>
					<view>{{timeList[0].addTime}}</view>
			</view>
			<view class="view-item" v-if="timeList.length==2">
					<view>下班：</view>
					<view>{{timeList[1].addTime}}</view>
			</view>
		</view>
		<view class="view-box">
			<view class="title-view">
				上班打卡
			</view>
			<view class="img-box" @click="handleAttendance(1)">
				<view class="img-item upload-btn">
					<image class="img" src="../../../../static/realName/tianjia-img.png" mode=""></image>
				</view>
				 <view style="font-size: 28rpx;font-weight: bold;color: #757575;margin-top: 20rpx;" v-if="timeList.length==0">可以打卡</view>
			</view>
			<view class="title-view">
				下班打卡
			</view>
			<view class="img-box" @click="handleAttendance(2)">
				<view class="img-item upload-btn">
					<image class="img" src="../../../../static/realName/tianjia-img.png" mode=""></image>
				</view>
				<view style="font-size: 28rpx;font-weight: bold;color: #757575;margin-top: 20rpx;" v-if="timeList.length==0||timeList.length==1">可以打卡</view>
			</view>
		</view>
 
	
		 
	
	</scroll-view>
  </view>
</template>

<script>
import { pathToBase64, base64ToPath } from 'image-tools';
import config from '@/config'
export default {
	data() {
		return {
			userId:uni.getStorageSync('realNameUser').userId,
			userName:uni.getStorageSync('realNameUser').userName,
			idNumber:uni.getStorageSync('realNameUser').idNumber,
			proId:uni.getStorageSync('realNameUser').proId,
			subId:uni.getStorageSync('realNameUser').subId,
			teamId:uni.getStorageSync('realNameUser').teamId,
			timeList:[],
			userData:{},
			proName:"", 
			longitude:"",
			latitude:"",
			timeStr:"",
			// isToWork:"",
			// isOffWork:"",
			type:1,//1上班，2下班
			faceData:{},
			photoPath:'',
		}
	},
	onLoad(option) {
		this.getLocation();//获取定位
		this.Time();//定时器
		this.getPro()
		this.getWork()
		this.getSelectManageAttendance()
	},
	onShow() {
		
	},
	methods: {
		// 获取工程名称
		getPro(){
			let param={
				id:this.proId,
				subId:-1
			}
			uni.request({
			  url: config.realAppUrl + '/offLine/getPro',
			  method: 'post',
			  data: param,
			  header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				Authorization: uni.getStorageSync('realNameToken')
			  },
			  success: res => {
				res = res.data;
				if(res.code==200){
					console.log(res)
					this.proName = res.data[0].abbreviation||"";
				}
			  },
			  fail: err => {
				console.log(err)
			  }
			})
		},
		//获取人员信息
		getWork(){
			let param={
				idNumber:this.idNumber
			}
			uni.request({
			  url: config.realAppUrl + '/BasePerson/downloadWorkPerson',
			  method: 'post',
			  data: param,
			  header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				Authorization: uni.getStorageSync('realNameToken')
			  },
			  success: res => {
				res = res.data;
				if(res.code==200){
					console.log(res)
					this.userData=res.data[0] 
				}
			  },
			  fail: err => {
				console.log(err)
			  }
			})
		},
		//获取考勤信息
		getSelectManageAttendance(){
			let param={
				userId:this.userId,
				currentDay: this.timeFormat(null,'yyyy-mm-dd')
			}
			console.log(param)
			uni.request({
			  url: config.realAppUrl + '/BasePerson/selectManageAttendance',
			  method: 'post',
			  data: param,
			  header: {
				'Content-Type': 'application/json',
				Authorization: uni.getStorageSync('realNameToken')
			  },
			  success: res => {
			    console.log(res)
				res = res.data;
				if(res.code==500){
					this.timeList=[];
				}
				if(res.code==200){
					this.timeList=res.data;
				}
			  },
			  fail: err => {
				console.log(err)
			  }
			})
		},
		// 定时器
		Time() {
			setTimeout(()=>{
				this.getLocation();//获取定位
			},1000)
			this.timme = setInterval(() => {
				this.timeStr=this.timeFormat(null,'yyyy-mm-dd hh:MM:ss')
			}, 1000);
		},
		//获取定位
		getLocation(){
			uni.getLocation({
				type: "wgs84",  //默认为 wgs84 返回 gps 坐标
				isHighAccuracy: true, // 开启高精度定位
				success: e => {
					this.longitude=e.longitude;
					this.latitude=e.latitude;
				},fail:e=>{
				}
			});  
		},
		//查看列表
		goList(){
			uni.navigateTo({ 
				url: `/pages/realName/workAttendance/tempClock/tempPeopleList` 
			})
		},
		goOverList(){
			uni.navigateTo({ 
				url: `/pages/realName/workAttendance/tempClock/tempPeopleList?isOver=1` 
			})
		},
		//计算距离
		getDistance(lat1, lon1, lat2, lon2) {
		  const R = 6371; // 地球半径，单位为千米
		  const toRadians = (degree) => degree * Math.PI / 180; // 角度转弧度
		  const deltaLat = toRadians(lat2 - lat1); // 纬度差
		  const deltaLon = toRadians(lon2 - lon1); // 经度差
		  
		  const a = Math.sin(deltaLat / 2) ** 2 + 
		            Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
		            Math.sin(deltaLon / 2) ** 2;
		  const c = 2 * Math.asin(Math.sqrt(a));
		  const distance = R * c; // 距离，单位为千米
		  
		  return distance.toFixed(2);
		},
		//打卡
		handleAttendance(type){
			this.type=type;
			console.log(this.userData)
			this.getLocation()//重新获取定位 
			if(this.type==1&&this.timeList.length==1){
				uni.$u.toast('已打过上班卡！');
			}else if(this.type==2&&this.timeList.length==2){
				uni.$u.toast('已打过下班卡！');
			}else if(this.userData.workerType==1||this.userData.workerType==0){
				uni.$u.toast('只有管理人员可在当前页面打卡！');
			}else{
				this.uploadAttendance()
			}
			// if(this.type==2&&this.userData.isToWork==0){
			// 	uni.$u.toast('还未打上班卡！');
			// }else if(this.type==1&&this.userData.isToWork!=0){
			// 	uni.$u.toast('已打过上班卡！');
			// }else if(this.type==2&&this.userData.isOffWork!=0){
			// 	uni.$u.toast('已打过下班卡！');
			// }else if(this.userData.lightStatus==0&&this.userData.attDayNum>7){
			// 	uni.$u.toast('红灯人员只能打7天卡！');
			// }else if(this.userData.isFurloughPerson==1){
			// 	uni.$u.toast('暂退人员无法打卡！');
			// }else if(this.userData.workerType==1||this.userData.workerType==0){
			// 	uni.$u.toast('只有管理人员可在当前页面打卡！');
			// }else{
				// this.uploadAttendance()//打开人脸识别
			// }
		},
		//人脸识别采集
		distinguish(){
			console.log('人脸识别采集')
			uni.chooseImage({
				count: 1,
				sizeType: ['compressed'],
				sourceType: ['camera'],
				success: res => {
					console.log('?? ~ res-拍照:', res)
					this.imgToBase64(res.tempFilePaths[0]).then(base64 => {
						// console.log(base64)
						//上传考勤图片
						uni.uploadFile({
							url:  config.realFileUrl+`file/upload`, //服务器地址
							fileType:"image",//ZFB必填,不然报错
							filePath: res.tempFilePaths[0],//这个就是我们上面拍照返回或者先中照片返回的数组
							name: "imgFile",
							formData: {
								photoType:'attendance',
							},
							success: (uploadFileRes) => {
								console.log(uploadFileRes)
								if(uploadFileRes.statusCode==200){
									this.photoPath=JSON.parse(uploadFileRes.data).data.url;
								}else{
									uni.$u.toast('上传失败');
								} 
							},
							fail: err => {
								uni.$u.toast('上传失败');
								console.log(err)
							}
						});
						//人脸验证
						uni.uploadFile({
							url: config.realFileUrl+'file/getFaceRecognition', //服务器地址
							fileType:"image",//ZFB必填,不然报错
							filePath: res.tempFilePaths[0],//这个就是我们上面拍照返回或者先中照片返回的数组
							formData: {
								file:base64,
								photoType:'face',
							},
							success: (uploadFileRes) => {
								console.log(uploadFileRes)
								if(uploadFileRes.statusCode==200){
									uploadFileRes=JSON.parse(uploadFileRes.data)
									console.log(uploadFileRes)
									if(uploadFileRes.code==200){
										if(uploadFileRes.data.face){
											this.faceData=JSON.parse(uploadFileRes.data.personData)
											console.log(this.faceData)
											if(this.faceData){
												// if(this.faceData.idNumber==this.idNumber){//人脸身份证号与登录身份证号相同
													this.idNumber=this.faceData.idNumber;
													// this.idNumber=this.faceData.idNumber;
													this.userName=this.faceData.name;
													this.proId=this.faceData.proId;
													this.uploadAttendance()
												// }else{
												// 	uni.$u.toast('不是本人！');
												// }
											}else{
												uni.$u.toast('识别失败');
											}
										}else{
											uni.$u.toast(uploadFileRes.msg);
										}
									}else{
										uni.$u.toast(uploadFileRes.msg);
									}
								}else{
									uni.$u.toast('识别失败');
								}
							},
							fail: err => {
								uni.$u.toast('识别失败');
								console.log(err)
							}
						});
					})
				},
				fail: err => { 
				  console.log('?? ~ err:', err)
				}
			})
		},
		//打卡接口
		uploadAttendance(){ 
			let param={
				// "name": this.userName,
				// "idNumber": this.idNumber,
				// "proId": this.proId,
				"imei": this.idNumber,
				"userId": this.userId,
				"currentDay": this.timeFormat(null,'yyyy-mm-dd'),
				"addTime": this.timeFormat(null,'yyyy-mm-dd hh:MM:ss'),
				// "photoPath": this.photoPath,
				"attendanceType": this.type,
				"lon": this.longitude,
				"lat": this.latitude,
			}
			console.log(param)
			uni.request({
			  url: config.realAppUrl + '/BasePerson/uploadManageAttendance',
			  method: 'post',
			  data: param,
			  header: {
				'Content-Type': 'application/json',
				Authorization: uni.getStorageSync('realNameToken')
			  },
			  success: res => {
			    console.log(res) 
				res = res.data;
				if(res.code==200){
					uni.$u.toast('打卡成功！');
					this.getSelectManageAttendance()
				}else{
					uni.$u.toast('打卡失败！');
				}
			  },
			  fail: err => {
			    uni.$u.toast('打卡失败！');
				console.log(err)
			  }
			})
		},
		goDayPlan(){//准入信息查询
			uni.navigateTo({
				url: `/pages/realName/workbench/dayPlan/index`
			})
		},
		leftClick() {
			console.log('返回')
			uni.navigateBack({
				delta: 1 // 返回
			});
		},
		imgToBase64(data) {
		  return new Promise((resolve, reject) => {
			pathToBase64(data)
			  .then(base64 => {
				resolve(base64)
			  })
			  .catch(error => {
				console.error(error)
				reject(error)
			  })
		  })
		},
		timeFormat(dateTime = null, formatStr = 'yyyy-mm-dd') {
			let date
			// 若传入时间为假值，则取当前时间
			if (!dateTime) {
				date = new Date()
			}
			// 若为unix秒时间戳，则转为毫秒时间戳（逻辑有点奇怪，但不敢改，以保证历史兼容）
			else if (/^\d{10}$/.test(dateTime?.toString().trim())) {
				date = new Date(dateTime * 1000)
			}
			// 若用户传入字符串格式时间戳，new Date无法解析，需做兼容
			else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) {
				date = new Date(Number(dateTime))
			}
			// 处理平台性差异，在Safari/Webkit中，new Date仅支持/作为分割符的字符串时间
			// 处理 '2022-07-10 01:02:03'，跳过 '2022-07-10T01:02:03'
			else if (typeof dateTime === 'string' && dateTime.includes('-') && !dateTime.includes('T')) {
				date = new Date(dateTime.replace(/-/g, '/'))
			}
			// 其他都认为符合 RFC 2822 规范
			else {
				date = new Date(dateTime)
			}
		
			const timeSource = {
				'y': date.getFullYear().toString(), // 年
				'm': (date.getMonth() + 1).toString().padStart(2, '0'), // 月
				'd': date.getDate().toString().padStart(2, '0'), // 日
				'h': date.getHours().toString().padStart(2, '0'), // 时
				'M': date.getMinutes().toString().padStart(2, '0'), // 分
				's': date.getSeconds().toString().padStart(2, '0') // 秒
				// 有其他格式化字符需求可以继续添加，必须转化成字符串
			}
		
			for (const key in timeSource) {
				const [ret] = new RegExp(`${key}+`).exec(formatStr) || []
				if (ret) {
					// 年可能只需展示两位
					const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0
					formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex))
				}
			}
			return formatStr
		},
 
	},
	destroyed() {
		clearInterval(this.timme);
	}
}
</script>

<style lang="scss">
.page {
	width: 100vw;
	height: 100vh;
	background-color: #EFEFEF;
	box-sizing: border-box;
	// padding: 0 20px;
	
	.content{
		width: 100%;
		height: 90vh;
		margin-top: 20rpx;
		padding-bottom: 80rpx;
		background-color: #EFEFEF;
	}
	.view-box{
		width: 94%;
		margin: 20rpx auto;
		background-color: #FFF;
		border-radius: 10rpx;
		padding-top: 20rpx;
		.title-view{
			font-weight: 600;
			color: #757575;
			margin-left: 20rpx;
		}
	}
	.view-item{
		width: 94%;
		margin: 0rpx auto;
		padding:20rpx;
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid #EFEFEF;
		font-size: 28rpx;
		font-weight: bold;
		color: #757575;
	}
	.button-box{
		padding: 15rpx 30rpx;
		border: 1rpx solid #00337A;
		border-radius: 10rpx;
		color: #00337A;
		margin-left: 20rpx;
	}
	.img-box{
		width: 94%;
		height: auto;
		margin: 0rpx auto;
		padding:20rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		border-bottom: 1rpx solid #EFEFEF;
		.img-item {
			width: 200upx;
			height: 200upx;
			border: 1px solid #ddd;
			position: relative;
			box-sizing: border-box;
			background: #eee;
			.img {
				display: block;
				width: 100%;
				height: 100%;
			}
		}
		.upload-btn {
				display: flex;
				justify-content: center;
				align-items: center;
			.img {
				width: 60upx;
				height: 60upx;
				margin: unset;
			}
		}
	}
}
</style>
