<template>
	<el-dialog v-model="state.dialogVisible" draggable title="扭转评判" width="40%" append-to-body destroy-on-close @close="close">
		<div class="collapse-container" v-loading="state.loading">
			<el-form label-width="110px" ref="ruleFormRef" :model="state.ruleForm">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="12">
						<el-form-item label="评判结果" prop="isAgree" :rules="[{ required: true, message: '请选择评判结果', trigger: 'change' }]">
							<el-radio-group v-model="state.ruleForm.isAgree">
								<el-radio :label="true">同意</el-radio>
								<el-radio :label="false">不同意</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="评判意见" prop="judgeContent" :rules="[{ required: true, message: '请填评判意见', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.judgeContent" type="textarea" :autosize="{ minRows: 6, maxRows: 10 }" placeholder="请填评判意见" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onAudit(ruleFormRef)" :loading="state.loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { defineAsyncComponent, reactive, ref } from 'vue';
import {  ElNotification, FormInstance } from 'element-plus';
import { visitTurnSatisfaction } from '@/api/business/visit';

// 引入组件
const AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue'));

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		judgeContent: '', // 评判内容
		isAgree: true, // 是否通过
	},
});
const ruleFormRef = ref<RefType>();
const ids = ref<any>([]);
// 打开弹窗
const openDialog = async (val: any) => {
	ids.value = val;
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
  ruleFormRef.value?.resetFields();
	state.loading = false;
};

// 审批
const onAudit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		const request = {
			ids: ids.value,
			judgeContent: state.ruleForm.judgeContent,
			isAgree: state.ruleForm.isAgree,
		};
		visitTurnSatisfaction(request)
			.then((res: any) => {
				state.loading = false;
				closeDialog();
				emit('updateList');
				ElNotification({
					type: 'info',
					title: '评判完成',
					message: `成功评判${res.result?.successCount}条，失败${res.result?.errorCount}条`,
				});
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
