import { ElMessage } from 'element-plus';
import {
  markerClassTemplate,
  exportUserByExam,
  exportUserBySubject,
  exportUser,
  bindStudentTemplate,
  headerInspectorTemplate,
} from '@/api/user';
import {
  studentTemplate,
  absentStudentTemplate,
  breachStudentTemplate,
  exceptionStudentTemplate,
  mediaStudentTemplate,
  exportStudent,
} from '@/api/student';
import { exportLog } from '@/api/log';
import {
  exportSubjectMainObjective,
  downloadSubjectObjectiveImportTemplate,
} from '@/api/subject';
import { exportScanStatByPoint, exportScanStatByCourse } from '@/api/scan';

import useLoading from '@/hooks/loading';

import { downloadByApi } from './download';

const downloadConfig = {
  // user
  // 评卷员班级导入模板下载
  markerClassTemplate,
  // 按考试导出用户
  exportUserByExam,
  // 按考试导出科目分表用户
  exportUserBySubject,
  // 导出用户
  exportUser,
  // 绑定考生导入模板下载
  bindStudentTemplate,
  // 科组长-复核员导入模板下载
  headerInspectorTemplate,
  // student
  // 下载考生导入模板
  studentTemplate,
  // 下载缺考考生导入模板
  absentStudentTemplate,
  // 下载违纪考生导入模板
  breachStudentTemplate,
  // 下载整理异常导入模板
  exceptionStudentTemplate,
  // 下载多媒体考生上传模板
  mediaStudentTemplate,
  // 导出考生
  exportStudent,
  // log
  // 导出日志
  exportLog,
  // subject
  // 导出主客观题
  exportSubjectMainObjective,
  // 下载主客观题导入模版
  downloadSubjectObjectiveImportTemplate,
  // scan
  // 按考点导出
  exportScanStatByPoint,
  // 按科目导出
  exportScanStatByCourse,
};
type DownloadType = keyof typeof downloadConfig;

const { loading, setLoading } = useLoading();
export async function downloadExport(
  type: DownloadType,
  params?: Record<string, any>
) {
  if (loading.value) return;

  if (!downloadConfig[type]) {
    ElMessage.error('下载类型错误！');
    return;
  }

  setLoading(true);
  const res = await downloadByApi(() => downloadConfig[type](params)).catch(
    (e) => {
      ElMessage.error(e || '下载失败，请重新尝试！');
    }
  );
  setLoading(false);
  if (!res) return;
  ElMessage.success('下载成功！');
}
