<template>
	<div class="container">
		<u-modal v-model="show" :content="content"></u-modal>

		<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/zhizhao.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-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="gray-text">
				1. 运用企业、个体工商户、政府、事业单位、学校、组织等，账号归属企业。
			</view>
			<view class="gray-text">
				2.一个企业信息主体默认可认证1个账号。
			</view>
			<view class="gray-text">
				3.所有上传信息均会被妥善保管，不会用于其他商业用途或传输给其他第三方。
			</view>
		</view>
		<view style="display: flex;width: 100%;">
			<view class="finish-button" @click="goBack">
				上一步
			</view>
			<view class="finish-button" @click="next">
				下一步
			</view>
		</view>
	</div>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue'
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app';
	import {
		base_url
	} from '@/request/index.js'
	import {
		changemessage
	} from './api/addjigou.js'

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

	const nameArray = ["企业名称", "注册地址", "信用代码", "法人"];
	const textArray = reactive(["", "", "", ""]);

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

	// 拍照并上传
	function getMessage() {

		uni.chooseImage({
			count: 1,
			sourceType: ['camera'],
			success: chooseRes => {
				tempImagePath.value = chooseRes.tempFilePaths[0]
				// 拍照成功后，调用上传函数
				// 拍照成功后，调用上传函数
				uni.navigateTo({ url: `/compontent/public/camera?url=${chooseRes.tempFilePaths[0]}&type=0&size=1` });
				// uploadImage(tempImagePath.value)
			},
			fail: err => {
				console.error('拍照失败：', err)
			}
		})
	}

	const headImge = ref("");
	const backImge = ref("");
	const uping = ref(true);
	// 上传图片到服务器
	function uploadImage(filePath) {
		uni.showLoading()
		uping.value = false;
		uni.uploadFile({
			url: `${base_url}/api/ocr/businessLicense`, // 替换为你的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
				}
				console.log("营业执照", JSON.parse(JSON.parse(uploadRes.data).result.data).data)

				// if (JSON.parse(JSON.parse(uploadRes.data).result.data).data.face) {
				let father = JSON.parse(JSON.parse(uploadRes.data).result.data).data;
				textArray[0] = father.companyName;
				textArray[1] = father.businessAddress;
				textArray[2] = father.creditCode;
				textArray[3] = father.legalPerson;
				headImge.value = filePath;
				savephoto(filePath);
				uni.hideLoading()
				// 	textArray[4] = father.birthDate;
				// 	textArray[5] = father.address;
				// 	uni.showToast({
				// 		title: '识别成功',
				// 	})
				// 	
				// 	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()
			}
		})
	}
	const fontphoto = ref("");
	const savephoto = (filePath) => {
		uni.uploadFile({
			url: `${base_url}/sys/common/upload`, // 替换为你的POST接口地址
			filePath,
			name: 'file', // 后端接收时的字段名
			header: {
				'X-Access-Token': uni.getStorageSync('token') || '',
			},
			formData: {
				biz: `temp`
			},
			success: uploadRes => {
				fontphoto.value = JSON.parse(uploadRes.data).message;
				uping.value = true;
			},
			fail: err => {
				uni.showToast({
					title: '上传出错',
					icon: 'error'
				})
				uni.hideLoading()
			}
		})
	}
	const openLook = (res) => {
		if (res) {
			content.value = res;
			show.value = true
		}
	}
	const next = () => {
		if(!uping.value){
			return
		}
		const allNonEmpty = textArray[0];
		if (allNonEmpty) {
			let pushMessage = {
				id: uni.getStorageSync('specicalid') || "",
				tel: uni.getStorageSync('tel'),
				comName: textArray[0],
				comRegisterAddress: textArray[1],
				comCreditCode: textArray[2],
				comLegalPerson: textArray[3],
				comBusinessLicense:fontphoto.value,
			}
			// console.log("????",pushMessage)
			changemessage(pushMessage).then(res => {
				if(res.success){
					if(res.message==`保存成功！`){
						uni.navigateTo({
							url: "/pages/addjigou/where"
						});
					}else{
						uni.navigateTo({
							url: "/pages/addjigou/where"
						});
					}
					
				}else{
					uni.showToast({
						title: res.message,
						icon: 'error'
					})
				}
			})
		} else {
			uni.showToast({
				title: '请完善信息',
				icon: 'error'
			})
		}
	}
	onLoad(() => {
		if(uni.getStorageSync('baddata')){
			let data = uni.getStorageSync('baddata');
			if(data.comName){
				textArray[0] = data.comName;
				textArray[1] = data.comRegisterAddress;
				textArray[2] = data.comCreditCode;
				textArray[3] = data.comLegalPerson;
				if(data.comBusinessLicense){
					headImge.value = `${base_url}/sys/common/static/${data.comBusinessLicense}`;
					fontphoto.value = data.comBusinessLicense
				}
			}
		}
	})
	const goBack = () => {
		uni.navigateBack()
	}
	onShow(() => {
	  const img = uni.getStorageSync('imgkey0')
	  if (img) {
	    uploadImage(img)
	    uni.removeStorageSync('imgkey0')
	  }
	})
</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: 180rpx;
			// 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: 40rpx;
					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: 45%;
		height: 100rpx;
		margin: 0rpx auto;
		margin-bottom: 80rpx;
		color: #fff;
		background: linear-gradient(to right, #00C9FF, #0076FF);
		border-radius: 50rpx;
		font-size: 35rpx;
	}

	.gray-text {
		margin: 10rpx 0;
	}
</style>