<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="alarmType">
				<a-radio-group v-model:value="formData.alarmType" placeholder="请选择异常分类" :options="[{label:'报警',value:'报警'},{label:'异常',value:'异常'}]"/>
			</a-form-item>
			<a-form-item label="标题：" name="alarmTitle">
				<a-input v-model:value="formData.alarmTitle" placeholder="请输入异常标题" allow-clear/>
			</a-form-item>
			<a-form-item label="进程编码：" name="flowType">
				<a-input v-model:value="formData.flowType" placeholder="请输入进程编码" 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>
		<p>※分类为报警时，车辆停止作业，需要人员手动处理车辆报警</p>
		<p>※此设置只针对磅房端的系统监控</p>
	</a-drawer>
</template>

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

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	} else {
		formData.value.alarmType = '报警'
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {
	alarmType: [required('请选择')],
	alarmTitle: [required('请输入')],
	flowType: [required('请输入')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			depotWorkAlarmSetApi
				.depotWorkAlarmSetSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
