<template>

	<view class="skeleton-container" v-if="isLoading">
		<!-- 顶部提示栏骨架 -->
		<view class="skeleton-top-tip"></view>

		<!-- 身份信息区域骨架 -->
		<view class="skeleton-section">
			<view class="skeleton-title"></view>
			<view class="skeleton-form-item">
				<view class="skeleton-label"></view>
				<view class="skeleton-input"></view>
			</view>
			<view class="skeleton-form-item">
				<view class="skeleton-label"></view>
				<view class="skeleton-input"></view>
			</view>
		</view>

		<!-- 身份证照片区域骨架 -->
		<view class="skeleton-section">
			<view class="skeleton-title"></view>
			<view class="skeleton-upload-item"></view>
			<view class="skeleton-upload-item"></view>
		</view>


		<!-- 协议勾选骨架 -->
		<view class="skeleton-agreement"></view>

		<!-- 提交按钮骨架 -->
		<view class="skeleton-submit"></view>
	</view>


	<view v-else class="real-name-auth-container">
		<!-- 顶部提示栏 -->
		<view class="top-tip">
			<text class="tip-text">修改实名认证将重新审核，大约1~2工作日</text>
		</view>

		<!-- 表单内容 -->
		<view class="form-content">
			<!-- 身份信息区域 -->
			<view class="section">
				<view class="section-title">身份信息</view>

				<!-- 姓名输入 -->
				<view class="form-item">
					<view class="label">姓名</view>
					<u-input v-model="formData.nick" placeholder="请输入身份证姓名" placeholder-color="#999" border="none"
						class="input" input-align="right" />
				</view>

				<!-- 身份证号输入 -->
				<view class="form-item">
					<view class="label">身份证号</view>
					<u-input v-model="formData.idenNumber" placeholder="请输入18位身份证号" placeholder-color="#999" border="none"
						class="input" input-align="right" maxlength="18" />
				</view>
			</view>

			<!-- 身份证照片区域 -->
			<view class="section">
				<view class="section-title">身份证照片</view>

				<!-- 上传身份证正面 -->
				<view class="upload-item">
					<view  @click="uploadFImg(1)" class="upload-area bordered-area">
						<view v-if="!formData.identityA" class="upload-content">
							<view class="upload-icon">+</view>
							<view class="upload-text">上传身份证正面</view>
						</view>
						<!-- 显示上传后的占位图 -->
						<view v-else class="uploaded-placeholder">
							<image :src="Service.GetMateUrlByImg(formData.identityA)" style="width: 100%; height: 100%;" mode=""></image>
						</view>
					</view>
				</view>

				<!-- 上传身份证反面 -->
				<view class="upload-item">
					<view @click="uploadFImg(2)" class="upload-area bordered-area">
						<view v-if="!formData.identityB" class="upload-content">
							<view class="upload-icon">+</view>
							<view class="upload-text">上传身份证反面</view>
						</view>
						<!-- 显示上传后的占位图 -->
						<view v-else class="uploaded-placeholder">
							<image :src="Service.GetMateUrlByImg(formData.identityB)" style="width: 100%; height: 100%;" mode=""></image>
						</view>
					</view>
				</view>
			</view>


			<!-- 协议勾选 -->
			<view  class="agreement-section">
				<view  @click="isAgreed=!isAgreed" class="" style="margin-right: 10rpx;">
					<up-icon :name="!isAgreed?'checkmark-circle':'checkmark-circle-fill'"
						:color="!isAgreed?'#999':'var(--nav-mian)'" size="20"></up-icon>
				</view>
				<text class="agreement-text">我已阅读并同意</text>
				
				<a style="color: #1890ff; text-decoration: none; " :href="url">《隐私政策》</a>
			</view>

			<!-- 提交按钮 -->
			<view class="submit-section">
				<u-button @click="save()" type="primary" class="submit-btn" size="large">
					提交认证
				</u-button>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { onLoad } from '@dcloudio/uni-app';
	import { ref, computed } from 'vue';
	import { CNRiderDataService , Service } from "@/Service/CN/CNRiderDataService"
	import { CNRiderLoginService  } from "@/Service/CN/CNRiderLoginService"
	
	let isLoading = ref(true)
	
	let isAgreed=ref(false)
	
	// 表单数据
	const formData = ref<any>({});

	let url=ref('')
	
	onLoad(() => {
		getData()
		getUrl()
	})
	
	
	
	const getUrl=()=>{
		CNRiderLoginService.GetPrivacy(3).then(res=>{
			url.value=res.data.url
		})
	}
	
	const getData = () => {
		CNRiderDataService.GetRiderInfo().then(res => {
			isLoading.value = false
			if(res.data){
				formData.value=res.data.riderInfo
				
			}
		})
	}
	
	const uploadFImg = (index:number) => {
		uni.chooseImage({
			count: 1, // 最多选择3张图片
			sizeType: ['original', 'compressed'], // 支持原图和压缩图
			sourceType: ['album', 'camera'], // 可从相册选择或使用相机拍照
			success: function (res) {
				let path = res.tempFiles[0].path
				Service.uploadH5(path, 'Avatar', data => {
					if(index==1){
						formData.value.identityA=data
					}else{
						formData.value.identityB=data
					}
				})
	
			},
			fail: function (err) {
				console.error('选择失败:', err.errMsg);
			}
		})
	}
	

	// 处理协议点击
	const handleAgreementClick = (e) => {
		console.log(e);
		// 打开协议详情
		console.log('打开协议详情');
	};
	
	
	const save=()=>{
		
		if(!isAgreed.value){
			Service.Msg('请同意骑手实名认证协议')
			return
		}
		if(!formData.value.nick){
			Service.Msg('请输入姓名')
			return
		}
		if(!formData.value.idenNumber){
			Service.Msg('请输入身份证号')
			return
		}
		
		
		CNRiderDataService.UpdateRiderIdentity(formData.value.nick,formData.value.idenNumber,formData.value.identityA,formData.value.identityB).then(res=>{
			if(res.data){
				Service.Msg('上传成功!')
				setTimeout(()=>{
					Service.GoPageBack()
				},1000)
			}else{
				Service.Msg(res.msg)
			}
		})
		
	}
	
	
