<template>
	<div class="knowledge-type-edit-container">
		<el-dialog title="修改类型" v-model="state.dialogVisible" draggable append-to-body destroy-on-close @close="close" width="500px">
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="80px" v-loading="state.loading">
				<el-row :gutter="10">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="类型名称" prop="name" :rules="[{ required: true, message: '请填写类型名称', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.name" placeholder="请填写类型名称" clearable></el-input>
						</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' }]">
							<el-cascader
								:options="state.treeData"
								filterable
								:props="{ checkStrictly: true, value: 'id', label: 'name', emitPath: false, children: 'children' }"
								placeholder="请选择上级类型"
								clearable
								class="w100"
								v-model="state.ruleForm.parentId"
								ref="cascadeRef"
								@change="getKnowledgeList"
							>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="排序" prop="sort" :rules="[{ required: false, message: '请填写排序', trigger: 'blur' }]">
							<el-input-number v-model="state.ruleForm.sort" :min="0" :precision="0" class="w100" placeholder="请填写排序" />
						</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>
	</div>
</template>

<script setup lang="ts" name="knowledgeTypeEdit">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { excludeSelfById } from '@/utils/tools';
import other from '@/utils/other';
import { updateType, typeDetail } from '@/api/knowledge/type';

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

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗
	ruleForm: {
		name: '', // 类型名称
		sort: 0, // 排序
		parentId: '', // 上级类型
	},
	treeData: [], // 上级
	loading: false, // 加载
});
// 打开弹窗
const ruleFormRef = ref<any>(); // 表单ref
const openDialog = async (row: any, treeData: any) => {
	try {
		state.dialogVisible = true;
		const { result } = await typeDetail(row.id);
		state.ruleForm = result;
		state.treeData = other.deepClone(treeData);
		state.treeData = excludeSelfById(state.treeData, row.id);
	} catch (error) {
		// 打印错误信息
		console.error(error);
	}
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const cascadeRef = ref<RefType>();
// 获取选择name值
const getKnowledgeList = () => {
	let currentNode = cascadeRef.value.getCheckedNodes();
	state.ruleForm.parentName = currentNode[0]?.label ?? '';
};
// 保存
const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		updateType(state.ruleForm)
			.then(() => {
				closeDialog(); // 关闭弹窗
				ElMessage.success('操作成功');
				state.loading = false;
				emit('updateList');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
