<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-card title="预约信息 (请选择车牌号码)" :bordered="false">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="企业客户：" name="depotGrainName">
							<a-input v-model:value="formData.depotGrainName" disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="出入库：" name="depotBizType">
							<a-input v-model:value="formData.depotBizType" placeholder="" disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="粮食种类：" name="grainGroup">
							<a-input v-model:value="formData.grainGroup" placeholder="" disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="车牌号码：" name="licensePlate">
							<a-select v-model:value="formData.licensePlate" placeholder="" @change="putLicensePlate" allow-clear
									  :options="licensePlateOptions"/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="后车牌：" name="licensePlateBack">
							<a-input v-model:value="formData.licensePlateBack" placeholder="" disabled allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="粮食类型：" name="grainType">
							<a-input v-model:value="formData.grainType" placeholder="" disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="司机姓名：" name="driverName">
							<a-input v-model:value="formData.driverName" placeholder="" allow-clear disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="司机电话：" name="driverMobile">
							<a-input v-model:value="formData.driverMobile" placeholder="" allow-clear disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="预约编号：" name="truckBookId">
							<a-input v-model:value="formData.truckBookId" placeholder="" disabled/>
						</a-form-item>
					</a-col>
				</a-row>
			</a-card>
			<a-card title="扦样信息" :bordered="false">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="水份(%)：" name="samplingShuiFen">
							<a-input-number v-model:value="formData.samplingShuiFen" placeholder="请输入水份" :min="1" style="width:100%" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="容重：" name="samplingRongZhong">
							<a-input v-model:value="formData.samplingRongZhong" placeholder="请输入容重" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="不完善率：" name="samplingQueShao">
							<a-input v-model:value="formData.samplingQueShao" placeholder="请输入不完善率" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="霉变：" name="samplingMeiBian">
							<a-input v-model:value="formData.samplingMeiBian" placeholder="请输入霉变" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="杂质：" name="samplingZaZhi">
							<a-input v-model:value="formData.samplingZaZhi" placeholder="请输入杂质" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="价格(元/吨)：" name="samplingPrice">
							<a-input-number v-model:value="formData.samplingPrice" placeholder="请输入价格" :min="0.01" style="width:100%" allow-clear/>
						</a-form-item>
					</a-col>
				</a-row>
			</a-card>
			※ 保存时，车辆进程将会同时完成3个节点：已至扦样区、完成扦样、离开扦样区。
		</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="depotSamplingForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import depotSamplingApi from '@/api/biz/depotSamplingApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
import depotTruckBookApi from "@/api/biz/depotTruckBookApi";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const depotIdOptions = ref([])
let depotTruckBookData = []
const licensePlateOptions = ref([])

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	} else {
		depotTruckBookApi.depotTruckBookPage({size: -1, bizFlowName: '增加扦样'}).then((data) => {
			depotTruckBookData = data.records
			licensePlateOptions.value = depotTruckBookData.map((item) => {
				return {
					value: item['truckBookId'],
					label: item['licensePlate']
				}
			})
		})
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	depotTruckBookData = []
	visible.value = false
}
// 默认要校验的
const formRules = {
	depotGrainName: [required('请输入')],
	depotBizType: [required('请输入')],
	grainGroup: [required('请输入')],
	driverName: [required('请输入')],
	driverMobile: [required('请输入')],
	grainType: [required('请输入')],
	licensePlate: [required('请输入')],
	plateColor: [required('请输入')],
	samplingShuiFen: [required('请输入')],
	samplingPrice: [required('请输入')],
}
// 预约选择
const putLicensePlate = () => {
	depotTruckBookData.map((item) => {
		if (formData.value.licensePlate === item['truckBookId']) {
			formData.value = Object.assign({}, item)
			dictOptionsApi.options({'dictValue': '库位', depotId: formData.value.depotId}).then((data) => {
				depotShedOptions.value = data
			})
			return
		}
	})
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			depotSamplingApi
				.depotSamplingSubmitForm(formDataParam, true)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
