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

	/**
	 * 简单签约流程
	 * 1.查询用户信息
	 * 2.上传影响资料(用户信息带入过来)
	 * 3.人脸识别
	 * 4.开户
	 * 5.快捷支付签约
	 * 6.代扣签约
	 */
	let signId = ''
	let uploadImageTradeNo = '' //影像件流水号
	let uploadLivingTradeNo = '' //活体识别流水号
	
	let smsCodeSeq = '' //短信序列号民生时必填
	let provinceCode = '' //"省编码" 民生时必填
	let cityCode = '' //市编码 民生时必填
	let countyCode = '' //区县编码 民生时必填
	
	let config = ({
		submitName: '开户',
		titleWidth: 160
	})
	let paramArr = [{
			'title': '居住地址',
			'type': 10,
			'vertical': 1,
			'value': 'areaName,cityName,district',
			'required': true,
		}, {
			'title': '职业编码',
			'type': 4,
			'value': 'workCode',
			'required': true,
			'name': 'professionCode',
			'itemKey': 'professionName',
			'itemData': []
		}, {
			'title': '网点号',
			'type': 2,
			'value': 'signNet'
		}, {
			'title': '一类银行卡号',
			'type': 2,
			'value': 'bankCardNo',
			'required': true
		},
		{
			'title': '开户手机号',
			'type': 2,
			'value': 'tel',
			'required': true
		},
		{
			'title': '短信验证码',
			'type': 11,
			'value': 'mobileCode',
			'required': true,
			'hint': '请输入短信验证码',
			'maxlength': 6,
		}
	];
	let formData = reactive(paramArr);
	let signChannel = uni.getStorageSync(storageKey.signChannel);
	if (signChannel.channelCode == BankTypeEnum.MSB.code) {
		let company = {
			'title': '公司名称',
			'type': 2,
			'inputType': 'text',
			'value': 'company',
			'hint': '请输入公司名称',
			'required': true,
			'maxlength': 50
		};
		formData.splice(2,0,company);
	} else if (signChannel.channelCode == BankTypeEnum.SZCB.code) {
		let userImageUrl = {
			'title': '人脸识别照片',
			'type': 8,
			'vertical': 2,
			'required': true,
			'placeholderImg1': '../../static/image/renlianzhao.png',
			'placeholderImg2': null,
			'inputType': 999, //(ocr识别)
			'value': 'userImageUrl',
			'style': 'width: 45%;',
			'hint1': '人脸照',
			'hint': '注意事项:且上传有效证件照且图片大小不超过2M'
		};
		formData.splice(0,0,userImageUrl);
	}
	let index = Index('开户手机号', formData);
	formData[index][formData[index].value] = uni.getStorageSync('operatorId');
	
	onLoad((res) => {
		

		console.log('输出内容123', uni.getStorageSync(storageKey.userInfo), res)
		let userInfo = JSON.parse(uni.getStorageSync(storageKey.userInfo));
		signId = res.signId
		//查询账户
		request('IF01001202209060888', {
			data: {
				signId: signId
			},
			showTitle: '查询账户信息'
		}).then((res) => {
			console.log('页面回调内容', res.rd.data)
			let item = res.rd.data
			userInfo.validDate = userInfo.validEndDate.replaceAll('-', '')
			userInfo.gender = getSex(userInfo.userIdNum)
			userInfo.signId = signId
			if (item.uploadImageTradeNo) {
				uploadImageTradeNo = item.uploadImageTradeNo
					
				if(item.bankSignStatus < 6) {
					// 未开户_0,开户成功_1,开户失败_2,开户中_3
					if(item.bankSignStatus == 5 &&  item.openAccountStatus == 3) {
						//开户中，请稍后重试
						uni.showModal({
							title: '提示',
							content: "正在开户中，请稍后重试",
							showCancel:false,
						});
						return;
					} else if (item.bankSignStatus == 5) {
						//开户失败，请重新开户
						uni.showModal({
							title: '提示',
							content: "开户失败，请重新开户",
							showCancel:false,
						});
						return;
					}
				} else {
					//去快支付签约
					uni.showModal({
						title: '提示',
						content: "已开户成功，请前往快支付签约",
						showCancel:false,
						success: function(res) {
							if (res.confirm) {
								let idx = Index('开户手机号', formData);
								let tel = formData[idx][formData[idx].value];
								uni.navigateTo({
									url: '/pages/sigManage/sigFastPayment?signId=' + signId + '&tel=' + tel,
								})
							} 
						}
					});
					
				}
			} else {
				//上传影像资料（银行侧的需要的用户信息）
				request('IF01001202209060884', {
					data: userInfo,
					showTitle: '影像资料上传..'
				}).then((res) => {
					//获取影响上传流水号
					uploadImageTradeNo = res.rd.data.uploadImageTradeNo
				})
			}
		})
	})
	// 选择省市区
	function handleGetRegion (values,item) {
		console.log(values);
		console.log(item);
		provinceCode = values[0].code;
		cityCode = values[1].code;
		countyCode = values[2].code;
	}
	
	// 选择职业
	function bindPickerChange (values,item) {
		console.log(values);
		console.log(item);
		if (item.workCode.professionCode == '82') {
			// 其他为82，需要手动输入
			let occupationRemark = {
				'title': '职业',
				'type': 2,
				'inputType': 'text',
				'value': 'occupationRemark',
				'hint': '请输入职业',
				'required': true,
				'maxlength': 10
			};
			let index = Index('职业编码', formData) + 1;
			formData.splice(index,0,occupationRemark);
		} else {
			let index = Index('职业', formData);
			console.log(index);
			if (index > -1) { // 移除找到的指定元素
			  formData.splice(index, 1); // 移除元素
			}
		}
	
	}

	getOccupationNumber((res: any) => {
		formData[Index('职业编码', formData)].itemData = res
	})
	/**
	 * @param idCard
	 */
	function getSex(idCard) {
		var sexStr = 1; //1男 2女
		if (parseInt(idCard.slice(-2, -1)) % 2 == 1) {
			sexStr = 1;
		} else {
			sexStr = 2;
		}
		return sexStr;
	}




	//发送短信
	function sendText(e: any, item: TypeData) {
		//telNo: uni.getStorageSync('operatorId') //手机号
		let idx = Index('开户手机号', formData);
		request('IF01001202209060886', {
			data: {
				signId: signId,
				tel: formData[idx][formData[idx].value],
			}
		}).then((res) => {
			smsCodeSeq = res.rd.data.smsCodeSeq;//民生银行专有
		})
	}

	//图像文件上传
	function uploadImg(data: any, item: TypeData, index: Number) {
		console.log('图片文件', data, item, index, '结果长度', Index('姓名', formData))
		//人脸识别
		if (item.title === '人脸识别照片') {
			request('IF01001202209060885', {
				data: {
					signId: signId,
					userImageUrl: item.placeholderImg1
				},
				showTitle: '人脸识别校验中...'
			}).then((res) => {
				uploadLivingTradeNo = res.rd.data.uploadLivingTradeNo
			})
		}
	}

	function submit(item: any) {
		item.signId = signId
		item.liveAddress = item.areaName + item.cityName + item.district
		item.uploadImageTradeNo = uploadImageTradeNo
		item.uploadLivingTradeNo = uploadLivingTradeNo
		item.smsCodeSeq = smsCodeSeq;
		item.provinceCode = provinceCode;
		item.cityCode = cityCode;
		item.countyCode = countyCode;
		console.log('提交内容', item);
		//开户
		request('IF01001202209060887', {
			data: item
		}).then((res) => {
			uni.navigateTo({
				url: '/pages/sigManage/sigFastPayment?signId=' + signId + '&tel=' + item.tel,
				animationType: 'pop-in',
				animationDuration: 500
			})
		})
	}
</script>
<style lang='scss' scoped>

</style>
