<template>
	<div>
		<custom-header :back="false" title="邮寄"></custom-header>
		<div style="padding-top: 20rpx">
			<form-builder-vue :formData="formData" :config="config" @radioChange="radioChange" @submit="submit">
			</form-builder-vue>
		</div>
	</div>

</template>
<script setup lang='ts'>
	import CustomHeader from '@/components/CustomHeader'
	import formBuilderVue from '@/components/form-builder/form-builder-vue3'
	import {
		TypeData,
		Index
	} from '@/components/form-builder/tools'
	import {
		ref,
		reactive,
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app';
	// import {
	// 	request
	// } from '../../static/js/network/request'
	// import {
	// 	storageKey
	// } from '../../static/data/storageKey'
	import {
		request
	} from '@/utils/network/request'
	import {
		useQdOrderStore
	} from '@/stores/qdOrder.js'
	const qdOrderStore = useQdOrderStore()
	//获取页面配置
	onLoad((opin) => {
		let qdOrderNo: any
		if (opin && opin.qdOrderNo) {
			qdOrderNo = opin.qdOrderNo
			uni.setStorageSync('qdOrderNo', opin.qdOrderNo)
		}else{
			qdOrderNo = uni.getStorageSync('qdOrderNo')
		}
		// 拼接参数
		request('ac8756cc31eb4816b8eaeb71907ff9c1', {
			data: {
				qdOrderNo,
				pageStep: 0
			}
		}).then((res) => {
			let data = JSON.parse(res.bizContent)
			formData.value = JSON.parse(data.config.tableConfig)
			qdOrderStore.setQdOrder(data.qdOrder)
			delete data.config.tableConfig
			config.value = Object.assign(data.config, config.value)
		})
	})

	let config = ref < any > ({
		submitName: '提交',
		titleWidth: 160
	})
	let formData = ref([{
			'title': '是否邮寄',
			'type': 7,
			'value': 'whetherToMail',
			'postal': 1,
			'itemData': [{
				'checked': true,
				'value': '1',
				'name': '不邮寄'
			}, {
				'checked': false, //默认选中
				'value': '0',
				'name': '邮寄'
			}],
		}, {
			'required': false,
			'title': '收件人',
			'type': 2,
			'vertical': 1,
			'value': 'consignee',
			'underline': true,
			'show': true,

		}, {
			'required': false,
			'title': '收件人手机号',
			'type': 2,
			'value': 'consigneeTel',
			'hint': '请输入手机号',
			'underline': true,
			'show': true
		}, {
			'required': false,
			'title': '省市区',
			'type': 10,
			'value': 'region',
			'hint': '请选择省市区',
			'underline': true,
			'show': true
		}, {
			'required': false,
			'title': '详细地址',
			'type': 2,
			'value': 'address',
			'hint': '请输入详细地址',
			'underline': true,
			'show': true
		},
		{
			'required': false,
			'title': '邮政编码',
			'type': 2,
			'value': 'postalCode',
			'hint': '请输入邮政编码',
			'underline': true,
			'show': true
		},
	])

	// let index = Index('收件人手机号', formData)
	// formData[index][formData[index].value] = uni.getStorageSync('operatorId')
	// console.log(formData)
	//单项选择器
	function radioChange(e: any, item: TypeData) {
		if (item.title === '是否邮寄') {
			if (e.detail.value == '1') {
				formData.value[Index('收件人', formData.value)].show = true
				formData.value[Index('收件人手机号', formData.value)].show = true
				formData.value[Index('省市区', formData.value)].show = true
				formData.value[Index('详细地址', formData.value)].show = true
				formData.value[Index('邮政编码', formData.value)].show = true
			} else {
				formData.value[Index('收件人', formData.value)].show = false
				formData.value[Index('收件人手机号', formData.value)].show = false
				formData.value[Index('省市区', formData.value)].show = false
				formData.value[Index('详细地址', formData.value)].show = false
				formData.value[Index('邮政编码', formData.value)].show = false
			}
		}
	}

	function submit(e: any) {
		qdOrderStore.orderInfo.value = e
		let {
			userType,
			vanType,
			vehicleColor,
			vehiclePlate,
			qdOrderNo
		} = qdOrderStore.qdOrderVal
		qdOrderStore.qdOrderVal.type = vanType - 1
		const data = {
			userType,
			qdOrderNo,
			vehiclePlate,
			vehiclePlateColor: vehicleColor,
			type: vanType - 1,
			orderSource: 'H5',
			...e
		}
		// uni.navigateTo({
		// 	url: '/pages/userInfoUploading/userInfoUploading',
		// 	animationType: 'pop-in',
		// 	animationDuration: 500
		// })
		let {
			commitInterface,
			jumpPage
		} = config.value
		request(commitInterface || '3dc2df0ba7004fffbd97192cbaa66b6f', {
			data,
		}).then((res) => {
			if (res.statusCode === 0) {
				qdOrderStore.orderInfo.value.orderId = JSON.parse(res.bizContent).orderId
				relatedProducts(qdOrderStore.qdOrderVal.productCode, JSON.parse(res.bizContent).orderId)
				uni.navigateTo({
					url: '/' + jumpPage,
					animationType: 'pop-in',
					animationDuration: 500
				})
			}
		})

	}

	function relatedProducts(promoteId: string, orderId: string) {
		const data = {
			promoteId,
			orderId
		}
		return request('198172b1858d474fa8e35225d65014d7', {
			showLoading: true,
			data,
		}).then((res) => {
			console.log('关联成功', res)
			return
		})
	}
</script>
<style lang='scss' scoped>

</style>