<template>
	<view class="container">
		<!-- 头部学生信息区域 -->
		<view class="header">
			<view class="img">
				<img
					:src="userInfo.headImage||'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F7974778847%2F1000&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1673765601&t=e38144720930d00c5bf59536379da218'">
			</view>
			<view class="msg">
				<view class="name">
					{{userInfo.name}}
				</view>
				<view class="major">
					{{userInfo.college||"南昌交通学院"}}
				</view>
			</view>
		</view>

		<!-- 主体打卡区域 -->
		<view class="body">
			<!-- 卡片区域 -->
			<view class="card" v-if="list.length">
				<!-- 每一个卡片区域 -->
				<view class="item" v-for="item in list" :key="item.id">
					<view class="item_box">
						<view class="title">
							{{item.ruleName}}
						</view>
						<view class="time">
							{{item.timeRange}}
						</view>
						<view class="type">
							<img v-if="item.finish" src="./imgs/success.png">
							<span v-if="item.finish">{{item.updateTime}}</span>
							{{item.finish?"已打卡":"未打卡"}}
						</view>
					</view>
				</view>

			</view>
			<!-- list数组为空时的占位盒子 -->
			<view class="card" v-else></view>

			<!-- 打卡区域 -->
			<view :class="{clock:flags,active:!flags}" @click="handlePunch">
				<view class="info" v-if="flags&&list.length">
					打卡
				</view>
				<view class="info" v-else>
					无法打卡
				</view>
				<view class="time">
					{{nowTime}}
				</view>
			</view>

			<!-- 位置信息区域 -->
			<view class="address" v-if="flags&&list.length">
				{{address}}
			</view>
			<view class="address" v-if="addressFlags&&!flags">
				不在管理员设定范围打卡
			</view>
			<view class="address" v-if="timeFlags&&!flags">
				未到打卡时间无法打卡
			</view>
			<view class="address" v-if="!list.length">
				无打卡任务无需打卡
			</view>
			<view class="address" v-if="!list.length">
				今日休息无需打卡
			</view>

			<view class="address" v-if="!list.length">
				下一次打卡时间段为18:00-19:00
			</view>
		</view>
	</view>
</template>

