<template>
	<view class='bg-content'>
		<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="config.hasReturn === 'true'"></custom-header>
			<view style="padding: 20rpx 0" v-if='isShow'>
				<form-builder-vue @radioChange="radioChange" :formData.value="formData" :config="config"
					@uploadImgOcr="uploadImg" @submit="submit">
				</form-builder-vue>
			</view>
		</view>
	</view>
</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 ocrHandle from './ocrHandle'
	import fromData from './fromData.js'
	import {
		requestNew,
		request
	} from '@/utils/network/request'
	const {
		formBuilderVue, //组件
		CustomHeader, //头部组件
		initData, //初始化数据
		isShow, //是否展示formBuilderVue组件
		qdOrderVal //qdOrder中数据 ref
	} = unifyTemplate() //初始化数据

	onLoad((opin) => {
		// 初始化数据
		initData(opin, 2).then(data => {
			formData.value = JSON.parse(data.config.tableConfig)
			console.log(formData.value, 'formData.value')
			nextTick(() => {
				submitQuery.channelId = data.order.channelId
				if (qdOrderVal.value.vanType == 1) {
					// 客车
					checkCarTpe(true)
					submitQuery.type = 1
				} else if (qdOrderVal.value.vanType == 2) {
					// 货车
					checkCarTpe(false)
					isTractorHandle()
					submitQuery.type = 2
				} else {
					// 专项作业车
					submitQuery.type = 3
					checkCarTpe(false)
					isTractorHandle()
				}
				isShow.value = true
			})
			delete data.config.tableConfig
			config.value = Object.assign(data.config, config.value)
			nextTick(() => {
				handleHide()
			})
		})
	})

	function isTractorHandle() {
		const index = Index('车辆用户类型', formData.value);
		// if (isTractor === 0) {
		formData.value[index].disabled = false
		console.log(formData.value[index], '货车111')
		// }
	}
	//单项选择器
	function radioChange(e: any, item: TypeData) {
		if (item.title === '是否牵引车') {
			if (parseInt(e.detail.value) == 1) {
				formData.value[Index('道路运输证', formData.value)].show = false;
			} else {
				formData.value[Index('道路运输证', formData.value)].show = true;
			}
		}
	}
	//有哪些字段隐藏哪些字段
	function handleHide() {
		if (formData.value[Index('道路运输证', formData.value)]) {
			formData.value[Index('道路运输证', formData.value)].show = true;
		}
	}
	// 货车或者客车切换
	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
		if (formData.value[Index('道路运输证', formData.value)]) {
			formData.value[Index('道路运输证', formData.value)].show = key
			formData.value[Index('是否牵引车', formData.value)].show = key
		}

		// formData.value[Index('车轴数', formData.value)].show = key
		if (formData.value[Index('车头照', formData.value)]) {
			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,
		ocrDataFu
	} = ocrHandle()

	function uploadImg(fileList, item) {
		uploadImgHandle(fileList, item, formData)
	}
	let isLock = false

	function submit(item: any) {
		if (isLock) return
		isLock = true
		let vehicleColor = qdOrderVal.value.vehiclePlateColor || 0
		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.orderNo
		let data = {
			...item,
			orderId,
			vanType: qdOrderVal.value.vanType
		}
		for (let dataItem in submitQuery) {
			if (submitQuery[dataItem] || submitQuery[dataItem] === 0 && !data[dataItem]) {
				data[dataItem] = submitQuery[dataItem]
			}
		}
		ocrDataFu.forEach(ele => {
			const itemValue = data[ele.sendKey]
			if (itemValue) {
				if (ele.isNumbers) {
					data[ele.sendKey] = parseInt(itemValue)
				} else if (ele.sendKey === 'vehicleDimensions') {
					data[ele.sendKey] = itemValue.replace('mm', '').trim()
				}
			}

		})
		data.axleCount = parseInt(data.axleCount)
		data.character = 2
		data.registerDate = data.register
		data.tractorSign = data.tractorSign ? parseInt(data.tractorSign) : 0
		console.log('请求参数drivingLicense', data);
		let {
			commitInterface,
			jumpPage
		} = config.value

		let options = {
			type: 2,
			data: data,
			method: 'POST',
			showLoading: true
		}
		requestNew('/iaw/issue/vehicle/vehicleUpload', options).then((res) => {
			uni.navigateTo({
				url: '/' + jumpPage,
				animationType: 'pop-in',
				animationDuration: 500
			})
		}).finally(() => {
			isLock = false
		})
		// request(commitInterface || '18', {
		// 	data,
		// }).then((res) => {
		// 	if (res.statusCode === 0) {
		// 		uni.navigateTo({
		// 			url: '/' + jumpPage,
		// 			animationType: 'pop-in',
		// 			animationDuration: 500
		// 		})

		// 	}
		// }).finally(()=>{
		// 	isLock = false
		// })
	}
</script>
<style lang='scss' scoped>

</style>