<template></template>
<script setup lang="ts">
	import { unifyTemplate } from '@/hooks/unifyTemplate';
	import { onLoad } from '@dcloudio/uni-app';
	import { request, requestNew } from '@/utils/network/request';
	import { setItem, setToken, StorageKeys } from '@/utils/storage';
	const {
		initData, //初始化数据
		qdOrderVal //qdOrder中数据 ref
	} = unifyTemplate(); //初始化数据

	//获取页面配置
	onLoad((opin) => {
		initData(opin, 3).then(async (data) => {
			await login(data);
			if (parseInt(data.order.isValueCard) === 1) {
				// 1-储值，2-记账
				handleGoods(data.order.orderNo, data.config.jumpPage);
			} else {
				contractTesting(data.order.orderNo, data.config.jumpPage);
			}
		});
	});

	// 推送
	function handleGoods(orderNo, jumpPage) {
		uni.navigateTo({
			url: '/' + jumpPage,
			animationType: 'pop-in',
			animationDuration: 500
		});
	}
	// 签约检测
	function contractTesting(orderId, jumpPage) {
		const data = {
			orderNo: orderId
		};
		console.log(data, '签约检测');
		requestNew('/iaw/h5/order/web/signcheck', {
			data
		}).then((res) => {
			if (res.code === 0) {
				const data = res.data;
				console.log(data, '签约状态');
				if (
					data.signStatus === 1 &&
					data.accountStatus === 1 &&
					data.bindStatus === 1
				) {
					// 上面三值都为1才进行下一步，否则进行签约初始化
					if (!jumpPage) return;
					uni.navigateTo({
						url: '/' + jumpPage,
						animationType: 'pop-in',
						animationDuration: 500
					});
					/* 自定义返回 */
				} else {
					// 进行签约初始化
					signingInitialization(orderId);
				}
				// qdOrderStore.orderInfo.value.orderId = JSON.parse(res.bizContent).orderId
				// 通过检测
			}
		});
	}
	// 签约初始化
	function signingInitialization(qdOrderNo) {
		const data = {
			qdOrderNo
		};
		requestNew('/iaw/h5/order/web/signinit', {
			data
		}).then((res) => {
			if (res.code === 0) {
				const data = res.data;
				console.log(data);
				if (data.returnType === 0) {
					// 0-无需跳转，关闭H5即可 1-需跳转至返回url
					// const opened = window.open('about:blank', '_self');
					// opened.opener = null;
					// opened.close();
					closeWin();
				} else {
					// uni.navigateTo({
					// 	url: `/pages/sigManage/signContract?qdOrderNo=${qdOrderNo}&jumpPage=${data.returnUrl}`,
					// 	animationType: 'pop-in',
					// 	animationDuration: 500
					// })
					console.log(data,'签约初始化跳转');
					const local = window.location.protocol + '//' + window.location.host + location.pathname
					data.signUrl = data.signUrl.replace('https://qtzl.etcjz.cn/h5/', local)
					data.signUrl = data.signUrl.replace('https://qtzl.etcjz.cn/order/', local)
					window.open(data.signUrl, '_self');
					// window.open('https://qtzl.etcjz.cn/prohtmlproduct/#/pages/sigManage/signContract', '_self');
					// window.open('http://localhost:3000/#/pages/sigManage/signContract', '_self');
				}
			}
		});
	}
	import wx from 'weixin-js-sdk';
	// 关闭当前页面方法
	function closeWin() {
		// @ts-ignore
		wx.miniProgram.getEnv(function (res) {
			if (res.miniprogram) {
				// @ts-ignore
				wx.miniProgram.navigateTo({
					url: '/page/index/index'
				});
			} else {
				const opened = window.open('about:blank', '_self');
				opened.opener = null;
				opened.close();
			}
		});
	}


	// 无感登录,如果没有登录调用登录
	async function login(orderData) {

		let data = {
			userType: '',
			accountNumber: '',
			loginSource: '431bfdc8b9c645e0b293b85d6ce1948n',
		}
		if (orderData.order.userType === "PERSONAL_USER") {
			// 个人办理
			data.userType = 'PERSONAL'
			data.accountNumber = orderData.order.customerTel
		} else {
			// 单位办理
			data.userType = 'ENTERPRISE'
			data.accountNumber = orderData.order.customerIdnum
		}
		console.log('login请求参数', data);
		await requestNew('/iaw/portal/loginByNoPassword', {
			data,
		}).then((res) => {
			if (res.code === 0) {
				let data = res.data
				// 保存token
				setToken(data.accessToken)
				setItem(StorageKeys.OpenId, data.openId)
				console.log(data, '登录获取的数据')
				return data.openId;
			}
			// contractTesting(qdOrderVal.value.qdOrderNo, jumpPage)
		})
	}
</script>
<!-- 签约管理 -->
<style lang="scss" scoped>
	.sig-manage {
		height: 100vh;
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.container {
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	:deep(.u-checkbox__label) {
		margin: 0;
		margin-right: 5rpx;
	}

	.check-content {
		flex: 1 1 auto;
		position: relative;

		.posi {
			position: absolute;
			padding: 20rpx;
			bottom: 30rpx;
			left: 0;
			width: 100%;
		}
	}

	.content {
		display: inline;
		word-wrap: break-word;
	}

	.item-line {
		color: $u-type-primary;
		cursor: pointer;
	}

	.btn {
		height: 80rpx;
		opacity: 1;
		border-radius: 100rpx;
		margin: 20rpx;
	}

	.btn-text {
		color: #ffffff;
		font-size: 28rpx;
	}

	.nav-bg {
		/* background: linear-gradient(to right, #13E7C1, #43A1E0); */
		border: 1px solid #ffffff;
		background: #1aac1b;
	}

	.btn-buttom {
		flex: 0 0 auto;
	}

	.slot-content {
		height: 82vh;
		overflow-y: auto;
	}
</style>