<template>
	<view class="container">
		<view class="tab">
			<view class="tab-item" @click="chanegTab(1)" :class="tabIdx===1?'active':''">
				<view class="text">打卡签到</view>
			</view>
			<view class="tab-item" @click="chanegTab(2)" :class="tabIdx===2?'active':''">
				<view class="text">打卡记录</view>
			</view>
		</view>
		<view class="tab-content">
			<view class="tab-wrap">
				<view class="signIn" v-if="tabIdx===1">
					<view class="info">
						<view class="date">日期：<text class="text">{{signInDate}}</text></view>
						<view class="location">地点：<text class="text">{{formData.dutyLocation}}</text></view>					
					</view>
					<view class="signIn-handle" @click="creatDuty">
						<view class="name">打卡</view>
						<view class="signInTime">{{signInTime}}</view>
					</view>
				</view>
				<view class="list" v-if="tabIdx===2">
<!-- 					<view>
						<uni-calendar 
						ref="calendar"
						:insert="false"
						@confirm="confirm"
						 />
						 <button @click="open">打开日历</button>
					</view> -->
					<view class="item" v-for="(item,index) in items" :key="index">
						 <view class="item-info">
							<view class="location">
								<text>位置：</text>
								<text>{{item.dutyLocation}}</text>
							</view>
							<view class="date">{{item.dutyTime}}</view>
						 </view>
						 <view class="item-icon-box" @click="previewImage(item.dutyPhoto)">
							<img class="item-icon" :src="item.dutyIcon" alt="打卡图片">
						 </view>
					</view>
				</view>
			</view>
		</view>
		<view class="alert success" v-if="state===1">
			<view class="mask">
				<view class="alert-wrap">
					<image class="icon" src="/static/face.png" mode="widthFix"></image>
					<view class="tip">打卡成功</view>
					<view class="button" @click="state=0">确定</view>					
				</view>			
			</view>
		</view>
	</view>
</template>

