<template>
	<el-dialog :title="dialogTitle" v-model="state.dialogVisible" 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" v-if="[1, 2].includes(planInfo.knowledgeApproveType)">{{ applyReason }}</el-form-item>
			<el-form-item label="审批结果" prop="isSuccess" :rules="[{ required: true, message: '请选择审批结果', trigger: 'change' }]">
				<el-radio-group v-model="state.ruleForm.isSuccess">
					<el-radio :value="true">同意</el-radio>
					<el-radio :value="false">不同意</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="审批意见" prop="opinion" :rules="[{ required: true, message: '请填写审批意见', trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.opinion" type="textarea" :autosize="{ minRows: 6, maxRows: 10 }" placeholder="请填写审批意见"> </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 { knowledgeAuditBatch } from '@/api/knowledge/audit';

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

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗
	ruleForm: {
		isSuccess: true, // 默认同意
		opinion: null, //审核意见
	},
	loading: false, // 加载
});
const planType = ref('update');
const textTip = computed(() => {
	if (planType.value === 'update') {
		return '更新说明';
	} else if (planType.value === 'delete') {
		return '删除说明';
	} else if (planType.value === 'offShelf') {
		return '下架说明';
	}
});
// 打开弹窗
const ruleFormRef = ref<any>(); // 表单ref
const planInfo = ref<any>();
const dialogTitle = ref('知识更新审批');
/*[
	{
		"key": -1,
		"value": "全部"
	},
	{
		"key": 0,
		"value": "新增审核"
	},
	{
		"key": 1,
		"value": "修改审核"
	},
	{
		"key": 2,
		"value": "删除审核"
	}
]*/
// 查询申请审批意见
const ids = ref<any>([]);
const applyReason = ref<any>('');
const openDialog = (row: any, isMultiple = false) => {
	console.log(row, '111');
	if (isMultiple) {
		// 如果是批量审批
		dialogTitle.value = '批量审批';
		ids.value = row;
		planInfo.value = {};
	} else {
		switch (row.knowledgeApproveType) {
			case 0:
				dialogTitle.value = '知识新增审批';
				break;
			case 1:
				planType.value = 'update';
				dialogTitle.value = '知识修改审批';
				break;
			case 2:
				planType.value = 'delete';
				dialogTitle.value = '知识删除审批';
				break;
		}
		applyReason.value = row.createOpinion;
		ids.value = [row.id];
		planInfo.value = row;
	}
	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;
		knowledgeAuditBatch({
			approveIds: ids.value,
			isSuccess: state.ruleForm.isSuccess,
			opinion: state.ruleForm.opinion,
		})
			.then(() => {
				emit('updateList');
				closeDialog(); // 关闭弹窗
				ElMessage.success('审核成功');
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
