<template>
	<view class="home">
		<!-- 顶部背景 -->
		<view class="home-top">
			<!-- https://tennis-oss.xzaiyp.top/2024-10-22/81f3569e-c60c-4945-bfc9-b0e976f26d8e.png -->
			<image class="home-top-bg" src="/static/image/center/1.png" mode="widthFix"></image>
		</view>

		<!-- 卡片(地图) -->
		<view class="card">
			<view @click="openBroadside" class="team">
				<view class="change-icon">
					<image src="/static/image/broadside/17.png" mode="widthFix" style="height: 40rpx;"></image>
				</view>
				<view class="project"
				v-if="userInfo.team">
					{{ userInfo.team.name }}
				</view>
			</view>
			<view class="map">
				<map style="width: 100%; height: 100%" 
				:latitude="position.latitude" 
				:longitude="position.longitude" 
				:markers="covers" :scale="13">
				</map>
			</view>
		</view>

		<!-- 打卡按钮 -->
		<view class="punch-card">
			<view @click="toClock" class="circle"
			:style="{background : ['#aaa', '', '#f40'][position.status]}">
				<view class="title" v-if="position.status == 0">定位中...</view>
				<view class="title" v-else-if="position.status == 2">定位失败</view>
				<view class="title" v-else>拍照打卡</view>
				<view class="time">{{ date.format('YYYY-MM-DD HH:mm:ss') }}</view>
			</view>
		</view>

		<!-- 签到记录 -->
		<div @click="toRecord" class="sign">
			<div class="sign-title">
				<image class="sign-title-img" src="/static/image/broadside/project.png" mode="widthFix" style="height: 40rpx;"></image>
				<div class="title">打卡记录</div>
			</div>
			<div class="project">
				<text class="project-name"
				v-if="userInfo.team">{{ userInfo.team.name }}</text>
				<uv-icon name="arrow-right" color="#000"></uv-icon>
			</div>
		</div>

        <!-- 使用说明 -->
        <view class="usage-notice">
            <text class="notice-text">⚠️ 仅限公司内部人员使用</text>
        </view>
		
		<!-- <image 
		@click="upload"
		:src="image" mode=""></image> -->

		<!-- tab栏 -->
		<tabbar :select="0"></tabbar>

		<!-- 侧边栏 -->
		<broadside ref="broadside"></broadside>
	</view>
</template>

