<template>
	<div class="container">
		<!-- <u-modal v-model="show" :content="content"></u-modal> -->
		<model :show="show" @close="show=false" :content="content" />
		<view class="white-content">
			<view class="content-title">
				<view class="content-weight">身份证上传</view>
				<image class="content-img" src="https://www.focusnu.com/media/directive/index/bian.png" />
			</view>
			<view class="white-photo" @click="getMessage">
				<view class="photo-left">
					<view class="photo-weight">人像面</view>
					<view class="photo-font">请上传身份证人像面</view>
				</view>
				<view style="position: relative;">
					<image class="photo"
						:src="headImge ? headImge : `https://www.focusnu.com/media/directive/index/IDcard.png`" />
					<image v-if="!headImge"
						style="position: absolute;top: 50%;left: 50%;width: 70rpx;height: 60rpx;transform: translate(-50%,-50%);"
						src="https://www.focusnu.com/media/directive/index/takephoto.png" />
				</view>

			</view>
			<view class="white-photo" style="margin-top: 30rpx;" @click="getMessage">
				<view class="photo-left">
					<view class="photo-weight">国徽面</view>
					<view class="photo-font">请上传身份证国徽面</view>
				</view>
				<view style="position: relative;">
					<image class="photo"
						:src="backImge ? backImge : `https://www.focusnu.com/media/directive/index/backIDcard.png`" />
					<image v-if="!backImge"
						style="position: absolute;top: 50%;left: 50%;width: 70rpx;height: 60rpx;transform: translate(-50%,-50%);"
						src="https://www.focusnu.com/media/directive/index/takephoto.png" />
				</view>

			</view>
			<view class="white-message">
				<view class="message-title">
					<view class="shu"></view>
					<view class="message-weight">
						确认户口本本人页信息
					</view>
				</view>
				<view style="margin-bottom: 20rpx;">
					<view v-for="(item,index) in nameArray" :key="index" class="one"
						@click="openLook(textArray[index])">
						<view class="one-left">{{item}}</view>
						<!-- <view class="one-right">{{textArray[index] ? textArray[index] : "自动获取" }}</view> -->
						<view class="one-right">{{textArray[index] ? textArray[index] : "自动获取" }}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="gray-font">
			<view class="">注意事项:</view>
			<view class="">
				同一个身份证号只能认证一个账号国徽而与正面信息应为同一身份证的信息目在有效期内,所有上传照片需清晰且未遮挡，请勿进行美化和修改,所有上传信息均会被妥善保管,不会用于其他商业用途或传输给第三方。</view>
		</view>
		<view class="finish-button" @click="next">
			下一步
		</view>
	</div>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue'
	import model from "@/compontent/public/model.vue"

	const show = ref(false);
	const content = ref("");

	const nameArray = ["姓名", "性别", "身份证号码", "民族", "出生日期", "住址", "签发机关", "有效期限"];
	const textArray = reactive(["", "", "", "", "", "", "", ""]);

	// 本地保存的临时文件路径
	const tempImagePath = ref('')

	// 拍照并上传
	function getMessage() {
		// uni.navigateTo({ url: '/pages/addoldman/camera' });
		// 使用 UniApp 的 API 调用摄像头
		uni.chooseImage({
			count: 1,
			sourceType: ['camera'],
			success: chooseRes => {
				tempImagePath.value = chooseRes.tempFilePaths[0]
				// 拍照成功后，调用上传函数
				uploadImage(tempImagePath.value)

				// uni.navigateTo({ url: `/pages/addoldman/camera?url=${chooseRes.tempFilePaths[0]}` });
			},
			fail: err => {
				console.error('拍照失败：', err)
			}
		})
	}

	const headImge = ref("");
	const backImge = ref("");

	// 上传图片到服务器
	function uploadImage(filePath) {
		uni.showLoading()
		uni.uploadFile({
			url: `${uni.getStorageSync('serverUrl')}/api/ocr/idCard`, // 替换为您的POST接口地址
			filePath,
			name: 'file', // 后端接收时的字段名
			header: {
				'X-Access-Token': uni.getStorageSync('token') || '',
			},
			formData: {},
			success: uploadRes => {
				if (!JSON.parse(uploadRes.data).success) {
					uni.showToast({
						title: '识别失败',
						icon: 'error'
					})
					uni.hideLoading()
					return
				}

				if (JSON.parse(JSON.parse(uploadRes.data).result.data).data.face) {
					let father = JSON.parse(JSON.parse(uploadRes.data).result.data).data.face.data;
					textArray[0] = father.name;
					textArray[1] = father.sex;
					textArray[2] = father.idNumber;
					textArray[3] = father.ethnicity;
					textArray[4] = father.birthDate;
					textArray[5] = father.address;
					uni.showToast({
						title: '识别成功',
					})
					headImge.value = filePath;
					uni.hideLoading()
				} else {
					let father = JSON.parse(JSON.parse(uploadRes.data).result.data).data.back.data;
					textArray[6] = father.issueAuthority;
					textArray[7] = father.validPeriod;
					uni.showToast({
						title: '识别成功',
					})
					backImge.value = filePath;
					uni.hideLoading()
				}
			},
			fail: err => {
				uni.showToast({
					title: '上传出错',
					icon: 'error'
				})
				uni.hideLoading()
			}
		})
	}

	function isAtLeastEightChars(str) {
		return typeof str === 'string' && str.length >= 8;
	}
	const openLook = (res) => {
		if (isAtLeastEightChars(res)) {
			content.value = res;
			show.value = true
		}
	}
	const next = () => {
		uni.navigateTo({
			url: "/pages/addoldman/yibao"
		});
	}
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		width: 100%;
		background-color: rgb(239, 241, 252);
		position: relative;
		box-shadow: 2rpx 2rpx 4rpx rgba(0, 0, 0, 0.1);

		.white-content {
			width: 90%;
			margin-left: 5%;
			margin-top: 30rpx;
			// height: 1200rpx;
			border-radius: 35rpx;
			background-color: rgb(245, 251, 254);

			.content-title {
				display: flex;
				// align-items: center;
				height: 100rpx;
				position: relative;

				.content-weight {
					// font-size: 35rpx;
					font-weight: 600;
					margin-left: 70rpx;
					margin-top: 20rpx;
				}

				.content-img {
					position: absolute;
					right: 0;
					top: 0;
					width: 400rpx;
					height: 100%;
				}
			}
		}

		.white-photo {
			width: 90%;
			margin-left: 5%;
			// margin-top: 30rpx;
			height: 300rpx;
			border-radius: 35rpx;
			background-color: #fff;
			box-shadow: 4rpx 4rpx 8rpx rgba(0, 0, 0, 0.1);
			justify-content: space-around;
			align-items: center;
			display: flex;

			.photo {
				width: 300rpx;
				height: 200rpx;
			}
		}

		.white-message {
			width: 90%;
			margin-left: 5%;
			margin-top: 30rpx;
			margin-bottom: 30rpx;
			// height: 800rpx;
			border-radius: 35rpx;
			background-color: #fff;
			box-shadow: 4rpx 4rpx 8rpx rgba(0, 0, 0, 0.1);
			justify-content: space-around;
			// align-items: center;
			display: flex;
			flex-direction: column;

			.message-title {
				width: 100%;
				height: 100rpx;
				align-items: center;
				display: flex;

				.shu {
					width: 10rpx;
					height: 30rpx;
					background-color: #0097FF;
					border-radius: 10rpx;
					margin: 0 20rpx 0 30rpx;
				}

				.message-weight {
					font-size: 30rpx;
					// font-weight: 600;
				}
			}

			.one {
				width: 90%;
				margin-left: 5%;
				border-bottom: 1rpx solid #d7d7d7;
				height: 90rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;

				.one-left {
					margin-left: 10rpx;
				}

				.one-right {
					margin-right: 10rpx;
					color: #999999;
					overflow: hidden;
					/* 隐藏超出内容 */
					white-space: nowrap;
					/* 不换行 */
					text-overflow: ellipsis;
					max-width: 300rpx;
				}
			}
		}
	}

	.photo-left {
		.photo-weight {
			font-size: 26rpx;
			font-weight: 600;
		}

		.photo-font {
			font-size: 23rpx;
			margin-top: 10rpx;
		}

	}

	.gray-font {
		padding: 30rpx 60rpx;
		color: #999999;
	}

	.finish-button {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 70%;
		height: 100rpx;
		margin: 0rpx auto;
		margin-bottom: 80rpx;
		color: #fff;
		background: linear-gradient(to right, #00C9FF, #0076FF);
		border-radius: 50rpx;
		font-size: 35rpx;
	}
</style>