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

//获取页面配置
onLoad((opin) => {
	initData(opin, 3).then(async (data) => {
		await register(data.order);
		if (parseInt(data.order.isValueCard) === 1) {
			// 1-储值，2-记账
			handleGoods(data.qdOrder.qdOrderNo, data.config.jumpPage);
		} else {
			contractTesting(data.qdOrder.qdOrderNo, data.config.jumpPage);
		}
	});
});
// 无感登录,如果没有登录调用登录
async function login(orderData) {
	let data = {
		userType: '',
		account: ''
	};
	if (orderData.order.userType === 'PERSONAL_USER') {
		// 个人办理
		data.userType = 'PERSONAL';
		data.account = orderData.order.customerTel;
	} else {
		// 单位办理
		data.userType = 'ENTERPRISE';
		data.account = orderData.order.customerIdnum;
	}
	await request('abaf0013caa24dafad12b0f571e8ee40', {
		data
	}).then((res) => {
		if (res.statusCode === 0) {
			let bizContent = JSON.parse(res.bizContent);
			// 保存token
			setToken(bizContent.accessToken);
		}
		// contractTesting(qdOrderVal.value.qdOrderNo, jumpPage)
	});
}
// 推送
function handleGoods(qdOrderNo, jumpPage) {
	const data = {
		qdOrderNo
	};
	console.log(data, '推送请求参数ad8836c3ec25437b913059cf94d6c9ba');
	request('ad8836c3ec25437b913059cf94d6c9ba', {
		data
	}).then((res) => {
		if (res.statusCode === 0) {
			// 储值卡跳转下个页面
			uni.navigateTo({
				url: '/' + jumpPage,
				animationType: 'pop-in',
				animationDuration: 500
			});
		}
	});
}
// 签约检测
function contractTesting(qdOrderNo, jumpPage) {
	const data = {
		qdOrderNo
	};
	console.log(data,'签约检测');
	request('fdfbdddba6ec49f8b451cf2e299b4feb', {
		data
	}).then((res) => {
		if (res.statusCode === 0) {
			const data = JSON.parse(res.bizContent);
			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(qdOrderNo);
			}
			// qdOrderStore.orderInfo.value.orderId = JSON.parse(res.bizContent).orderId
			// 通过检测
		}
	});
}
// 签约初始化
function signingInitialization(qdOrderNo) {
	const data = {
		qdOrderNo
	};
	request('94569071cf814517908cd35693f1f2e1', {
		data
	}).then((res) => {
		if (res.statusCode === 0) {
			const data = JSON.parse(res.bizContent);
			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);
				window.open(data.signUrl, '_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();
		}
	});
}

// 无感注册拿到openId
function register(data: any) {
	let params: any = {};
	params.idNum = data.customerIdnum;
	params.idType = data.customerIdtype;
	params.mobile = data.customerTel || data.agentTel;
	params.userName = data.customerName;
	params.gender = data.customerGender || 'UNKOWN';
	params.certifyChannel = 'BAIDUOCR'; //实名渠道
	params.address = data.address || '默认地址';
	let userType = '';
	if (data.userType !== 'PERSONAL_USER') {
		// 企业 办理
		params.agentIdNum = data.agentIdNum;
		params.agentIdType = data.agentIdType;
		params.agentName = data.agentName;
		userType = 'ENTERPRISE';
		params.department = data.customerDepartment;
	}else{
		userType = 'PERSONAL';
	}
	params.userType = userType;
	console.log('请求参数',params);
	return request('52de4029940f4e03b18c3a0b8d64c2db', {
		data: params
	}).then((res) => {
		if (res.statusCode === 0) {
			let bizContent = JSON.parse(res.bizContent);
			// 保存token
			setToken(bizContent.accessToken);
			return bizContent.openId;
		}
	});
}
</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>
