<template>
	<a-drawer
		:title="formData.id ? '编辑人工补单' : '增加人工补单'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="企业客户：" name="depotId">
				<a-select v-model:value="formData.depotId" placeholder="请选择企业客户" :options="depotIdOptions"/>
			</a-form-item>
			<a-form-item label="预约ID：" name="truckBookId">
				<a-input v-model:value="formData.truckBookId" placeholder="请输入预约ID" allow-clear/>
			</a-form-item>
			<a-form-item label="出入库：" name="depotBizType">
				<a-select v-model:value="formData.depotBizType" placeholder="请选择出入库" :options="depotBizTypeOptions"/>
			</a-form-item>
			<a-form-item label="粮食种类：" name="grainGroup">
				<a-select v-model:value="formData.grainGroup" placeholder="请选择粮食种类" :options="grainGroupOptions"/>
			</a-form-item>
			<a-form-item label="粮食类型：" name="grainType">
				<a-select v-model:value="formData.grainType" placeholder="请选择粮食类型" :options="grainTypeOptions"/>
			</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="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="plateColor">
				<a-input v-model:value="formData.plateColor" 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="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="unitPrice">
				<a-input v-model:value="formData.unitPrice" placeholder="请输入单价(元/吨)" allow-clear/>
			</a-form-item>
			<a-form-item label="总价(元)：" name="totalPrice">
				<a-input v-model:value="formData.totalPrice" placeholder="请输入总价(元)" allow-clear/>
			</a-form-item>
			<a-form-item label="库位：" name="shedName">
				<a-input v-model:value="formData.shedName" placeholder="请输入库位" allow-clear/>
			</a-form-item>
			<a-form-item label="水份(%)：" name="samplingShuiFen">
				<a-input v-model:value="formData.samplingShuiFen" placeholder="请输入水份" allow-clear/>
			</a-form-item>
			<a-form-item label="容重：" name="samplingRongZhong">
				<a-input v-model:value="formData.samplingRongZhong" placeholder="请输入容重" allow-clear/>
			</a-form-item>
			<a-form-item label="不完善率：" name="samplingQueShao">
				<a-input v-model:value="formData.samplingQueShao" placeholder="请输入不完善率" allow-clear/>
			</a-form-item>
			<a-form-item label="霉变：" name="samplingMeiBian">
				<a-input v-model:value="formData.samplingMeiBian" placeholder="请输入霉变" allow-clear/>
			</a-form-item>
			<a-form-item label="杂质：" name="samplingZaZhi">
				<a-input v-model:value="formData.samplingZaZhi" placeholder="请输入杂质" allow-clear/>
			</a-form-item>
			<a-form-item label="审批状态：" name="bankOpApprovalState">
				<a-select v-model:value="formData.bankOpApprovalState" placeholder="请选择审批状态" :options="bankOpApprovalStateOptions"/>
			</a-form-item>
			<a-form-item label="审批时间：" name="bankOpApprovalTime">
				<a-date-picker v-model:value="formData.bankOpApprovalTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择审批时间"
							   style="width: 100%"/>
			</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>
	</a-drawer>
</template>

<script setup name="depotTruckWeighPlugForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import depotTruckWeighPlugApi from '@/api/biz/depotTruckWeighPlugApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const depotIdOptions = ref([])
const depotBizTypeOptions = ref([])
const grainGroupOptions = ref([])
const grainTypeOptions = ref([])
const bankOpApprovalStateOptions = ref([])

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		dictOptionsApi.options(Object.assign('', {'dictValue': '粮库'})).then((data) => {
			depotIdOptions.value = data
		})
	}
	depotBizTypeOptions.value = tool.dictList('出入库')
	grainGroupOptions.value = tool.dictList('粮食种类')
	grainTypeOptions.value = tool.dictList('粮食类型')
	bankOpApprovalStateOptions.value = tool.dictList('审批状态')
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			depotTruckWeighPlugApi
				.depotTruckWeighPlugSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
