<template>
	<el-dialog v-model="state.dialogVisible" width="769px" draggable title="小休审批" ref="dialogRef" append-to-body destroy-on-close @close="close">
		<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
			<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-form-item label="审核意见" prop="auditOpinion" :rules="[{ required: !state.ruleForm.isPass, message: '请填写审核意见', trigger: 'blur' }]">
				<el-input
					v-model="state.ruleForm.auditOpinion"
					placeholder="请填写审核意见"
					type="textarea"
					:autosize="{ minRows: 4, maxRows: 8 }"
				></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="loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="systemUserAdd">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { restApplyProcess } from '@/api/tels/restApply';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	ruleForm: {
		isPass: true,
		auditOpinion: '',
		ids: [],
	},
});
let loading = ref<boolean>(false); // 确定按钮loading
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = (ids: string[]) => {
	state.ruleForm.ids = ids;
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};

// 新增
const onSubmit = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		restApplyProcess(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '审批成功',
					type: 'success',
				});
				loading.value = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
				// 新增失败
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