<script>
	import tabbar from '../../components/base/tabbar.vue'
	import broadside from '../../components/broadside/broadside.vue'
	import { mapState } from 'vuex'
	export default {
		name: "Home",
		components: {
			tabbar,
			broadside
		},
		data() {
			return {
				latitude: 39.909,
				longitude: 116.39742,
				covers: [],
				inter : null,
				date : this.$dayjs(),
				image : '',
			}
		},
		computed : {
			...mapState(['teamList', 'userInfo', 'position']),
		},
		onLoad() {
			this.inter = setInterval(() => {
				this.date = this.$dayjs()
			}, 1000)
		},
		onUnload() {
			clearInterval(this.inter)
		},
		onShow() {
			this.getLocation()
			this.$store.commit('getUserInfo')
			this.$store.commit('getLocation')
		},
		methods: {
			// 跳转签到记录
			toRecord() {
				uni.navigateTo({
					url: "/pages/subPack/record/record"
				})
			},
			upload(){
				this.$Oss.ossUploadImage({
					success : url => {
						this.image = url
					}
				})
			},

			//打开侧边栏
			openBroadside() {
				this.$refs.broadside.open()
			},
			
			toClock(){
				
				if(this.position.status == 0){
					uni.showToast({
						title: '正在定位中，请稍后！',
						icon : 'none'
					})
					return
				}
				
				if(this.position.status == 0){
					uni.showToast({
						title: '定位失败，请检查当前定位是否开启！',
						icon : 'none'
					})
					return
				}
				
				// uni.navigateTo({
				// 	url: "/pages/subPack/human/human"
				// })
				
				// 检查当前是否在打卡时间段内
				this.$api('clockTime', {
					lon : this.position.longitude,
					lat : this.position.latitude,
				}, res => {
					//打卡(跳转人脸识别)
					if(res.code == 200){
						uni.navigateTo({
							// url: "/pages/subPack/face/face"
							url: "/pages/subPack/human/human"
						})
					}
				})
			},

			//获取用户位置
			getLocation() {
				let self = this;
				// uni.showLoading({
				// 	title: '定位中...'
				// })
				uni.getLocation({
					type: 'gcj02',
					success: function(res) {
						// uni.hideLoading()
						self.longitude = res.longitude;
						self.latitude = res.latitude;
						// self.covers.push({
						// 	id: 1,
						// 	latitude: res.latitude,
						// 	longitude: res.longitude,
						// 	iconPath: 'https://tennis-oss.xzaiyp.top/2024-10-22/103fa1db-8524-45e6-8d00-c36a69977a5b.png',
						// 	width: 30,
						// 	height: 30
						// })
					},
					fail() {
						self.checkAndRequestLocationPermission()
					}
				});
			},

			checkAndRequestLocationPermission() {
				let self = this;
				uni.getSetting({
					success: function(res) {
						if (!res.authSetting['scope.userLocation']) {
							// 用户未授权定位权限  
							uni.showModal({
								title: '提示',
								content: '当前定位未开启，请点击确定手动开启定位',
								success: function(modalRes) {
									if (modalRes.confirm) {
										// 用户选择确认开启定位  
										uni.openSetting({
											success: function(settingRes) {
												if (settingRes.authSetting[
														'scope.userLocation']) {
													// 重新获取位置信息  
													self.getLocation();
												} else {
													// 用户未开启定位权限  
													console.log('用户未开启定位权限');
												}
											},
											fail: function(err) {
												console.log('打开设置页面失败：', err);
											}
										});
									} else {
										// 用户选择取消  
										self.getLocation()
									}
								}
							});
						} else {
							// 用户已授权定位权限但获取位置失败（可能是设备未开启定位服务）  
							console.log('用户已授权定位权限，但获取位置失败');
							// 可以提示用户检查设备定位服务是否开启  
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.home {
		background: $bg-color;
		min-height: 100vh;
		// 顶部背景
		.home-top {
			height: 490rpx;
			overflow: hidden;
			position: relative;
			.home-top-bg {
				width: 750rpx;
				height: 200rpx;
			}
		}
        // 使用说明
        .usage-notice {
            padding: 10rpx;
            margin-left: 220rpx;
            .notice-text {
                font-size: 24rpx;
                color: #ff6b35;
                font-weight: 500;
            }
        }

		// 卡片(地图)
		.card {
			width: 95%;
			margin: -340rpx auto 0rpx auto;
			position: relative;

			.team {
				display: flex;
				margin: 15rpx 0rpx;
				color: white;
				font-size: 34rpx;

				.change-icon {
					display: flex;
					align-items: center;
					width: 40rpx;
					margin-right: 10rpx;

					image {
						width: 100%;
					}
				}
			}

			.map {
				height: 350rpx;
				background: $main-color;
				border-radius: 20rpx;
				overflow: hidden;
			}
		}

		//打卡按钮
		.punch-card {
			display: flex;
			justify-content: center;
			margin: 60rpx 0rpx;

			.circle {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				width: 370rpx;
				height: 370rpx;
				border-radius: 50%;
				background: $main-color;
				color: white;

				.title {
					font-size: 36rpx;
				}

				.time {
					font-size: 30rpx;
					margin-top: 20rpx;
				}
			}
		}

		// 签到记录
		.sign {
			display: flex;
			flex-wrap: wrap;
			width: 90%;
			margin: 0rpx auto;
			background: white;
			border-radius: 15rpx;
			box-sizing: border-box;
			padding: 30rpx 30rpx 30rpx 20rpx;
			box-shadow: 0rpx 0rpx 10rpx rgba(0, 0, 0, .1);

			.sign-title {
				display: flex;
				width: 30%;

				.sign-title-img {
					width: 40rpx;
					margin-right: 15rpx;
				}

				.title {}
			}

			.project {
				display: flex;
				width: 70%;
				justify-content: space-between;

				.project-name {
					color: $main-color;
				}
			}
		}
	}
</style>