</script>

<style scoped lang="scss">
	page {
		background-color: #f5f5f5;
	}

	/* 骨架屏样式 */
	.skeleton-container {
		padding: 0 30rpx;
	}

	.skeleton-top-tip {
		height: 70rpx;
		width: 100%;
		background-color: #e6f7ff;
		margin: 20rpx 0;
		border-radius: 10rpx;
		animation: skeleton-loading 1.5s infinite ease-in-out;
	}

	.skeleton-section {
		margin-bottom: 40rpx;
	}

	.skeleton-title {
		height: 32rpx;
		width: 160rpx;
		background-color: #e6e6e6;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		animation: skeleton-loading 1.5s infinite ease-in-out;
	}

	.skeleton-form-item {
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.skeleton-form-item:last-child {
		border-bottom: none;
	}

	.skeleton-label {
		height: 28rpx;
		width: 120rpx;
		background-color: #e6e6e6;
		border-radius: 14rpx;
		animation: skeleton-loading 1.5s infinite ease-in-out;
	}

	.skeleton-input {
		height: 28rpx;
		width: calc(100% - 140rpx);
		background-color: #e6e6e6;
		margin-left: 20rpx;
		border-radius: 14rpx;
		animation: skeleton-loading 1.5s infinite ease-in-out;
	}

	.skeleton-upload-item {
		height: 200rpx;
		width: 100%;
		background-color: #fff;
		border: 2rpx solid #000;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		animation: skeleton-loading 1.5s infinite ease-in-out;
	}

	.skeleton-face-area {
		height: 240rpx;
		background-color: #fff;
		border-radius: 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.skeleton-face-icon {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		background-color: #e6e6e6;
		margin-bottom: 20rpx;
		animation: skeleton-loading 1.5s infinite ease-in-out;
	}

	.skeleton-face-text {
		height: 28rpx;
		width: 200rpx;
		background-color: #e6e6e6;
		border-radius: 14rpx;
		animation: skeleton-loading 1.5s infinite ease-in-out;
	}

	.skeleton-agreement {
		height: 30rpx;
		width: 60%;
		background-color: #e6e6e6;
		border-radius: 15rpx;
		margin-bottom: 40rpx;
		animation: skeleton-loading 1.5s infinite ease-in-out;
	}

	.skeleton-submit {
		height: 92rpx;
		width: 100%;
		background-color: #e6e6e6;
		border-radius: 46rpx;
		animation: skeleton-loading 1.5s infinite ease-in-out;
	}

	/* 骨架屏动画 */
	@keyframes skeleton-loading {
		0% {
			opacity: 0.6;
		}

		50% {
			opacity: 1;
		}

		100% {
			opacity: 0.6;
		}
	}

	.top-tip {
		background-color: #E6F7FF;
		padding: 20rpx 30rpx;
		margin: 20rpx;
		border-radius: 20rpx;
	}

	.tip-text {
		color: #1890ff;
		font-size: 28rpx;
	}

	.form-content {
		padding: 0 30rpx;
	}

	.section {
		margin-bottom: 40rpx;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
		padding-left: 10rpx;
	}

	.form-item {
		background-color: #fff;
		padding: 30rpx;
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.form-item:last-child {
		border-bottom: none;
	}

	.label {
		width: 120rpx;
		font-size: 28rpx;
		color: #333;
	}

	.input {
		flex: 1;
		font-size: 28rpx;
	}

	.upload-item {
		margin-bottom: 20rpx;
	}

	.upload-area {
		height: 350rpx;
		border-radius: 8rpx;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		cursor: pointer;
		position: relative;
	}

	.bordered-area {
		background-color: #fff;
		border-radius: 20rpx;
		width: 100%;
	}

	.upload-content {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 100%;
		z-index: 1;
	}

	.upload-icon {
		font-size: 60rpx;
		color: #1890ff;
		margin-bottom: 10rpx;
	}

	.upload-text {
		font-size: 28rpx;
		color: #1890ff;
	}

	.uploaded-placeholder {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #fff;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #333;
		font-size: 28rpx;
	}

	.face-verify-area {
		background-color: #fff;
		height: 240rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		cursor: pointer;
		border-radius: 8rpx;
	}

	.face-icon {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		background-color: #e6f7ff;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.face-text {
		font-size: 28rpx;
		color: #1890ff;
		font-weight: 500;
	}

	.agreement-section {
		display: flex;
		align-items: center;
		margin-bottom: 40rpx;
		padding: 0 10rpx;
	}

	.checkbox {
		margin-right: 10rpx;
	}

	.agreement-text {
		font-size: 26rpx;
		color: #666;
	}

	.agreement-link {
		font-size: 26rpx;
		color: #1890ff;
		margin-left: 4rpx;
	}

	.input {
		flex: 1;
		font-size: 28rpx;
		color: #333;
	}

	.submit-section {
		margin-top: 60rpx;
		padding: 0 0rpx;
	}

	.submit-btn {
		height: 92rpx;
		font-size: 32rpx;
		border-radius: 46rpx;
		background-color: #1890ff;
	}

	.submit-btn[disabled] {
		background-color: #a0cfff;
		color: #ffffff;
	}
</style>