<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="130px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字搜索" prop="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        :data="list"
        @get-data="fnGetDataTransfer"
        v-model:pagination="pagination"
      >
        <el-table-column prop="QUESTIONNUMBER" label="题号" width="60" />
        <el-table-column
          prop="QUESTIONTYPE"
          label="试题类型"
          show-overflow-tooltip
          width="80"
        >
          <template v-slot="{ row }">
            {{ fnFormatterQuestionType(row) }}
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="QUESTIONDRY"
          label="题干"
          align="left"
        />
        <el-table-column
          show-overflow-tooltip
          prop="ANSWER"
          label="答案"
          width="55"
        >
          <template v-slot="{ row }">
            <template v-if="row.QUESTIONTYPE === '3'">
              <div v-if="row.ANSWER === 'A'">正确</div>
              <div v-if="row.ANSWER === 'B'">错误</div>
            </template>
            <template v-else>
              <div>{{ row.ANSWER }}</div>
            </template>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import LayoutTable from "@/components/table/index.vue";
import LayoutCard from "@/components/card/index.vue";
import { getQuestionListByCoursewareIdAndType } from "@/request/platform_resource_library.js";
import useListData from "@/assets/js/useListData.js";
import { reactive } from "vue";
import { useRoute } from "vue-router";

const route = useRoute();
const { COURSE_ID, COURSE_TYPE } = route.query;

const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getQuestionListByCoursewareIdAndType, {
    otherParams: { COURSEWAREID: COURSE_ID, COURSEWARETYPE: COURSE_TYPE },
  });

const data = reactive({
  questionTypeOptions: [
    {
      value: "1",
      label: "单选题",
    },
    {
      value: "2",
      label: "多选题",
    },
    {
      value: "3",
      label: "判断题",
    },
  ],
});

const fnGetDataTransfer = async () => {
  await fnGetData({
    COURSEWAREID: COURSE_ID,
    COURSEWARETYPE: COURSE_TYPE,
  });
};

const fnResetPaginationTransfer = async () => {
  await fnResetPagination({
    COURSEWAREID: COURSE_ID,
    COURSEWARETYPE: COURSE_TYPE,
  });
};

const fnFormatterQuestionType = (row) => {
  let label = "";
  data.questionTypeOptions.forEach((option) => {
    if (option.value === row.QUESTIONTYPE) {
      label = option.label;
    }
  });
  return label;
};
</script>

<style scoped lang="scss"></style>
