<template>
	<xn-form-container
		:title="formData.id ? '编辑' + parentData.name : '增加' + parentData.name"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="名称：" name="configName">
				<a-input v-model:value="formData.configName" placeholder="请输入名字" allow-clear />
			</a-form-item>
			<a-form-item label="状态：" name="status">
				<a-radio-group v-model:value="formData.status">
					<a-radio :value="1">启用</a-radio>
					<a-radio :value="0">禁用</a-radio>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="排序：" name="sortCode">
				<a-input v-model:value="formData.sortCode" placeholder="请输入排序" allow-clear />
			</a-form-item>
			<a-form-item label="备注：">
				<a-textarea v-model:value="formData.remarks" show-count :maxlength="100" />
			</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="configForm">
	import { Empty } from 'ant-design-vue'
	import { cloneDeep, isEmpty } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizConfigApi from "@/api/biz/bizConfigApi";

	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		configName: '',
		status: 1,
		sortCode: 99,
		remarks: ''
	})
	const treeData = ref([])
	const submitLoading = ref(false)
	const parentData = ref({})
	const formRules = ref({
		configName: [required('请输入名字')],
		status: [required('请选择状态')],
		sortCode: [required('请输入排序')],
	})
	const onOpen = (record, data) => {
		if (data.parentId) {
			formData.value.parentId = data.parentId
			parentData.value = data
		}
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		visible.value = true
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {
			configName: '',
			status: 1,
			sortCode: 99,
			remarks: ''
		}
		visible.value = false
		submitLoading.value = false
	}
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			if (formDataParam.id) {
				bizConfigApi
					.editBaseConfig(formDataParam)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			} else {
				bizConfigApi
					.addBaseConfig(formDataParam)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			}

		})
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>

<style scoped>

</style>
