<template>
	<div style="padding-top: 20rpx">
		<form-builder-vue :formData="formData" :config="config" @submit="submit" @sendText="sendText" >
		</form-builder-vue>
	</div>
</template>
<script setup lang='ts'>
	import formBuilderVue from '@/components/form-builder/form-builder-vue3'
	import {
		ref,
		reactive
	} from 'vue'
	import {
		request
	} from '../../static/js/network/request'
	import {
		TypeData,
		Index
	} from '@/components/form-builder/tools'
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		storageKey
	} from '../../static/data/storageKey.js'
	


	let config = ({
		submitName: '代扣签约',
		titleWidth: 160
	})
	let formData = reactive([]);
	// let bestSignedInnerPath = ''; //绑定卡已签约入金汇路中最优汇路 00-入金汇路1（对应原行内代扣）， 70-入金汇路2（对应原他行代扣）， 20-入金汇路3（新增他行代扣，需要进行短信签约）
	let bestAvailableInnerPath = ''; //绑定卡所有可签约最优汇路 00-入金汇路1（对应原行内代扣） 70-入金汇路2（对应原他行代扣） 20-入金汇路3（新增他行代扣，需要进行短信签约)
	let signId = ''
	let index = Index('手机号', formData);

	let signObj = [{
			'title': '手机号',
			'type': 2,
			'value': 'tel',
			'required': true,
			'disabled': true, 
			'tel': '18985009834',
		},
		{
			'title': '短信验证码',
			'type': 11,
			'value': 'smsVerficationCode',
			'required': true,
			'hint': '请输入短信验证码',
			'maxlength': 6,
		}
	];
	formData = formData.concat(signObj);

	onLoad((res) => {
		if (res.signId) {
			signId = res.signId;
			formData[index][formData[index].value] = res.tel;
			// formData[index][formData[index].value] = res.tel.replace(/(\d{3})\d*(\d{4})/, '$1****$2')
		}

	})

	//发送短信
	function sendText(e: any, item: TypeData) {
		if (!formData[index][formData[index].value]) {
			uni.showToast({
				title: '手机号不能为空',
				icon: 'none',
				duration: 2000
			});
			return
		}
		//发送短信(入金汇路签约查询接口)
		request('IF01001202303200951', {
			data: {
				signId: signId,
				isSendSmsVerficationCode: "Y"
			}
		}).then((res) => {
			if (res.rd.data.bestSignedInnerPath) {//有值说明已经完成了代扣签约
				uni.showModal({
					title: '提示',
					content: "已完成代扣签约",
					showCancel:false,
					success: function(res) {
						if (res.confirm) {
							//添加车辆信息
							uni.navigateTo({
								url: '/pages/drivingLicense/drivingLicense?userIdType=' + uni.getStorageSync(storageKey
									.userIdType) + '&userIdNum=' + uni.getStorageSync(storageKey.userIdNum),
							});
						} 
					}
				});
				return;
			}
			bestAvailableInnerPath = res.rd.data.bestAvailableInnerPath;
		})
	}

	/**
	 * 提交
	 */
	function submit(item: any) {
		console.log('提交内容', item);
		if (!bestAvailableInnerPath) {
			uni.showToast({
				title: '请先获取短信验证码',
			})
			return
		}
		item.signId = signId;
		item.bankInnerPathID = bestAvailableInnerPath;
		//代扣签约
		request('IF01001202304200959', {
			data: item
		}).then((res) => {
			//添加车辆信息
			uni.navigateTo({
				url: '/pages/drivingLicense/drivingLicense?userIdType=' + uni.getStorageSync(storageKey
					.userIdType) + '&userIdNum=' + uni.getStorageSync(storageKey.userIdNum),
				animationType: 'pop-in',
				animationDuration: 500
			});
		})
	}
</script>
<style lang='scss' scoped>

</style>
