<template>
	<div>
		<custom-header title="车辆信息" :back="config.hasReturn === 'true'"></custom-header>
		<div style="padding: 20rpx 0" v-if='isShow'>
			<form-builder-vue :formData.value="formData" :config="config" @uploadImgOcr="uploadImg" @submit="submit">
			</form-builder-vue>
		</div>
	</div>

</template>
<script setup lang='ts'>
	import {
		unifyTemplate
	} from '@/hooks/unifyTemplate'
	// import Agreement from '@/components/Agreement.vue';
	import {
		TypeData,
		Index
	} from '@/components/form-builder/tools'
	// import {
	// 	IdCard
	// } from '../../static/data/testData.js'
	import {
		reactive,
		ref,
		nextTick
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		request
	} from '@/utils/network/request'
	import ocrHandle from './ocrHandle'
	import fromData from './fromData.js'
	const {
		formBuilderVue, //组件
		CustomHeader, //头部组件
		initData, //初始化数据
		isShow, //是否展示formBuilderVue组件
		qdOrderVal //qdOrder中数据 ref
	} = unifyTemplate() //初始化数据

	onLoad((opin) => {
		// 初始化数据
		initData(opin, 2).then(data => {
			formData.value = JSON.parse(data.config.tableConfig)
			nextTick(() => {
				submitQuery.channelId = data.qdOrder.promoterChannelNum + '00000001'
				if (qdOrderVal.value.vanType == 1) {
					// 客车
					checkCarTpe(true)
					submitQuery.type = 1
				} else {
					// 货车
					checkCarTpe(false)
					isTractorHandle(data.qdOrder.isTractor)
					submitQuery.type = 2
				}
				isShow.value = true
			})
			delete data.config.tableConfig
			config.value = Object.assign(data.config, config.value)
		})
	})

	function isTractorHandle(isTractor: number) {
		const index = Index('车辆用户类型', formData.value);
		if (isTractor === 0) {
			formData.value[index].disabled = false
		}
	}
	// 货车或者客车切换
	function checkCarTpe(key) {
		// true为客车，false为货车
		formData.value[Index('总质量(kg)', formData.value)].show = key
		formData.value[Index('整备质量(kg)', formData.value)].show = key
		formData.value[Index('核定载质量(kg)', formData.value)].show = key
		formData.value[Index('准牵引总质量(kg)', formData.value)].show = key
		formData.value[Index('车轴数', formData.value)].show = key
		const dataItem = formData.value[Index('车辆用户类型', formData.value)]
		dataItem.disabled = true
		dataItem[dataItem.value] = 0;
	}
	let config = ref < any > ({
		submitName: '下一步',
		titleWidth: 250,
		isHeader: true
	})
	let formData = ref < any > (fromData)



	// ocr图片处理和额外请求参数处理
	const {
		uploadImgHandle,
		submitQuery
	} = ocrHandle()

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

	function submit(item: any) {
		let vehicleColor = qdOrderVal.value.vehicleColor
		const dataItem = formData.value[Index('车牌号', formData.value)];
		let vehiclePlate = dataItem[dataItem.value]
		// console.log(qdOrderVal.value.vehiclePlate, vehiclePlate);
		submitQuery.vehicleId =
			vehiclePlate + "_" + vehicleColor;
		const orderId = qdOrderVal.value.qtOrderNo
		let data = {
			...item,
			orderId
		}
		for (let dataItem in submitQuery) {
			if (submitQuery[dataItem] || submitQuery[dataItem] === 0 && !data[dataItem]) {
				data[dataItem] = submitQuery[dataItem]
			}
		}
		let {
			commitInterface,
			jumpPage
		} = config.value
		request(commitInterface || '18', {
			data,
		}).then((res) => {
			if (res.statusCode === 0) {
				uni.navigateTo({
					url: '/' + jumpPage,
					animationType: 'pop-in',
					animationDuration: 500
				})

			}
		})
	}
</script>
<style lang='scss' scoped>

</style>