<template>
	<xn-form-container
		:title="formData.id ? '编辑超限配置' : '增加超限配置'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" :wrapper-col="wrapperCol" :label-col="labelCol">
			<a-form-item label="车辆轴数：" name="vehicleAxleNumber">
				<a-input-number v-model:value="formData.vehicleAxleNumber" style="width:90%"  :precision="0" :min="1" :max="99999" placeholder="请输入车辆轴数" allow-clear /><span style="margin-left:10px;">个</span>
			</a-form-item>
			<a-form-item label="超限重量：" name="excessWeight">
				<a-input-number v-model:value="formData.excessWeight" style="width:90%"  :precision="0" :min="1" :max="999999" placeholder="请输入超限重量" allow-clear /><span style="margin-left:10px;">KG</span>
			</a-form-item>
			<a-form-item label="是否启用：" name="status">
				<a-radio-group button-style="solid" v-model:value="formData.status">
					<a-radio-button value="1">
						启用
					</a-radio-button>
					<a-radio-button value="2">
						关闭
					</a-radio-button>
				</a-radio-group>
			</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>
	</xn-form-container>
</template>

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

	//设置表单样式
	const labelCol = ref({ span: 5})
	const wrapperCol = ref({ span: 16})

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}else{
			formData.value.status = '2'
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		vehicleAxleNumber: [required('请输入车辆轴数')],
		excessWeight: [required('请输入超限重量')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizExcessConfigApi
					.bizExcessConfigSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
