<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.applyStatus)">{{ applyReason }}</el-form-item>
			<el-form-item label="审批结果" prop="state" :rules="[{ required: true, message: '请选择审批结果', trigger: 'change' }]">
				<el-radio-group v-model="state.ruleForm.state">
					<el-radio :value="1">同意</el-radio>
					<el-radio :value="0">不同意</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="审批意见" prop="examinOpinion" :rules="[{ required: true, message: '请填写审批意见', trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.examinOpinion" 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 { caseAudit, caseReason } from '@/api/case';

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

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗
	ruleForm: {
		state: 1, // 默认同意
		examinOpinion: null, //审核意见
	},
	loading: false, // 加载
});
const planType = ref('update');
const textTip = computed(() => {
	if (planType.value === 'update') {
		return '更新说明';
	} else if (planType.value === 'delete') {
		return '删除说明';
	}
});
// 查询申请审批意见
const applyReason = ref<any>('');
const getTips = async (row: any) => {
	try {
		const { result } = await caseReason(row.id);
		applyReason.value = result.applyReason;
	} catch (e) {
		console.log(e);
	}
};
// 打开弹窗
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 openDialog = (row: any) => {
	switch (row.applyStatus) {
		case 0:
			dialogTitle.value = '案例新增审批';
			break;
		case 1:
			planType.value = 'update';
			dialogTitle.value = '案例修改审批';
			break;
		case 2:
			planType.value = 'delete';
			dialogTitle.value = '案例删除审批';
			break;
	}
	planInfo.value = row;
	getTips(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;
		caseAudit({
			id: planInfo.value.id,
			state: state.ruleForm.state,
			examinOpinion: state.ruleForm.examinOpinion,
		})
			.then(() => {
				emit('updateList');
				closeDialog(); // 关闭弹窗
				ElMessage.success('审核成功');
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
