<script setup>
import Filter from "@/components/Filter/index.vue";
import FilterGroup from "@/components/FilterGroup";
import QuestionList from "../../components/QuestionList";
import { getSubjectStageChannelType, getSubjectTitleList } from "@/api";
import usePaperStore from "@/store/modules/paper";
import { storeToRefs } from "pinia";
import { formatTitleToPaper } from "@/utils";

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

const options = reactive([
  {
    label: "题型",
    optional: true,
    children: [],
  },
]);
const filter = reactive([[]]);

const filterData = ref({}); //过滤
const filterConfig = computed(() => {
  return [
    {
      comName: "el-input",
      label: "关键字",
      prop: "title",
      placeholder: "请输入",
      clearable: true,
    },
  ];
});

// 获取数据
const questionData = ref([]);
const QuestionListRef = ref();
const loading = ref(false);
async function getData() {
  loading.value = true;
  const page = QuestionListRef.value?.page || {};
  try {
    const res = await getSubjectTitleList({
      pageNum: page.pageNum,
      pageSize: page.pageSize,
      stagePid: search.value.stagePid,
      subjectId: search.value.subjectId,
      ...filterData.value,
      collectPageFlag: 1,
      channelTypeId: filter[0][0],
    });
    page.total = res.total;
    questionData.value = formatTitleToPaper(res.rows);
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
}

async function init() {
  if (!search.value.stagePid || !search.value.subjectId) return;
  const { data: res } = await getSubjectStageChannelType({
    pid: 0,
    stagePid: search.value.stagePid,
    subjectId: search.value.subjectId,
  });
  filter[0] = [];
  options[0].children = res.map((i) => {
    let obj = {
      label: i.name,
      value: i.baseId,
    };
    if (i.children && i.children.length > 0) {
      obj.children = i.children.map((j) => ({
        label: j.name,
        value: j.baseId,
      }));
    }
    return obj;
  });
  getData();
}

function onAnalysis(data) {
  console.log(data);
}

function onRefresh() {
  getData();
}

watch(
  () => filter[0][0],
  () => getData()
);

watchEffect(() => {
  init();
});

onMounted(() => {
  getData();
});
</script>
<template>
  <div class="app-container">
    <div class="box">
      <FilterGroup class="box" v-model="filter" :options="options" />
      <Filter
        v-model="filterData"
        :config="filterConfig"
        @handleAction="getData"
      />
    </div>

    <div class="list">
      <QuestionList
        ref="QuestionListRef"
        v-loading="loading"
        :showTool="false"
        :showFilter="false"
        custom
        :data="questionData"
        @refresh="onRefresh"
        @onAnalysis="onAnalysis"
      />
    </div>
  </div>
</template>
<style scoped lang="scss">
.app-container {
  &-title {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 16px;
  }
}
:deep(.tabs) {
  margin-right: 50%;

  .tabs-item {
    display: flex;
    margin-bottom: 16px;
    &:last-child {
      margin-bottom: 0;
    }
    .el-tag {
      margin-right: 12px;
      cursor: pointer;
    }
  }
}
.box {
  background: #fff;
  padding: 16px;
  margin-bottom: 16px;
  border-radius: 4px;
}
</style>
