<script setup>
import { computed, onMounted, reactive, ref, watchEffect } from "vue";
import FilterGroup from "@/components/FilterGroup";
import QuestionList from "../components/QuestionList";
import SyncPaper from "../components/SyncPaper";
import usePaperStore from "@/store/modules/paper";
import { storeToRefs } from "pinia";
import {
  getStageSubjectVoList,
  getSubjectTitleList,
  getSubjectPaperList,
} from "@/api";
import { formatTitleToPaper } from "@/utils";

const { search } = storeToRefs(usePaperStore());

const options = ref([
  {
    label: "学科",
    children: [],
  },
  {
    label: "年份",
    optional: true,
    children: Array.from({ length: 10 }, (i, idx) => ({
      label: 2024 - idx,
      value: 2024 - idx,
    })),
  },
]);
const filter = reactive([[], []]);
const filterId = computed(() => {
  const [stagePid, subjectId] = (filter[0][0] || "").split("#");
  return {
    stagePid,
    subjectId,
  };
});

const questionData = ref([]);
const QuestionListRef = ref();
const loading = ref(false);
// 查题
async function getData() {
  if (!search.value.keyword) return;
  loading.value = true;
  const page = QuestionListRef.value?.page || {};
  try {
    const res = await getSubjectTitleList({
      pageNum: page.pageNum,
      pageSize: page.pageSize,
      ...filterId.value,
      years: filter[1][0],
      title: search.value.keyword,
    });
    page.total = res.total;
    questionData.value = formatTitleToPaper(res.rows);
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
}
// 查试卷
const paperData = ref([]);
const SyncPaperRef = ref();
async function getPaperData() {
  if (!search.value.keyword) return;
  loading.value = true;
  const page = SyncPaperRef.value?.page || {};
  try {
    const res = await getSubjectPaperList({
      pageNum: page.pageNum,
      pageSize: page.pageSize,
      ...filterId.value,
      years: filter[1][0],
      name: search.value.keyword,
    });
    page.total = res.total;
    paperData.value = res.rows;
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
}

async function init() {
  const { data: res } = await getStageSubjectVoList();
  options.value[0].children = res.map((i) => ({
    label: i.name,
    value: `${i.stagePid}#${i.subjectId}`,
  }));
  // 默认第一个
  filter[0] = [options.value[0].children[0].value];
}

onMounted(async () => {
  await init();
  watchEffect(() => {
    if (search.value.keywordType === 0) {
      getData();
    }
    if (search.value.keywordType === 1) {
      getPaperData();
    }
  });
});
</script>

<template>
  <div class="search-main">
    <FilterGroup class="box" v-model="filter" :options="options" />
    <QuestionList
      v-if="search.keywordType === 0"
      ref="QuestionListRef"
      v-loading="loading"
      :showTool="false"
      :showFilter="false"
      custom
      :data="questionData"
    />
    <SyncPaper
      v-if="search.keywordType === 1"
      ref="SyncPaperRef"
      v-loading="loading"
      :showFilter="false"
      custom
      :data="paperData"
    />
  </div>
</template>

<style lang="scss" scoped>
.search-main {
  padding: 16px;

  .box {
    background: #fff;
    padding: 16px;
    margin-bottom: 16px;
    border-radius: 4px;
  }
}
</style>
