<template>
  <div class="question-export-paper-edit">
    <div v-for="(detail, dIndex) in paperData" :key="dIndex" class="ep-detail">
      <div class="ep-detail-head">
        <h3 class="ep-detail-title">
          {{ detail.number | numberToChaineseFilter }}、{{ detail.name }}
        </h3>
        <!-- <p class="ep-detail-desc"></p> -->
      </div>
      <div class="ep-detail-body">
        <div
          v-for="question in detail.questions"
          :key="question.number"
          class="ep-question"
        >
          <div class="ep-question-type">
            [题型：{{ question.questionType | questionTypeFilter }}]
          </div>
          <component
            :is="structTypeComp(question.questionType)"
            :ref="`QuestionEditDetail${dIndex}-${question.number}`"
            :question="question"
          ></component>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { deepCopy, randomCode } from "@/plugins/utils";
import { structTypeToQuestionType } from "./import-edit/model";
import BooleanQuestion from "./import-edit/BooleanQuestion.vue";
import FillBlankQuestion from "./import-edit/FillBlankQuestion.vue";
import SelectQuestion from "./import-edit/SelectQuestion.vue";
import TextAnswerQuestion from "./import-edit/TextAnswerQuestion.vue";
import NestedQuestion from "./import-edit/NestedQuestion.vue";
import BankedClozeQuestion from "./import-edit/BankedClozeQuestion.vue";
import { STRUCT_TYPE_COMP_DICT } from "./edit/questionModel";

export default {
  name: "QuestionExportPaperEdit",
  components: {
    FillBlankQuestion,
    SelectQuestion,
    TextAnswerQuestion,
    BooleanQuestion,
    NestedQuestion,
    BankedClozeQuestion,
  },
  props: {
    paper: {
      type: Array,
      default() {
        return [];
      },
    },
    courseId: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      paperData: [],
    };
  },
  mounted() {
    this.transformPaperData();
  },
  methods: {
    structTypeComp(questionType) {
      return STRUCT_TYPE_COMP_DICT[questionType];
    },
    transformPaperData() {
      let paperData = deepCopy(this.paper);
      paperData.forEach((detail) => {
        detail.questions.forEach((question) => {
          question.questionType = structTypeToQuestionType(question.type);
          question.courseId = this.courseId;
          question.score = question.score || detail.questionScore || 0;
          question.id = randomCode();

          if (question.subQuestions && question.subQuestions.length) {
            question.subQuestions.forEach((subq) => {
              subq.id = randomCode();
              subq.questionType = structTypeToQuestionType(subq.type);
              subq.courseId = this.courseId;
              subq.score = subq.score || detail.questionScore || 0;
            });
          }
        });
      });
      this.paperData = paperData;
    },
    getData() {
      this.paperData.forEach((detail, dIndex) => {
        detail.questions.forEach((question) => {
          const refName = `QuestionEditDetail${dIndex}-${question.number}`;
          const questionData = this.$refs[refName][0].getData();
          Object.assign(question, questionData);
        });
      });
      return this.paperData;
    },
    // question
    questionActive(question) {
      this.activeQuestionId = question.id;
    },
  },
};
</script>
