<template>
  <div>
    <div>
      <span>设置：</span>
      <a-button @click="importModalVisible = true">题型分布设置</a-button>
    </div>

    <div v-if="props.questions.some((q) => q.type === 'TYPE1')">
      <div class="tw-flex tw-justify-between tw-items-center tw-my-4">
        <h3 class="section-title">题型题目区分度等级构成分布(一)</h3>
        <div class="tw-flex tw-gap-2">
          <!-- <a-button @click="openModalDESCRIBE160">说明</a-button> -->
        </div>
      </div>

      <DiscriminationDistriTable
        :questions="
          props.questions.filter((q) => q.type === 'TYPE1' || !q.type)
        "
      />
    </div>

    <div v-if="props.questions.some((q) => q.type === 'TYPE2')">
      <div class="tw-flex tw-justify-between tw-items-center tw-my-4">
        <h3 class="section-title">题型题目区分度等级构成分布(二)</h3>
        <div class="tw-flex tw-gap-2">
          <!-- <a-button @click="openModalDESCRIBE170">说明</a-button> -->
        </div>
      </div>

      <DiscriminationDistriTable
        :questions="
          props.questions.filter((q) => q.type === 'TYPE2' || !q.type)
        "
      />
    </div>

    <div v-if="props.questions.some((q) => q.type === 'TYPE3')">
      <div class="tw-flex tw-justify-between tw-items-center tw-my-4">
        <h3 class="section-title">题型题目区分度等级构成分布(三)</h3>
        <div class="tw-flex tw-gap-2">
          <!-- <a-button @click="openModalDESCRIBE180">说明</a-button> -->
        </div>
      </div>

      <DiscriminationDistriTable
        :questions="
          props.questions.filter((q) => q.type === 'TYPE3' || !q.type)
        "
      />
    </div>

    <div v-if="props.questions.some((q) => q.type === 'CONTENT1')">
      <div class="tw-flex tw-justify-between tw-items-center tw-my-4">
        <h3 class="section-title">内容题目区分度等级构成分布(一)</h3>
        <div class="tw-flex tw-gap-2">
          <!-- <a-button @click="openModalDESCRIBE190">说明</a-button> -->
        </div>
      </div>

      <DiscriminationDistriTable
        :questions="
          props.questions.filter((q) => q.type === 'CONTENT1' || !q.type)
        "
      />
    </div>

    <div v-if="props.questions.some((q) => q.type === 'CONTENT2')">
      <div class="tw-flex tw-justify-between tw-items-center tw-my-4">
        <h3 class="section-title">内容题目区分度等级构成分布(二)</h3>
        <div class="tw-flex tw-gap-2">
          <!-- <a-button @click="openModalDESCRIBE200">说明</a-button> -->
        </div>
      </div>

      <DiscriminationDistriTable
        :questions="
          props.questions.filter((q) => q.type === 'CONTENT2' || !q.type)
        "
      />
    </div>

    <div v-if="props.questions.some((q) => q.type === 'CONTENT3')">
      <div class="tw-flex tw-justify-between tw-items-center tw-my-4">
        <h3 class="section-title">内容题目区分度等级构成分布(三)</h3>
        <div class="tw-flex tw-gap-2">
          <!-- <a-button @click="openModalDESCRIBE210">说明</a-button> -->
        </div>
      </div>

      <DiscriminationDistriTable
        :questions="
          props.questions.filter((q) => q.type === 'CONTENT3' || !q.type)
        "
      />
    </div>

    <div v-if="props.questions.some((q) => q.type === 'ABILITY1')">
      <div class="tw-flex tw-justify-between tw-items-center tw-my-4">
        <h3 class="section-title">能力题目区分度等级构成分布(一)</h3>
        <div class="tw-flex tw-gap-2">
          <!-- <a-button @click="openModalDESCRIBE220">说明</a-button> -->
        </div>
      </div>

      <DiscriminationDistriTable
        :questions="
          props.questions.filter((q) => q.type === 'ABILITY1' || !q.type)
        "
      />
    </div>

    <div v-if="props.questions.some((q) => q.type === 'ABILITY2')">
      <div class="tw-flex tw-justify-between tw-items-center tw-my-4">
        <h3 class="section-title">能力题目区分度等级构成分布(二)</h3>
        <div class="tw-flex tw-gap-2">
          <!-- <a-button @click="openModalDESCRIBE230">说明</a-button> -->
        </div>
      </div>

      <DiscriminationDistriTable
        :questions="
          props.questions.filter((q) => q.type === 'ABILITY2' || !q.type)
        "
      />
    </div>

    <div v-if="props.questions.some((q) => q.type === 'ABILITY3')">
      <div class="tw-flex tw-justify-between tw-items-center tw-my-4">
        <h3 class="section-title">能力题目区分度等级构成分布(三)</h3>
        <div class="tw-flex tw-gap-2">
          <!-- <a-button @click="openModalDESCRIBE240">说明</a-button> -->
        </div>
      </div>

      <DiscriminationDistriTable
        :questions="
          props.questions.filter((q) => q.type === 'ABILITY3' || !q.type)
        "
      />
    </div>

    <a-modal
      v-model:visible="importModalVisible"
      title="题型设置导入"
      okText="确定"
      cancelText="取消"
      @ok="handleImport"
    >
      <a-form>
        <a-form-item label="文件地址">
          <input id="file-input" :multiple="false" type="file" />
        </a-form-item>
        <a-form-item label="下载模板">
          <a-button class="download-tpl-btn" @click="downloadTpl">
            下载模板
          </a-button>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { importQuestionGroups } from "@/api/paperAnalysisPage";