<script>
	import {parseTime} from '@/utils/index.js';
	import { creatDuty} from '@/api/system/dutyApi.js';
	import { getDutyRecord} from '@/api/system/dutyApi.js';
	import {upload} from '@/api/system/upload.js';	
	export default{
		data(){
			return{
				signInDate:parseTime(new Date(),'{y}-{m}-{d}'),
				signInTime:parseTime(new Date(),'{h}:{i}'),
				timer:null,
				tabIdx:1,
				state:0,
				dutyDate:new Date(),
				formData:{
					dutyLocation:"",
					dutyPhoto:"",
					dutyIcon:"",
					dutyLongitude:"",
					dutyLatitude:""
				},
				items:[]
			}
		},
		created() {
			let qrcode=uni.getStorageSync('qrcode')
			this.formData.dutyLocation=qrcode.targetTitle||"--"
			this.getLocation()
			this.setTime()
		},
		methods:{
			open(){
				this.$refs.calendar.open();
			},
			confirm(e) {
				console.log(e);
			},
			setTime(){
				this.timer=setInterval(()=>{
					this.signInTime=parseTime(new Date(),'{h}:{i}')
				},1000)
			},
			getLocation(){
				const self=this;
				uni.getLocation({
					type: 'wgs84',
					success: function (res) {
						self.formData.dutyLongitude=res.longitude
						self.formData.dutyLatitude=res.latitude
					}
				});		
			},			
			creatDuty(){	
				const self=this;
				uni.chooseImage({
					sourceType: ['camera'],
					success(res) {
						self.onsubmit(res)
					},
					fail() {
						uni.showToast({
							icon:"none",
							title:"打卡失败"
						})
					}
				})
				// let filePath=e.tempFiles[0].url
				// let fileresq=await upload({filePath})

			},
			async onsubmit(res){
				let filePath=res.tempFiles[0].path
				let fileResq=await upload({filePath,formData:{
					additions: `经度:${this.formData.dutyLongitude};纬度:${this.formData.dutyLatitude}`
				}})
				let file=JSON.parse(fileResq).data
				creatDuty({
					dutyLocation:this.formData.dutyLocation,
					dutyPhoto:file.fileUrl,
					dutyIcon:file.fileIcon,
					dutyLongitude:this.formData.dutyLongitude,
					dutyLatitude:this.formData.dutyLatitude	
				}).then(()=>{
					this.state=1
				}).catch(()=>{
					uni.showToast({
						icon:"none",
						title:"打卡失败"
					})
				})			
			},
			chanegTab(idx){
				this.tabIdx=idx
				if(idx===2){
					this.getRecord()
				}
			},
			getRecord(){
				let dutyDate=parseTime(this.dutyDate,"{y}-{m}-{d}")
				getDutyRecord(dutyDate).then((res)=>{
					this.items=res.data
				})
			},
			previewImage(img){
				uni.previewImage({
					urls:[img]
				})
			}
		},
		onUnload() {
			clearInterval(this.timer)
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		.tab{
			.tab-item{
				width: 50%;
				display: inline-block;
				padding: 0 100rpx;	
				box-sizing: border-box;
				.text{
					height: 100rpx;
					line-height: 100rpx;
					text-align: center;
					color: rgba(33, 33, 33, 1);
				}
				&.active{
					.text{
						color: rgba(77, 115, 255, 1);			
					}
				}
			}
		}
		.tab-content{
			background-color: rgba(245, 246, 248, 1);
			padding: 32rpx;
			.tab-wrap{
				background-color: #fff;
				min-height: calc(100vh - 170rpx);
			}
			.signIn{
				padding-top: 32rpx;
				.info{
					background-color: rgba(245, 246, 248, 1);
					width: 622rpx;
					margin: 0 auto;
					padding: 32rpx;
					box-sizing: border-box;
					.date{
						font-size: 30rpx;
						color:rgba(153, 153, 153, 1);
						.text{
							color: rgba(33, 33, 33, 1);
						}
					}
					.location{
						font-size: 30rpx;
						color:rgba(153, 153, 153, 1);
						.text{
							color: rgba(33, 33, 33, 1);
						}
					}					
				}

				.signIn-handle{
					width: 300rpx;
					height: 300rpx;
					border-radius: 50%;
					display: flex;
					justify-content: center;
					align-items: center;
					flex-direction: column;
					margin: 180rpx auto;
					background-color: rgba(90, 125, 251, 1);
					box-shadow: 0px 4px 20px 0px rgba(77, 115, 255, 0.3);
					.name{
						font-size: 40rpx;
						color: #fff;
					}
					.signInTime{
						font-size: 30rpx;
						color: rgba(255, 255, 255, 0.7);
					}
				}	
			}
			.list{
				background-color: rgba(245, 246, 248, 1);
				.item{
					margin-bottom: 20rpx;
					display: flex;
					align-items: center;
					background-color: #fff;
					padding: 30rpx;
					border-radius: 16rpx;
					.item-info{
						flex: 1;
						.location{
							font-weight: 400;
							font-size: 34rpx;
							color: rgba(33, 33, 33, 1);
						}
						.date{
							font-size: 24rpx;
							color: rgba(33, 33, 33, 1);	
							padding-top: 24rpx;
						}
					}
					.item-icon-box{
						width: 228rpx;
						height: 152rpx;
						background-color: rgba(245, 246, 248, 1);
						.item-icon{
							display: block;
							width: 228rpx;
							height: 152rpx;					
						}
					}
				}				
			}
		}
		.alert{
			.mask{
				display: block;
				position: fixed;
				content: "";
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				background-color: rgba(0,0,0,0.6);
				z-index: 99				
			}
			.alert-wrap{
				width: 630rpx;
				height: 556rpx;
				background-color: #fff;
				position: fixed;
				left: 50%;
				top: 50%;
				transform: translate(-50%,-50%);
				z-index: 999;
				border-radius: 16rpx;	
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				.icon{
					width: 160rpx;
				}
				.tip{
					font-size: 48rpx;
					color: rgba(77, 115, 255, 1);
					font-weight: 600;
					padding: 22rpx 0 40rpx 0;
				}
				.button{
					width: 360rpx;
					height: 84rpx;
					text-align: center;
					line-height: 84rpx;
					color: rgba(77, 115, 255, 1);
					font-size: 30rpx;
					border: 1px solid rgba(77, 115, 255, 1);
					border-radius: 8rpx;
				}
			}
		}
	}

</style>