<template>
	<el-dialog :title="dialogTitle" v-model="state.dialogVisible" width="30%" draggable append-to-body destroy-on-close @close="close">
		<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="80px" v-loading="state.loading">
			<el-form-item :label="textTip" prop="opinion" :rules="[{ required: true, message: '请填写' + textTip, trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.opinion" type="textarea" :autosize="{ minRows: 6, maxRows: 10 }" :placeholder="'请填写' + textTip">
				</el-input>
			</el-form-item>
		</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 { computed, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { knowledgeDraftDeleteAudit, knowledgeDraftEditAudit, KnowledgeOffShelfStartFlow } from '@/api/knowledge';

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

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗
	ruleForm: {
		opinion: '', // 内容
	},
	loading: false, // 加载
	planInfo: {}, // 预案详情内容
});
const planType = ref('update');
const dialogTitle = computed(() => {
	switch (planType.value) {
		case 'update':
			return '更新知识';
		case 'delete':
			return '删除知识';
		case 'offShelf':
			return '下架知识';
	}
});
const textTip = computed(() => {
	switch (planType.value) {
		case 'update':
			return '更新说明';
		case 'delete':
			return '删除说明';
		case 'offShelf':
			return '下架说明';
	}
});
// 打开弹窗
const ruleFormRef = ref<any>(); // 表单ref
const openDialog = (row: any, type = 'update') => {
	state.planInfo = row;
	planType.value = type;
	state.dialogVisible = true;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
// 新增
const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		switch (planType.value) {
			case 'update':
				// 更新提交审核
				knowledgeDraftEditAudit({
					...state.planInfo,
					opinion: state.ruleForm.opinion,
				})
					.then(() => {
						emit('updateList');
						closeDialog(); // 关闭弹窗
						ElMessage.success('更新审核提交成功');
						state.loading = false;
					})
					.catch(() => {
						state.loading = false;
					});
				break;
			case 'delete':
				// 删除提交审核
				knowledgeDraftDeleteAudit({
					id: state.planInfo.id,
					opinion: state.ruleForm.opinion,
				})
					.then(() => {
						emit('updateList');
						closeDialog(); // 关闭弹窗
						ElMessage.success('删除审核提交成功');
						state.loading = false;
					})
					.catch(() => {
						state.loading = false;
					});
				break;
			case 'offShelf':
				// 下架提交审核
				KnowledgeOffShelfStartFlow({
					id: state.planInfo.id,
					opinion: state.ruleForm.opinion,
				})
					.then(() => {
						emit('updateList');
						closeDialog(); // 关闭弹窗
						ElMessage.success('下架审核提交成功');
						state.loading = false;
					})
					.catch(() => {
						state.loading = false;
					});
				break;
		}
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
