<template>
	<xn-form-container
		:title="formData.id ? '编辑过磅记录' : '增加过磅记录'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="关联本地数据ID：" name="relationId">
				<a-input v-model:value="formData.relationId" placeholder="请输入关联本地数据ID" allow-clear />
			</a-form-item>
			<a-form-item label="车牌号码：" name="licensePlate">
				<a-input v-model:value="formData.licensePlate" placeholder="请输入车牌号码" allow-clear />
			</a-form-item>
			<a-form-item label="车牌颜色：" name="plateColor">
				<a-input v-model:value="formData.plateColor" placeholder="请输入车牌颜色" allow-clear />
			</a-form-item>
			<a-form-item label="车次编码：" name="carNumber">
				<a-input v-model:value="formData.carNumber" placeholder="请输入车次编码" allow-clear />
			</a-form-item>
			<a-form-item label="过毛时间：" name="grossTime">
				<a-date-picker v-model:value="formData.grossTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择过毛时间" style="width: 100%" />
			</a-form-item>
			<a-form-item label="过皮时间：" name="tareTime">
				<a-date-picker v-model:value="formData.tareTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择过皮时间" style="width: 100%" />
			</a-form-item>
			<a-form-item label="皮重类型：" name="tareType">
				<a-input v-model:value="formData.tareType" placeholder="请输入皮重类型" allow-clear />
			</a-form-item>
			<a-form-item label="毛量：" name="grossWeight">
				<a-input v-model:value="formData.grossWeight" placeholder="请输入毛量" allow-clear />
			</a-form-item>
			<a-form-item label="皮重：" name="tareWeight">
				<a-input v-model:value="formData.tareWeight" placeholder="请输入皮重" allow-clear />
			</a-form-item>
			<a-form-item label="净重：" name="netWeight">
				<a-input v-model:value="formData.netWeight" placeholder="请输入净重" allow-clear />
			</a-form-item>
			<a-form-item label="矿发重量：" name="kfWeight">
				<a-input v-model:value="formData.kfWeight" placeholder="请输入矿发重量" allow-clear />
			</a-form-item>
			<a-form-item label="司机姓名：" name="driverName">
				<a-input v-model:value="formData.driverName" placeholder="请输入司机姓名" allow-clear />
			</a-form-item>
			<a-form-item label="司机电话：" name="driverMobile">
				<a-input v-model:value="formData.driverMobile" placeholder="请输入司机电话" allow-clear />
			</a-form-item>
			<a-form-item label="货品名称：" name="goodsName">
				<a-input v-model:value="formData.goodsName" placeholder="请输入货品名称" allow-clear />
			</a-form-item>
			<a-form-item label="规格型号：" name="goodsModel">
				<a-input v-model:value="formData.goodsModel" placeholder="请输入规格型号" allow-clear />
			</a-form-item>
			<a-form-item label="收货单位：" name="receiptCompany">
				<a-input v-model:value="formData.receiptCompany" placeholder="请输入收货单位" allow-clear />
			</a-form-item>
			<a-form-item label="发货单位：" name="shippingCompany">
				<a-input v-model:value="formData.shippingCompany" placeholder="请输入发货单位" allow-clear />
			</a-form-item>
			<a-form-item label="运输路线：" name="transportCompany">
				<a-input v-model:value="formData.transportCompany" placeholder="请输入运输路线" allow-clear />
			</a-form-item>
			<a-form-item label="运输单位：" name="transportRoute">
				<a-input v-model:value="formData.transportRoute" placeholder="请输入运输单位" allow-clear />
			</a-form-item>
			<a-form-item label="过毛车牌图片：" name="grossPlateName">
				<a-input v-model:value="formData.grossPlateName" placeholder="请输入过毛车牌图片" allow-clear />
			</a-form-item>
			<a-form-item label="过毛车图片：" name="grossLicenseName">
				<a-input v-model:value="formData.grossLicenseName" placeholder="请输入过毛车图片" allow-clear />
			</a-form-item>
			<a-form-item label="过毛车头相机抓拍：" name="grossCaptureHead">
				<a-input v-model:value="formData.grossCaptureHead" placeholder="请输入过毛车头相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="过毛车尾相机抓拍：" name="grossCaptureTail">
				<a-input v-model:value="formData.grossCaptureTail" placeholder="请输入过毛车尾相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="过毛车仓相机抓拍：" name="grossCaptureWare">
				<a-input v-model:value="formData.grossCaptureWare" placeholder="请输入过毛车仓相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="过毛车身相机抓拍：" name="grossCaptureBody">
				<a-input v-model:value="formData.grossCaptureBody" placeholder="请输入过毛车身相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="过毛磅房相机抓拍：" name="grossCapturePoundRoom">
				<a-input v-model:value="formData.grossCapturePoundRoom" placeholder="请输入过毛磅房相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="过皮车牌图片：" name="tarePlateName">
				<a-input v-model:value="formData.tarePlateName" placeholder="请输入过皮车牌图片" allow-clear />
			</a-form-item>
			<a-form-item label="过皮车图片：" name="tareLicenseName">
				<a-input v-model:value="formData.tareLicenseName" placeholder="请输入过皮车图片" allow-clear />
			</a-form-item>
			<a-form-item label="过皮车头相机抓拍：" name="tareCaptureHead">
				<a-input v-model:value="formData.tareCaptureHead" placeholder="请输入过皮车头相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="过皮车尾相机抓拍：" name="tareCaptureTail">
				<a-input v-model:value="formData.tareCaptureTail" placeholder="请输入过皮车尾相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="过皮车仓相机抓拍：" name="tareCaptureWare">
				<a-input v-model:value="formData.tareCaptureWare" placeholder="请输入过皮车仓相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="过皮车身相机抓拍：" name="tareCaptureBody">
				<a-input v-model:value="formData.tareCaptureBody" placeholder="请输入过皮车身相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="过皮磅房相机抓拍：" name="tareCapturePoundRoom">
				<a-input v-model:value="formData.tareCapturePoundRoom" placeholder="请输入过皮磅房相机抓拍" allow-clear />
			</a-form-item>
			<a-form-item label="备注1：" name="extKey1">
				<a-input v-model:value="formData.extKey1" placeholder="请输入备注1" allow-clear />
			</a-form-item>
			<a-form-item label="备注2：" name="extKey2">
				<a-input v-model:value="formData.extKey2" placeholder="请输入备注2" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizRecordForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizRecordApi from '@/api/biz/bizRecordApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		relationId: [required('请输入关联本地数据ID')],
		licensePlate: [required('请输入车牌号码')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizRecordApi
					.bizRecordSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
