<template>
	<view class='container'>
		<view class="mar-20">
			<view class="flex-column br-12 fs-28 mt-30" style="background: white;">
				<text class="fs-32 fw-700 pad-30" style="text-align: left;">基本信息</text>
				<view class="flex-row-between pad-30" style="border-bottom: 1rpx solid #E9E9E9;">
					<text style="color: #999999;">职业</text>
					<text>{{userInfo.accountType==1?'保洁':userInfo.accountType==2?'保安':userInfo.accountType==3?'维修':''}}</text>
				</view>
				<view class="flex-row-between pad-30" style="border-bottom: 1rpx solid #E9E9E9;">
					<text style="color: #999999;">姓名</text>
					<text>{{userInfo.name}}</text>
				</view>
				<view class="flex-row-between pad-30" style="border-bottom: 1rpx solid #E9E9E9;">
					<text style="color: #999999;">性别</text>
					<text>{{userInfo.sex==1?'女':'男'}}</text>
				</view>
				<view class="flex-row-between pad-30" style="border-bottom: 1rpx solid #E9E9E9;">
					<text style="color: #999999;">手机号</text>
					<text>{{userInfo.mobile}}</text>
				</view>
			</view>

			<view class="flex-column br-12 fs-24 mt-30 pad-30" style="background: white;color: #999999;">
				<text class="fs-32 fw-700" style="color: #000;">我的位置</text>
				<text class="mt-20">{{address==''?'请定位当前位置':address}}</text>
				<view @click="onDw" class="mt-20"
					style="display: flex;flex-direction: row-reverse;align-items: center;">
					<text>重新定位</text>
					<image class="w-25 h-25" src="/pagesMine/static/icon_dk_dw.png"></image>
				</view>
				<view class="mt-50" style="height: 1rpx;background: #E9E9E9;"> </view>
				<text class="mt-50" style="color: #000;">拍照</text>
				<view class="mt-20">
					<image class="w-120 h-120" :src="image" @click="onPz()"></image>
				</view>
			</view>

		</view>
		<view @click="onSubmit" class="fullscreen-btn mt-100">
			<text>提交打卡</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '安保打卡',
				type: 0,
				reason: '',
				submitShow: false,
				userInfo: '',
				address: '',
				image: '/static/icon_upload.png'
			};
		},
		onLoad(e) {
			this.userInfo = uni.getStorageSync('userInfo')
			this.type = e.type
			if (e.type == 0) {
				this.title = "安保打卡"
			} else {
				this.title = "保洁打卡"
			}
			this.onDw()
		},
		onShow() {
			// uni.$once('imageSrc', function(data) {
			// 	console.log('监听到事件来自 update ，携带参数 msg 为：' + data);
			// 	this.image = data
			// })
		},
		mounted() {
			// 监听一次性事件
			uni.$once('imageSrc', (data) => {
				// 确保数据变化是响应式的
				this.image = data // 直接赋值会触发更新

			});
		},
		methods: {
			onPz() {
				const name = this.userInfo.name + '(' + (this.type == 0 ? '安保' : '保洁') + ')'
				this.$wskj.to("/pagesMine/pages/dkpz?address=" + this.address + "&name=" + name)
			},
			onDw() {
				const _this = this;
				uni.getLocation({
					type: 'gcj02',
					success: function(res) {
						const latitude = res.latitude;
						const longitude = res.longitude;
						_this.reverseGeocode(latitude, longitude)
					}
				});

			},
			async onSubmit() {
				if (!this.address) {
					return this.$u.toast('请先定位当前位置')
				}
				if (!this.image) {
					return this.$u.toast('请拍照上传照片')
				}
				const imgData = await this.$wskj.uploadImg(this.image)
				console.log(imgData)
				const params = {
					url: '/api/clock/add',
					loadingTip: '',
					data: {
						clockAddress: this.address,
						clockImg: imgData.url
					},
				}
				this.$request(params).then(res => {
					this.$wskj.toast('提交成功')
					setTimeout(() => {
						this.$wskj.goBack(1)
					}, 600)
				})
			},
			// 提交
			onConfirmSubmit() {

			},
			// 根据经纬度逆向解析为中文地址
			reverseGeocode(latitude, longitude) {
				const key = 'cc4f24cc4928169e0d623573e4e0687e'; // 替换为你的高德API Key
				const url =
					`https://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${longitude},${latitude}&radius=1000&extensions=all`;
				uni.request({
					url: url,
					success: (res) => {
						if (res.data.status === '1') {
							this.address = res.data.regeocode.formatted_address; // 获取中文地址
							console.log('当前位置中文地址:', this.address);
						} else {
							console.error('地址解析失败：', res.data.info);
						}
					},
					fail: (err) => {
						console.error('请求失败：', err);
					}
				});
			}
		},
	};
</script>

<style scoped>
	.container {
		display: flex;
		flex-direction: column;
		background: #F2F2F2;
	}
</style>