<template>
	<xn-form-container
		:title="formData.id ? '编辑监控对象' : '新增监控对象'"
		:width="'500px'"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form
			ref="formRef"
			:model="formData"
			:rules="formRules"
			layout="inline"
			:label-col="{ style: { width: '100px', justifyContent: 'end' } }"
		>
			<a-form-item label="对象名称" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入对象名称" allow-clear />
			</a-form-item>
			<a-form-item label="对象状态" name="status">
				<a-select ref="select" v-model:value="formData.status" placeholder="请选择对象状态">
					<a-select-option value="1">正常</a-select-option>
					<a-select-option value="2">停用</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="监控位置区域">
				<a-input v-model:value="formData.monitorPoint" placeholder="请输入监控位置区域" allow-clear />
			</a-form-item>
		</a-form>

		<template #footer>
			<a-button class="xn-mr8" @click="onClose">退出</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import setupApi from '@/api/basicset/setupApi'

	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()

	// 默认要校验的
	const formRules = {
		name: [required('请输入对象名称')],
		status: [required('请选择对象状态')]
	}

	// 表单数据
	const formData = ref({})

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				setupApi.submitForm(formData.value, formData.value.id).then(() => {
					onClose()
					emit('successful')
				})
			})
			.catch((error) => {
				console.log(error)
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style lang="less" scoped>
	:deep(.ant-form-item) {
		width: 100%;
		margin-bottom: 10px;
	}
</style>
