import { ElMessage } from 'element-plus';
import { splitCourseTemplate, exportHardwareInfo } from '@/api/admin';
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 { exportRejectStat, exportRejectRecord } from '@/api/reject';
import { exportIssuePaper } from '@/api/issue-paper';
import {
  exportTotalAnalysisSummaryList,
  exportTotalAnalysisList,
  exportSegmentAnalysisList,
  exportCollegeAnalysisList,
  exportTeacherAnalysisList,
  exportClassAnalysisList,
  exportQuestionAnalysisList,
  exportBigQuestionAnalysisList,
} from '@/api/analysis';
import {
  exportQualityMonitorList,
  exportGroupArbitrateStatList,
  exportArbitrationList,
  exportMarkTaskList,
  markerImportTemplate,
  exportMarkProgress,
  exportMarkWorkload,
} from '@/api/mark';
import {
  exportReviewStatList,
  exportFullReviewList,
  exportReviewWorkload,
  reviewImportTemplate,
} from '@/api/review';
import { exportImageCheckData } from '@/api/check';

import useLoading from '@/hooks/loading';

import { downloadByApi } from './download';

const downloadConfig = {
  // admin
  // 导出拆分科目模板
  splitCourseTemplate,
  // 导出硬件信息
  exportHardwareInfo,
  // user
  // 评卷员班级导入模板下载
  markerClassTemplate,
  // 按考试导出用户
  exportUserByExam,
  // 按考试导出科目分表用户
  exportUserBySubject,
  // 导出用户
  exportUser,
  // 绑定考生导入模板下载
  bindStudentTemplate,
  // 科组长-复核员导入模板下载
  headerInspectorTemplate,
  // student
  // 下载考生导入模板
  studentTemplate,
  // 下载缺考考生导入模板
  absentStudentTemplate,
  // 下载违纪考生导入模板
  breachStudentTemplate,
  // 下载整理异常导入模板
  exceptionStudentTemplate,
  // 下载多媒体考生上传模板
  mediaStudentTemplate,
  // 导出考生
  exportStudent,
  // log
  // 导出日志
  exportLog,
  // subject
  // 导出主客观题
  exportSubjectMainObjective,
  // 下载主客观题导入模版
  downloadSubjectObjectiveImportTemplate,
  // scan
  // 按考点导出
  exportScanStatByPoint,
  // 按科目导出
  exportScanStatByCourse,
  // reject
  // 导出打回统计
  exportRejectStat,
  // 导出打回记录
  exportRejectRecord,
  // issue paper
  // 导出问题卷
  exportIssuePaper,
  // analysis
  // 总量分析总结表导出
  exportTotalAnalysisSummaryList,
  // 总量分析导出
  exportTotalAnalysisList,
  // 分段分析导出
  exportSegmentAnalysisList,
  // 学院分析导出
  exportCollegeAnalysisList,
  // 教师分析导出
  exportTeacherAnalysisList,
  // 班级分析导出
  exportClassAnalysisList,
  // 问题分析导出
  exportQuestionAnalysisList,
  // 大题分析导出
  exportBigQuestionAnalysisList,
  // mark
  // 分组仲裁统计导出
  exportGroupArbitrateStatList,
  // 导出质量监控列表
  exportQualityMonitorList,
  // 导出仲裁列表
  exportArbitrationList,
  // 导出任务列表
  exportMarkTaskList,
  // 评卷员导入模板下载
  markerImportTemplate,
  // 导出评卷进度
  exportMarkProgress,
  // 导出工作量统计
  exportMarkWorkload,
  // review
  // 导出复核进度
  exportReviewStatList,
  // 导出全卷复核列表
  exportFullReviewList,
  // 导出复核工作量
  exportReviewWorkload,
  // 复核导入模板下载
  reviewImportTemplate,
  // check
  // 导出图片检查数据
  exportImageCheckData,
};
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('下载成功！');
}
