<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 v-if="!formData.depotGrainName" label="企业客户：" name="depotId">
				<a-select v-model:value="formData.depotId" placeholder="请选择客户" :options="depotIdOptions"/>
			</a-form-item>
			<a-form-item label="平均价格：" name="avgPrice">
				<a-input-number v-model:value="formData.avgPrice" placeholder="请输入平均价格" :min="0.01" style="width:100%" allow-clear/>
			</a-form-item>
			<a-form-item label="平均价格(颜色等级)：" name="warnPriceLevel">
				<a-select v-model:value="formData.warnPriceLevel" placeholder="请选择" :options="[{label:'黄',value:'黄'},{label:'红',value:'红'}]"/>
			</a-form-item>
			<a-form-item label="贷款到期日：" name="loanEndDate">
				<a-date-picker v-model:value="formData.loanEndDate" value-format="YYYY-MM-DD" style="width:100%"/>
			</a-form-item>
			<a-form-item label="贷款到期日(颜色等级)：" name="warnEndDateLevel">
				<a-select v-model:value="formData.warnEndDateLevel" placeholder="请选择" :options="[{label:'黄',value:'黄'},{label:'红',value:'红'}]"/>
			</a-form-item>
			<a-form-item label="主流价格：" name="morePrice">
				<a-input-number v-model:value="formData.morePrice" placeholder="请输入主流价格" :min="0.01" style="width:100%" allow-clear/>
			</a-form-item>
			<a-form-item label="平均质押价超价(%)：" name="avgPriceOverScale">
				<a-input-number v-model:value="formData.avgPriceOverScale" placeholder="请输入平均质押价超价" :min="0.01" style="width:100%" allow-clear/>
			</a-form-item>
			<a-form-item label="到期日剩余天数：" name="endDateCount">
				<a-input-number v-model:value="formData.endDateCount" placeholder="请输入到期日剩余天数" :min="0" style="width:100%" 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>
	</a-drawer>
</template>

<script setup name="depotGrainInfoWarnForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import depotGrainInfoWarnApi from '@/api/biz/depotGrainInfoWarnApi'
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 onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	} else {
		dictOptionsApi.options(Object.assign('', {'dictValue': '粮库'})).then((data) => {
			depotIdOptions.value = data
		})
	}
}
// 关闭抽屉
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)
			depotGrainInfoWarnApi
				.depotGrainInfoWarnSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
