<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="appointmentType">
				<a-radio-group button-style="solid" v-model:value="formData.appointmentType">
					<a-radio-button v-for="appointmentItem in appointmentClassifyOptions" :key="appointmentItem.value" :value="appointmentItem.value" >
						{{ appointmentItem.label }}
					</a-radio-button>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="免费停留时长(分钟)：" name="freeParkTime">
				<a-input-number v-model:value="formData.freeParkTime" :min="0" style="width: 100%;" placeholder="请输入免费停留时长" allow-clear />
			</a-form-item>
			<a-form-item label="超出费用(元/小时)：" name="exceedAmount">
				<a-input-number v-model:value="formData.exceedAmount" :min="0" style="width: 100%;" placeholder="请输入超出每小时费用" allow-clear />
			</a-form-item>
			<a-form-item label="支付后停留时长(分钟)：" name="payAfterParkTime">
				<a-input-number v-model:value="formData.payAfterParkTime" :min="0" style="width: 100%;" 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>
	</xn-form-container>
</template>

<script setup name="bizParkConfigForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizParkConfigApi from '@/api/biz/bizParkConfigApi'
	import tool from '@/utils/tool'

	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

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

	// 预约分类
	const appointmentClassifyOptions = tool.dictList('appointment_classify')

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		appointmentType: [required('请选择预约类型')],
		freeParkTime: [required('请输入免费停留时长')],
		exceedAmount: [required('请输入超出每小时费用')],
		payAfterParkTime: [required('请输入支付后停留时长')], 
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizParkConfigApi
					.bizParkConfigSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