<script>
	var QQMapWX = require('../../util/qqmap-wx-jssdk1.1/qqmap-wx-jssdk');
	var qqmapsdk;
	export default {
		data() {
			return {
				// 用户信息
				userInfo: {},
				// 打卡规则列表
				list: [],
				flags: true,
				addressFlags: false,
				timeFlags: false,
				// 当前时间
				nowTime: "",
				// 当前定位位置信息
				address: "",
				// 定时器标识
				timer: null
			};
		},
		onLoad() {
			uni.removeStorageSync("manager")
			uni.removeStorageSync("sub-administrator")
			// 实例化API核心类
			qqmapsdk = new QQMapWX({
				// 申请的key
				key: 'R43BZ-2XROX-L7T45-T5OQI-IBDFT-GNBOI'
			});
			this.getNowTime()
			this.getUserInfo()
			this.getLocationData()
			this.getRulesList()
		},
		onUnload() {
			if (this.timer) {
				clearInterval(this.timer)
			}
		},
		methods: {
			// 获取当前时间
			getNowTime() {
				if (this.timer) {
					clearInterval(this.timer)
				} else {
					this.timer = setInterval(() => {
						let date = new Date()
						let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
						let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
						let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
						this.nowTime = hours + ':' + minutes + ':' + seconds
						// console.log(this.nowTime);
					}, 1000)
				}
			},
			// 获取用户详细信息
			async getUserInfo() {
				let userId = uni.getStorageSync("userInfo").id || 5046
				let res = await this.$myRequest({
					url: `/attendance/api/system/user/detail/${userId}`,
				})
				// console.log(res);
				if (res.code == 200) {
					this.userInfo = res.data

					if (this.userInfo.roles) {
						let temList = []
						this.userInfo.roles.forEach((ele) => {
							temList.push(ele.id)
						})
						let flag = temList.includes(2)
						let flag2 = temList.includes(3)
						if (flag) {
							uni.setStorageSync("manager", flag)
						}
						if (flag2) {
							uni.setStorageSync("sub-administrator", flag2)
						}
					}
				}
			},
			// 获取当前定位位置信息
			getLocationData() {
				qqmapsdk.reverseGeocoder({
					success: (res) => {
						// console.log(res);
						if (res.status == 0) {
							// 获取详细地址信息
							this.address = res.result.address
						} else {
							uni.showToast({
								title: "请求定位失败",
								icon: 'none'
							})
						}
					}
				})
			},
			// 获取打卡规则列表
			async getRulesList() {
				let name = uni.getStorageSync("userInfo").username
				let res = await this.$myRequest({
					url: "/attendance/api/sign/check/in/list",
					data: {
						name: name ? name : "",
						size: 999
					}
				})
				// console.log(res);
				if (res.code == 200) {
					this.list = res.data.list
				}
			},
			// 点击打卡按钮回调
			handlePunch() {
				// 获取用户位置权限
				uni.authorize({
					scope: 'scope.userLocation',
					success() {
						uni.navigateTo({
							url: "/pages/location/location"
						})
					},
					fail() {
						uni.showModal({
							title: '提示',
							content: '请先开启定位权限，否则将无法使用定位功能',
							cancelText: '不授权',
							confirmText: '授权',
							success: function(res) {
								if (res.confirm) {
									uni.openSetting({
										success(res) {}
									})
								} else if (res.cancel) {}
							}
						});
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding-top: 20rpx;

		.header {
			display: flex;
			margin: 0 30rpx 30rpx 30rpx;
			width: 690rpx;
			height: 130rpx;
			background-color: #fff;

			.img {
				margin: 30rpx;
				width: 70rpx;
				height: 70rpx;

				img {
					width: 100%;
					height: 100%;
				}
			}

			.msg {
				margin: 30rpx 0;
				height: 70rpx;
				line-height: 36rpx;

				.name {
					font-size: 32rpx;
					font-weight: 400;
				}

				.major {
					font-size: 24rpx;
					font-weight: 400;
					color: #A6A6A6;
				}
			}
		}

		.body {
			margin: 0 30rpx;
			width: 690rpx;
			height: 919rpx;
			background-color: #fff;

			.card {
				padding-top: 30rpx;
				white-space: nowrap;
				height: 160rpx;
				overflow-x: auto;

				.item {
					display: inline-block;
					margin: 0 15rpx;
					width: 300rpx;
					height: 130rpx;
					line-height: 12rpx;
					border-radius: 8rpx;
					font-weight: 400;
					background-color: #E6E6E6;

					.item_box {
						display: flex;
						flex-direction: column;
						justify-content: space-evenly;
						height: 130rpx;
						padding-left: 30rpx;

						.title {
							font-size: 28rpx;
						}

						.time {
							font-size: 24rpx;
						}

						.type {
							font-size: 24rpx;
							color: #808080;

							img {
								margin-right: 10rpx;
								width: 20rpx;
								height: 20rpx;
							}
						}
					}
				}
			}

			.clock {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin: auto;
				margin-top: 145rpx;
				width: 300rpx;
				height: 300rpx;
				border-radius: 150rpx;
				text-align: center;
				color: #fff;
				background: linear-gradient(180deg, #00ACFC 0%, #0082FC 100%);

				.info {
					height: 58rpx;
					font-size: 40rpx;
					font-weight: 700;
				}

				.time {
					height: 41rpx;
					font-size: 28rpx;
					font-weight: 400;
				}
			}

			.active {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin: auto;
				margin-top: 145rpx;
				width: 300rpx;
				height: 300rpx;
				border-radius: 150rpx;
				text-align: center;
				color: #fff;
				background: linear-gradient(180deg, #CCCCCC 0%, #B3B3B3 100%);

				.info {
					height: 58rpx;
					font-size: 40rpx;
					font-weight: 700;
				}

				.time {
					height: 41rpx;
					font-size: 28rpx;
					font-weight: 400;
				}
			}

			.address {
				margin-top: 34rpx;
				height: 35rpx;
				line-height: 35rpx;
				text-align: center;
				font-size: 24rpx;
				font-weight: 400;
				color: #808080;
			}
		}
	}
</style>