// import EventBus from "@/plugins/eventBus";
import { SASQuestionGroup } from "@/types";
import { downloadFileURL } from "@/utils/utils";
import { message } from "ant-design-vue";
import { h } from "vue";
import DiscriminationDistriTable from "./DiscriminationDistriTable.vue";

const props = defineProps<{
  questions: SASQuestionGroup[];
  projectId: number;
}>();

// function openModalDESCRIBE160() {
//   EventBus.emit("SHOW_SETTING", "DESCRIBE160");
// }
// function openModalDESCRIBE170() {
//   EventBus.emit("SHOW_SETTING", "DESCRIBE170");
// }
// function openModalDESCRIBE180() {
//   EventBus.emit("SHOW_SETTING", "DESCRIBE180");
// }
// function openModalDESCRIBE190() {
//   EventBus.emit("SHOW_SETTING", "DESCRIBE190");
// }
// function openModalDESCRIBE200() {
//   EventBus.emit("SHOW_SETTING", "DESCRIBE200");
// }
// function openModalDESCRIBE210() {
//   EventBus.emit("SHOW_SETTING", "DESCRIBE210");
// }
// function openModalDESCRIBE220() {
//   EventBus.emit("SHOW_SETTING", "DESCRIBE220");
// }
// function openModalDESCRIBE230() {
//   EventBus.emit("SHOW_SETTING", "DESCRIBE230");
// }
// function openModalDESCRIBE240() {
//   EventBus.emit("SHOW_SETTING", "DESCRIBE240");
// }

/** <handleImport> */
let importModalVisible = $ref<boolean>(false);
async function handleImport() {
  const files = (document.querySelector("#file-input") as HTMLInputElement)
    .files;
  const fileToImport = files && files[0];
  if (!fileToImport) {
    void message.warn({ content: "请选择文件" });
    return;
  }
  (document.querySelector("#file-input") as HTMLInputElement).value = "";
  const res = await importQuestionGroups(props.projectId, fileToImport);
  if (!res.data.hasError) {
    importModalVisible = false;
    void message.success({ content: "导入成功" });
  } else {
    // eslint-disable-next-line
    const msg = res.data.failRecords.map((v) =>
      h("div", `行号：${v.lineNum}, 错误：${v.msg}`)
    );
    void message.error({
      content: h("span", ["导入失败", ...msg]),
    });
  }
}
/** </handleImport> */

async function downloadTpl() {
  await downloadFileURL("/api/ess/sasQuestionGroup/template");
}
</script>

<style scoped></style>
