<template>
	<el-dialog title="修改事件" v-model="state.dialogVisible" width="700px" draggable @close="close">
		<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="80px" v-loading="state.loading">
			<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-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 } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { treeEventClassDetail, treeEventClassEdit } from '@/api/auxiliary/eventClass';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const ruleFormRef = ref<FormInstance>();
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	ruleForm: {
		orderBy: 0,
		eventName: null, // 事件名称
	},
	loading: false, // 确定按钮loading
});
// 打开弹窗
const openDialog = async (val:any) => {
	getDetail(val.id);
};
// 关闭弹窗
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;
		treeEventClassEdit(state.ruleForm)
			.then(() => {
				emit('updateList');
				closeDialog(); // 关闭弹窗
				ElMessage.success('操作成功');
				state.loading = false;
			})
			.catch(() => {
				emit('updateList');
				state.loading = false;
			});
	});
};
// 获取详情
const getDetail = (id:string) => {
	state.loading = true;
	treeEventClassDetail({id})
		.then((res) => {
			state.ruleForm = res.result;
			state.dialogVisible = true;
			state.loading = false;
		}).catch(()=>{
		state.loading = false;
	})
}
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
