<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="isPass" :rules="[{ required: true, message: '请选择审批结果', trigger: 'change' }]">
							<el-radio-group v-model="state.ruleForm.isPass">
								<el-radio :value="true">同意</el-radio>
								<el-radio :value="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="opinion" :rules="[{ required: true, message: '请填写特提审批意见', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.opinion" 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 { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { storeToRefs } from 'pinia';
import { useUserInfo } from '@/stores/userInfo';
import { specialApproveBatch } from '@/api/business/special';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		opinion: '', // 审批意见
		isPass: true, // 是否通过
	},
});
const ruleFormRef = ref<RefType>();
const storesUserInfo = useUserInfo();
const { userInfos } = storeToRefs(storesUserInfo); // 用户信息
const ids = ref<any>([]);
// 打开弹窗
const openDialog = async (val: any) => {
	state.loading = false;
	ids.value = val;
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};

// 审批
const onAudit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		const request = {
			ids: ids.value,
			opinion: state.ruleForm.opinion,
			state: state.ruleForm.isPass ? 1 : 2, // 审核结果 0 待审核 1 审核通过 2 审核不通过
		};
		specialApproveBatch(request)
			.then(() => {
				state.loading = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('操作成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
