<template>
	<view>
		<image class='bg-image' mode="widthFix"
			src="https://qtzl.etcjz.cn/default-bucket/20240324/3eba2095f5204798a1f8101b_beijing.jpg"></image>
		<view class="content-re">
			<custom-header title="用户信息修改" :back="false"></custom-header>
			<view v-if="isShow">
				<formBuilderVue :formData="formData" :config="config" @radioChange="radioChange" @submit="submit"
					@uploadImgOcr="uploadImg"></formBuilderVue>
			</view>
		</view>
		<agreementfrom />
	</view>
</template>

<script setup lang='ts'>
	import {
		unifyTemplate
	} from '@/hooks/unifyTemplate'
	import {
		TypeData,
		Index
	} from '@/components/form-builder/tools'
	import agreementfrom from './components/agreement'
	import {
		ref,
		nextTick
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		request
	} from '@/utils/network/request'
	import fromData from './fromData'
	import ocrHandle from './ocrHandle'
	import {
		useAuditOrderStore
	} from '@/stores/auditOrder.js';
	import {
		setToken,
	} from '@/utils/storage'
	import {
		personal,
		personalAgent,
		company
	} from '@/datas/auditInfo'
	// import {
	// 	userInfoFormData
	// } from '@/utils/formDataHandle'
	const auditOrderStore = useAuditOrderStore();
	let config = ref < any > ({
		hasReturn: 'true',
		submitName: '确认',
		titleWidth: 180,
		isHeader: true,
	})
	let formData = ref < any > (fromData)

	const {
		formBuilderVue, //组件
		CustomHeader, //头部组件
		initData, //初始化数据
		isShow, //是否展示formBuilderVue组件
		qdOrderVal //qdOrder中数据 ref
	} = unifyTemplate() //初始化数据
	//获取页面配置
	onLoad((opin) => {
		// 初始化数据
		initData(opin, 1).then(data => {
			// formData.value = userInfoFormData(data.order, JSON.parse(data.config.tableConfig))
			formData.value = JSON.parse(data.config.tableConfig)
			nextTick(() => {
				if (data.order.userType === 'PERSONAL_USER') {
					// 个人办理
					danChange(false)
				} else {
					// 单位办理
					danChange(true)
				}
				handleData(data.order)
			})
			isShow.value = true
			console.log('新版', formData.value)
			delete data.config.tableConfig
			config.value = Object.assign(data.config, config.value)
		})
	})
	

	function handleData(orderInfo) {
		const isDan = orderInfo.userType !== 'PERSONAL_USER'
		let dataArr = []
		if (isDan) {
			// 单位办理
			dataArr = company
		} else {
			if (orderInfo.agentIdnum) {
				dataArr = personalAgent
				orderInfo.isSelf = '0' //他人办理
			} else {
				dataArr = personal
				orderInfo.isSelf = '1' //本人办理
			}
		}
		dataArr.forEach(item => {
			if (item.key) {
				const key = item.val
				let valData = orderInfo[item.key]
				if (item.title) {
					if (item.title === '本人办理') {
						formData.value[Index('本人办理', formData.value)].itemData.find(formItem => {
							if (formItem.value === valData) {
								formItem.checked = true
							} else {
								formItem.checked = false
							}

						})
						if (valData === '1') {
							businesses(true)
						} else {
							businesses(false)
						}
					}
					formData.value[Index(item.title, formData.value)][key] = valData
				} else {
					submitQuery.value[key] = valData
				}
			}
		})
		console.log(formData.value, submitQuery.value, '数据');
	}
	// function handleMobile(data) {
	// 	const itemData = formData.value[Index('联系方式', formData.value)]
	// 	if(data.qdOrder.mobile){
	// 		itemData[itemData.value] = data.qdOrder.mobile || ''
	// 		itemData.disabled = true
	// 	}
	// }
	//初始判断单位办理还是个人办理
	function danChange(key) {
		if (key) {
			// 单位办理
			businesses(false)
			handleDan(false)
			formData.value[Index('部门名称', formData.value)].department = '本部'
			submitCode = '20'

		} else {
			// 个人办理
			businesses(true)
			handleDan(true)
			submitCode = '22'
		}
	}
	//单项选择器
	function radioChange(e: any, item: any) {
		if (item.title === '本人办理') {
			if (e.detail.value === '1') {
				businesses(true)
			} else {
				businesses(false)
			}
		}
	}
	//本人办理还是他人办理 ,true为本人办理
	function businesses(key: Boolean) {
		formData.value[Index('委托书', formData.value)].show = key
		formData.value[Index('本人办理', formData.value)].underline = key
		formData.value[Index('经办人姓名', formData.value)].show = key
		formData.value[Index('经办人证件号码', formData.value)].show = key
		formData.value[Index('经办人电话', formData.value)].show = key
		formData.value[Index('经办人证件识别信息如下', formData.value)].show = key
		formData.value[Index('经办人证件人像面', formData.value)].show = key
		formData.value[Index('经办人证件国徽面', formData.value)].show = key
	}
	// 个人办理还是单位办理 true为个人办理
	function handleDan(key) {
		formData.value[Index('营业执照', formData.value)].show = key
		formData.value[Index('委托书', formData.value)].show = key
		formData.value[Index('公司名称', formData.value)].show = key
		formData.value[Index('部门名称', formData.value)].show = key
		formData.value[Index('社会信用代码', formData.value)].show = key
		// formData.value[Index('住址', formData.value)].show = !key
		formData.value[Index('本人办理', formData.value)].show = !key
		formData.value[Index('人像面', formData.value)].show = !key
		formData.value[Index('国徽面', formData.value)].show = !key
		formData.value[Index('证件号码', formData.value)].show = !key
		// formData.value[Index('住址', formData.value)].show = !key
		formData.value[Index('姓名', formData.value)].show = !key
		formData.value[Index('联系方式', formData.value)].show = !key
	}

	// 提交个人信息接口
	let submitCode = '22'
	// ocr图片处理和额外请求参数处理
	const {
		uploadImgHandle,
		submitQuery
	} = ocrHandle()

	function uploadImg(fileList, item) {
		uploadImgHandle(fileList, item, formData)
	}

	async function submit(item: any) {
		let data = {
			...submitQuery.value,
			orderId: qdOrderVal.value.qtOrderNo //订单id
		}
		// 处理参数，为空的不携带
		for (const key in item) {
			if (item[key]) {
				data[key] = item[key]
			}
		}
		const mobile = data.tel || data.agentTel
		if (isLock.value && isLock.value === mobile) {
			uni.showModal({
				title: '提示',
				content: '该证件号已经绑定了其他手机号，请联系客服进行处理!',
				showCancel: false,
				success: function(res) {
					if (res.confirm) {} else if (res.cancel) {}
				}
			});
			return
		}
		console.log(data, 'datas.datas');
		const openIdKey = await register(data)
		if (!openIdKey) {
			return
		}

		data.opId = openIdKey //操作人id
		console.log(data, '请求参数')
		let {
			jumpPage
		} = config.value
		console.log(data, submitCode)

		auditOrderStore.setUserData(data)
		uni.navigateTo({
			url: '/pages/audit/audit',
			animationType: 'pop-in',
			animationDuration: 500
		})

	}
	let isLock = ref(false)
	// 无感注册拿到openId
	function register(data: any) {
		let params: any = {}
		params.idNum = data.userIdNum;
		params.idType = data.userIdType;
		params.mobile = data.tel || data.agentTel;
		params.userName = data.userName;
		params.gender = 'UNKOWN';
		params.certifyChannel = 'BAIDUOCR'; //实名渠道
		params.address = data.address;

		if (data.isSelf === '0' || submitCode === '20') {
			// 他人办理
			params.agentIdNum = data.agentIdNum;
			params.agentIdType = data.agentIdType;
			params.agentName = data.agentName;
		}
		if (submitCode === '20') {
			// 单位办理
			params.userType = 'ENTERPRISE'
			params.department = data.department;
		} else {
			params.userType = 'PERSONAL'
		}
		console.log(params, 'params', submitCode);
		return request('52de4029940f4e03b18c3a0b8d64c2db', {
			data: params,
		}).then((res) => {
			if (res.statusCode === 0) {
				let bizContent = JSON.parse(res.bizContent)
				// 保存token
				setToken(bizContent.accessToken)
				isLock.value = false
				return bizContent.openId
			} else {
				if (res.errorMsg.includes('暂不支持绑定多个手机号')) {
					isLock.value = params.mobile
				}
				console.log(res.errorMsg, 'res.errorMsg', res);
				return false
			}
		})
	}
</script>
<style lang='scss' scoped>

</style>