<template>
	<el-dialog title="新增事件" v-model="state.dialogVisible" width="700px" draggable @close="close" destroy-on-close>
		<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="80px">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="事件名称" prop="eventName" :rules="[{ required: true, message: '请填写事件名称', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.eventName" placeholder="请填写事件名称" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="排序" prop="orderBy" :rules="[{ required: false, message: '请填写排序', trigger: 'blur' }]">
						<el-input-number v-model="state.ruleForm.orderBy" controls-position="right" placeholder="请填写排序" class="w100" />
					</el-form-item>
				</el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-form-item label="事件分类" prop="parentId" :rules="[{ required: false, message: '请选择上级事件', trigger: 'change' }]">
            <event-select
                v-model="state.ruleForm.parentId"
                v-model:externalArr="state.eventCategoryExternal"
                placeholder="请选择上级事件"
                clearable
								checkStrictly
            />
          </el-form-item>
        </el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="state.loading">确 定 </el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, defineAsyncComponent } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { treeEventClassAdd } from '@/api/auxiliary/eventClass';

const EventSelect = defineAsyncComponent(() => import('@/components/Hotspot/event.vue')); // 选择事件
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const ruleFormRef = ref<FormInstance>();
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	ruleForm: {
		orderBy: 0,
		eventName: null, // 事件名称
		parentId: null, // 上级区域id
	},
	loading: false, // 确定按钮loading
	eventCategoryExternal:[]
});
// 打开弹窗
const openDialog = async () => {
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 新增
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		treeEventClassAdd(state.ruleForm)
			.then(() => {
				emit('updateList');
				closeDialog(); // 关闭弹窗
				ElMessage.success('操作成功');
				state.loading = false;
			})
			.catch(() => {
				emit('updateList');
				state.loading = false;
				closeDialog();
			});
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
