<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 {
		getPlateColorText
	} from '../../static/data/typeData.js'
	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'
	import {
		BankTypeEnum
	} from '../../static/data/enums.js'


	let config = ({
		submitName: '快捷支付签约',
		titleWidth: 160
	})
	let formData = reactive([]);
	let szjhindex = '';
	let smsCodeSeq = '';
	let signId = ''
	let vehiclePlateColor = uni.getStorageSync(storageKey.vehicleColour);
	
	let signChannel = uni.getStorageSync(storageKey.signChannel);
	if (signChannel.channelCode == BankTypeEnum.MSB.code) {//民生
		var signObj = [{
				'title': '车牌号',
				'type': 2,
				'value': 'vehiclePlate',
				'required': true,
				'disabled': true,
				'vehiclePlate': uni.getStorageSync(storageKey.vehicle),
			},
			{
				'title': '车牌颜色',
				'type': 2,
				'value': 'vehiclePlateColorStr',
				'required': true,
				'disabled': true,
			}];
		formData = formData.concat(signObj);
		getPlateColorText(uni.getStorageSync(storageKey.vehicleColour), (res: any) => {
			let vehiclePlateColorIdx = Index('车牌颜色', formData);
			formData[vehiclePlateColorIdx][formData[vehiclePlateColorIdx].value] = res;
			vehiclePlateColor = uni.getStorageSync(storageKey.vehicleColour);
		})
		
	} else if (signChannel.channelCode == BankTypeEnum.SZCB.code) {//交行
		let signObj = [{
				'title': '手机号',
				'type': 2,
				'value': 'tel',
				'required': true,
				'disabled': true
			},
			{
				'title': '短信验证码',
				'type': 11,
				'value': 'smsCode',
				'required': true,
				'hint': '请输入短信验证码'
			}
		];
		formData = formData.concat(signObj);
		szjhindex = Index('手机号', formData);
	}

	onLoad((res) => {
		signId = res.signId
		if (signChannel.channelCode != BankTypeEnum.MSB.code) {//不是民生需要发送短信
			formData[szjhindex][formData[szjhindex].value] = res.tel;
			// formData[szjhindex][formData[szjhindex].value] = res.tel.replace(/(\d{3})\d*(\d{4})/, '$1****$2')
		}
		
	})
	//发送短信
	function sendText(e: any, item: TypeData) {
		if (!formData[szjhindex][formData[szjhindex].value]) {
			uni.showToast({
				title: '手机号不能为空',
				icon: 'none',
				duration: 2000
			});
			return
		}
		//发送短信
		request('IF01001202209060889', {
			data: {
				signId: signId,
				tel: formData[szjhindex][formData[szjhindex].value],
			}
		}).then((res) => {
			smsCodeSeq = res.rd.data.smsCodeSeq
		})
	}

	function submit(item: any) {
		console.log('提交内容', item);
			
		if(signChannel.channelCode == BankTypeEnum.SZCB.code) {//不是民生银行
			if (!smsCodeSeq) {
				uni.showToast({
					title: '请先获取短信验证码',
				})
				return
			}
			item.smsCodeSeq = smsCodeSeq
		}
 		
		
		item.signId = signId
		item.vehiclePlateColor = vehiclePlateColor;
		//快捷支付签约
		request('IF01001202209060890', {
			data: item
		}).then((res) => {
			if(signChannel.channelCode == BankTypeEnum.SZCB.code) {//深圳交通银行
				//添加车辆信息
				uni.navigateTo({
					url: '/pages/drivingLicense/drivingLicense?userIdType=' + uni.getStorageSync(storageKey
						.userIdType) + '&userIdNum=' + uni.getStorageSync(storageKey.userIdNum),
					animationType: 'pop-in',
					animationDuration: 500
				})
			} else if (signChannel.channelCode == BankTypeEnum.MSB.code) {//民生银行
				//前往代扣签约界面
				uni.navigateTo({
					url: '/pages/sigManage/sigWithhold?signId=' + signId + '&tel=' + item.tel,
					animationType: 'pop-in',
					animationDuration: 500
				})
			}
			
		}).catch((err)=>{
			console.log(err);
			let errData = err;
			if(errData.rm && errData.rm.indexOf("已快捷支付签约，请勿重复操作") != -1) {
				if(signChannel.channelCode == BankTypeEnum.SZCB.code) {//深圳交通银行
					//添加车辆信息
					uni.navigateTo({
						url: '/pages/drivingLicense/drivingLicense?userIdType=' + uni.getStorageSync(storageKey
							.userIdType) + '&userIdNum=' + uni.getStorageSync(storageKey.userIdNum),
						animationType: 'pop-in',
						animationDuration: 500
					})
				} else if (signChannel.channelCode == BankTypeEnum.MSB.code) {//民生银行
					//前往代扣签约界面
					uni.navigateTo({
						url: '/pages/sigManage/sigWithhold?signId=' + signId + '&tel=' + item.tel,
						animationType: 'pop-in',
						animationDuration: 500
					})
				}
			}
			
		})
	}
</script>
<style lang='scss' scoped>

</style>
