<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="samplingShuiFen">
				<a-input-number v-model:value="formData.samplingShuiFen" placeholder="请输入水份" :min="0" style="width:100%" 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="samplingPrice">
				<a-input-number v-model:value="formData.samplingPrice" placeholder="请输入价格" :min="0.01" style="width:100%" allow-clear/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button type="primary" @click="onSubmit('')" :loading="submitLoading">扦样</a-button>
			<a-button type="primary" @click="onSubmit('扦样驳回')" danger style="margin-left: 8px" :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";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const depotIdOptions = ref([])
const depotOpApprovalStateOptions = ref([])
const bankOpApprovalStateOptions = ref([])

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		if (formData.value.samplingShuiFen === 0)
			formData.value.samplingShuiFen = ''
	} else {
		dictOptionsApi.options(Object.assign('', {'dictValue': '粮库'})).then((data) => {
			depotIdOptions.value = data
		})
	}
	depotOpApprovalStateOptions.value = tool.dictList('审批状态')
	bankOpApprovalStateOptions.value = tool.dictList('审批状态')
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {}
// 验证并提交数据
const onSubmit = (samplingType) => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			formData.value.samplingType = samplingType
			const formDataParam = cloneDeep(formData.value)
			depotSamplingApi
				.addSampling(formDataParam)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
