<template>
  <a-modal
    v-model:open="visible"
    :width="456"
    style="top: 10vh"
    :footer="null"
    title="导入缺考名单"
  >
    <a-row :gutter="16">
      <a-col :span="12">
        <import-btn
          :upload-url="uploadUrl"
          :upload-data="updateData"
          :format="['txt']"
          @upload-success="checkUploadStatus"
          @valid-error="onValidError"
        >
          <a-button class="type-box" @click="seleted('COMPARE')">
            <img src="@/assets/imgs/bg-compare.png" alt="交叉对比" />
            <p>交叉对比</p>
            <p>CROSS-CONTRAST</p>
          </a-button>
        </import-btn>
      </a-col>
      <a-col :span="12">
        <import-btn
          :upload-url="uploadUrl"
          :upload-data="updateData"
          :format="['txt']"
          @upload-success="startLoopSync"
          @valid-error="onValidError"
        >
          <a-button class="type-box" @click="seleted('OVERRIDE')">
            <img src="@/assets/imgs/bg-overwrite.png" alt="覆盖数据" />
            <p>覆盖数据</p>
            <p>OVERWRITE THE DATA</p>
          </a-button>
        </import-btn>
      </a-col>
    </a-row>
    <div class="m-t-16px text-center">
      <a-button type="primary" :loading="loading" @click="onDownloadTemp">
        <template #icon><DownloadOutlined /></template>
        下载缺考名单模版
      </a-button>
    </div>
  </a-modal>
</template>

<script setup lang="ts">
import {
  FilterFilled,
  PictureFilled,
  IdcardFilled,
  DownloadOutlined,
} from "@ant-design/icons-vue";
import { computed, ref } from "vue";
import useModal from "@/hooks/useModal";
import { ImportType } from "@/ap/types/absentCheck";
import { absentImportStatus } from "@/ap/absentCheck";
import { absentTemplateDownload } from "@/ap/absentCheck";
import useLoading from "@/hooks/useLoading";
import useLoop from "@/hooks/useLoop";

import ImportBtn from "@/components/ImportBtn/index.vue";
import { useUserStore } from "@/store";
import { message } from "@qmth/ui";

defineOptions({
  name: "ImportTypeDialog",
});

/* modal */
const { visible, open, close } = useModal();
defineExpose({ open, close });

const userStore = useUserStore();

const uploadUrl = "/api/admin/check/exam-status/import";
const uploadMode = ref("" as ImportType);

const updateData = computed(() => {
  return {
    examId: userStore.curExam.id,
    mode: uploadMode.value,
  };
});

function seleted(type: ImportType) {
  uploadMode.value = type;
}

const { start: startLoopSync, stop: stopLoopSync } = useLoop(
  checkUploadStatus,
  1000
);

async function checkUploadStatus() {
  const res = await absentImportStatus(userStore.curExam.id).catch(() => {});
  if (res) {
    if (!res.synching) {
      stopLoopSync();
      message.success("导入成功！");
      close();
    }
  } else {
    stopLoopSync();
    message.error("导入错误！");
  }
}

function onValidError(data: { message: string }) {
  message.error(message);
}

// 下载模板
const { loading, setLoading } = useLoading();
async function onDownloadTemp() {
  if (loading.value) return;

  setLoading(true);
  const res = await absentTemplateDownload().catch((e: Error) => {
    message.error(e.message || "下载失败，请重新尝试！");
  });
  setLoading(false);
  if (!res) return;
  message.success("下载成功！");
}
</script>

<style lang="less" scoped>
.type-box {
  width: 200px;
  height: 140px;
  border-radius: 6px;
  padding: 26px;
  border: 1px solid @border-color1;
  text-align: center;
  cursor: pointer;

  &:hover {
    background: #f3f4f6;
  }

  > img {
    display: block;
    height: 35px;
    width: 32px;
    margin: 0 auto;
    margin-bottom: 12px;
  }

  > p:nth-of-type(1) {
    height: 24px;
    font-weight: 500;
    font-size: 16px;
    color: #14161a;
    line-height: 24px;
  }
  > p:nth-of-type(2) {
    height: 18px;
    font-size: 10px;
    color: #8c8c8c;
    line-height: 18px;
  }
}
</style>
