<template>
	<view class="content">
		<view class="dk-title" @click="goDkHis">
			打卡历史
		</view>
		<map name="" class="map" :latitude="latitude" :longitude="longitude" :markers="markers"></map>
		<view class="info">
			<text class="info-tit">打卡时段</text> <text class="info-val">{{today}} 00:00-23:59</text>
		</view>
		<view class="info">
			<text class="info-tit">打卡地点</text> <text class="info-val">{{address}}</text>
		</view>
		<view class="info flex" style="align-items: flex-start;	padding-right: 20rpx;">
			<text class="info-tit">工作内容</text> 
			<textarea placeholder="请输入工作内容" v-model="content" name="" id="" cols="30" rows="10" style="border-radius: 20rpx;border: 1px solid #999;padding: 20rpx;"></textarea>
		</view>
		<view class="info" v-for="(item, index) in list">
			<text class="info-tit">打卡记录({{index + 1}})</text> <text class="info-val">{{item.create_time}}</text>
		</view>
		<view class="dk flex" @click="dk">
			<view class="dk-main flex">
				<view class="dk-main-tit">
					立即打卡
				</view>
				<view class="dk-main-time">
					{{time}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { goDk, getClockList } from '@/api/signing.js'
	
	const today = new Date();
	const year = today.getFullYear();
	const month = today.getMonth() + 1; // 月份从0开始，需要加1
	const day = today.getDate();
	const date = `${year}-${month}-${day}`
	export default {
		data() {
			return {
				content: '',
				loading: false,
				id: 0,
				today: date,
				address: '',
				timer: null,
				time: '',
				latitude: '',
				longitude: '',
				latitudes: '',
				longitudes: '',
				markers: [],
				list: []
			}

		},
		onLoad(opt) {
			if (opt.latitude) {
				this.id = opt.id
				this.latitude = opt.latitude
				this.longitude = opt.longitude
				this.address = opt.address
				this.markers.push({
					id: 1,
					latitude: this.latitude,
					longitude: this.longitude,
					title: '打卡点',
					iconPath: '/static/icon/shopAddress.png',
					width: 20,
					height: 20
				})
			}
			this.getLocation()
			this.getDkHis()
			if (this.timer) {
				clearInterval(this.timer);
			}
			this.updateTime();
			// 设置定时器每秒更新时间
			this.timer = setInterval(() => {
				this.updateTime();
			}, 1000);
		},
		methods: {
			goDkHis() {
				uni.navigateTo({
					url:'/pages/user/signing/dkhis?id=' + this.id
				})
			},
			getLocation() {
				let that = this
				uni.getLocation({
					type: 'gcj02',
					success(res) {
						that.latitudes = res.latitude
						that.longitudes = res.longitude
						console.log(that.latitudes,that.longitudes);
					},
					fail(err) {
						console.log(err,'err');
					}
				})
			},
			updateTime() {
				const now = new Date();
				const hours = String(now.getHours()).padStart(2, '0');
				const minutes = String(now.getMinutes()).padStart(2, '0');
				const seconds = String(now.getSeconds()).padStart(2, '0');
				this.time = `${hours}:${minutes}:${seconds}`;
			},
			dk() {
				if(this.loading) return;
				this.loading = true
				goDk({
					contract_id: this.id,
					latitude: this.latitudes,
					longitude: this.longitudes,
					content: this.content
				}).then(res => {
					this.getDkHis()
					uni.showToast({
						title: '打卡成功',
						duration: 2000
					});
					setTimeout(()=> {
						this.loading = false
					})
				}).catch(err => {
					this.loading = false
				})
			},
			getDkHis() {
				getClockList({
					contract_id: this.id,
					page:1,
					limit: 10,
					time: this.today
				}).then(res => {
					// console.log(res);
					this.list = res.data.list
				})
			}
		},
		onHide() {
			if (this.timer) {
				clearInterval(this.timer);
			}
		}

	}
</script>

<style lang="scss" scoped>
	.content {
		background-color: #fff;
		height: 100vh;
	}
	.dk-title {
		padding: 20rpx;
		text-align: right;
		font-weight: bold;
		font-size: 28rpx;
		color: #0066FF;
	}
	.map {
		width: 710rpx;
		height: 500rpx;
		margin:0 auto 40rpx;
	}

	.info {
		font-size: 26rpx;
		padding-left: 28rpx;
		margin: 20rpx 0;
		&-tit {
			display: inline-block;
			width: 160rpx;
			color: #6E6E6E;
		}

		&-val {
			color: #1B1B1B;
		}
	}

	.dk {
		width: 290rpx;
		height: 290rpx;
		margin: 50rpx auto;
		border-radius: 50%;
		border: 3rpx solid #0066FF;
		justify-content: center;
		align-items: center;

		&-main {
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 268rpx;
			height: 268rpx;
			background: #0066FF;
			border-radius: 50%;
			color: #FFFFFF;
			font-size: 38rpx;
		}
	}
</style>