<template>
	<xn-form-container
		:title="formData.id ? '编辑栋舍' : '增加栋舍'"
		: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="buildName">
				<a-input v-model:value="formData.buildName" placeholder="请输入栋舍名字" allow-clear />
			</a-form-item>
			<a-form-item label="上级栋舍：" name="parentId">
				<a-tree-select
					v-model:value="formData.parentId"
					style="width: 100%"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择上级栋舍"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					:field-names="{
						children: 'children',
						label: 'name',
						value: 'id'
					}"
					selectable="false"
					tree-line
				/>
			</a-form-item>
			<a-form-item label="阶段" name="stage">
				<a-select v-model:value="formData.stage">
					<a-select-option v-for="item in typeList" :key="item.id" :value="item.configName">{{item.configName}}</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="排序：" name="sort">
				<a-input v-model:value="formData.sort" 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="houseForm">
	import { Empty } from 'ant-design-vue'
	import { cloneDeep, isEmpty } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import basePigpenApi from '@/api/biz/bizHouseApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const treeData = ref([])
	const submitLoading = ref(false)
	const typeList = ref([])

	// 打开抽屉
	const onOpen = (record, parentId) => {
		visible.value = true
		if (parentId) {
			formData.value.parentId = parentId
		}
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		basePigpenApi.basePigpenTree().then((res) => {
			treeData.value = [
				{
					id: 0,
					parentId: '-1',
					name: '顶级',
					children: res
				}
			]
		})
		basePigpenApi.basePigpenType().then((res) => {
			typeList.value = res
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		buildName: [required('请输入栋舍名字')],
		parentId: [required('请输入父级ID')],
		sort: [required('请输入排序')],
		stage: [required('请选择阶段')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			basePigpenApi
				.basePigpenSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
