<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="queueEfficiency">
				<a-input-number v-model:value="formData.queueEfficiency" 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="pullNumber">
				<a-input-number v-model:value="formData.pullNumber" 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="stopDuration">
				<a-input-number v-model:value="formData.stopDuration" 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="auditSwitch">
				<a-radio-group button-style="solid" v-model:value="formData.auditSwitch">
					<a-radio-button value="1">
						开启
					</a-radio-button>
					<a-radio-button value="2">
						关闭
					</a-radio-button>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="订单重量校验开关：" name="orderWeightSwitch">
				<a-radio-group button-style="solid" v-model:value="formData.orderWeightSwitch">
					<a-radio-button value="1">
						开启
					</a-radio-button>
					<a-radio-button value="2">
						关闭
					</a-radio-button>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="门禁强制校验开关：" name="accessControlSwitch">
				<a-radio-group button-style="solid" v-model:value="formData.accessControlSwitch">
					<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="bizConfigForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizConfigApi from '@/api/biz/bizConfigApi'
	// 抽屉状态
	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 onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}else{
			formData.value.auditSwitch = '2'
			formData.value.orderWeightSwitch = '2'
			formData.value.accessControlSwitch = '2'
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		queueEfficiency: [required('请输入排队时效')],
		pullNumber: [required('请输入时段下拉个数')],
		stopDuration: [required('请输入停留时长')],
		auditSwitch: [required('请输入预约审核开关（1：开启   2：关闭）')],
		orderWeightSwitch: [required('请输入订单重量校验开关（1：开启   2：关闭）')],
		accessControlSwitch: [required('请输入门禁强制校验开关')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizConfigApi
					.bizConfigSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
