<template>

  <div style="overflow: auto">
    <div class="question-header">
      <Icon :type="examQuestion.isSign ? 'ios-star':'ios-star-outline'" :style="{color: '#ffcc00'}" class="star" @click="toggleSign" />
      <question-index :examQuestion="examQuestion" />
    </div>
    <div v-if="parentQuestionBody" class="question-view">
      <question-body :questionBody="parentQuestionBody" :examQuestion="examQuestion" style="margin-bottom: 20px" :key="examQuestion.questionId"></question-body>
      <div class="hr" />
    </div>

    <transition name="fade">

      <div v-show="question && examQuestion" class="question-view" :key="examQuestion.order">

        <template v-if="question && question.questionType === 'SINGLE_CHOICE'">
          <single-question-view :question="question" :examQuestion="examQuestion" :key="examQuestion.order" />
        </template>
        <template v-if="question && question.questionType === 'MULTIPLE_CHOICE'">
          <multiple-question-view :question="question" :examQuestion="examQuestion" :key="examQuestion.order" />
        </template>
        <template v-if="question && question.questionType === 'TRUE_OR_FALSE'">
          <boolean-question-view :question="question" :examQuestion="examQuestion" :key="examQuestion.order" />
        </template>
        <template v-if="question && question.questionType === 'FILL_UP'">
          <fill-blank-question-view :question="question" :examQuestion="examQuestion" :key="examQuestion.order" />
        </template>
        <template v-if="question && question.questionType === 'ESSAY'">
          <text-question-view :question="question" :examQuestion="examQuestion" :key="examQuestion.order" />
        </template>
      </div>
    </transition>
  </div>
</template>

<script>
import QuestionIndex from "./QuestionIndex";
import QuestionBody from "./QuestionBody";
import SingleQuestionView from "./SingleQuestionView";
import MultipleQuestionView from "./MultipleQuestionView";
import BooleanQuestionView from "./BooleanQuestionView";
import FillBlankQuestionView from "./FillBlankQuestionView";
import TextQuestionView from "./TextQuestionView";
import NestedQuestionView from "./NestedQuestionView";
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations } = createNamespacedHelpers("examingHomeModule");

export default {
  name: "QuestionView",
  data() {
    return {
      parentQuestionBody: null,
      question: null,
      updateQuestioning: false // TODO: 更新问题时，隐藏旧内容。可能用不着
    };
  },
  props: {
    examQuestion: Object
  },
  created() {
    this.updateQuestion();
  },
  methods: {
    ...mapMutations(["updateExamQuestion"]),
    async updateQuestion() {
      this.updateQuestioning = true;
      const currentExamQuestion = this.examQuestion; // 避免以后执行时，this.examQuestion换掉了
      const examRecordDataId = this.$route.params.examRecordDataId;

      let qContentRes = null;
      try {
        qContentRes = await this.$http.get(
          "/api/ecs_oe_student/examQuestion/getQuestionContent?questionId=" +
            currentExamQuestion.questionId +
            "&exam_record_id=" +
            examRecordDataId
        );
      } catch (e) {
        this.updateQuestioning = false;
        this.parentQuestionBody = null;
        this.question = null;
        return;
      }
      this.updateQuestioning = false;
      const question = qContentRes.data;

      this.updateExamQuestion({
        order: currentExamQuestion.order,
        getQuestionContent: true
      });

      const transferWellNumberAndTrustInBody = function(repQuestion) {
        //将题干中的三个#替换为下划线
        if (repQuestion.body) {
          //将题干里的&nbsp;换成空格
          repQuestion.body = repQuestion.body
            .toString()
            .replace(new RegExp("&nbsp;", "g"), " ");
          repQuestion.body = repQuestion.body
            .toString()
            .replace(new RegExp("###", "g"), "_______");
          //将题干中的两个##数字##替换为下划线
          // var baseIndex = examQuestion.orders - 1;
          repQuestion.body = repQuestion.body
            .toString()
            .replace(/##(\d+)##/g, function(a, b) {
              return "__" + parseInt(b) + "__";
            });
        }
        // console.log(repQuestion);
      };
      const initQuestion = function(repQuestion) {
        if (
          repQuestion.questionType === "SINGLE_CHOICE" ||
          repQuestion.questionType === "MULTIPLE_CHOICE"
        ) {
          for (
            var i = 0, imax = repQuestion.questionOptionList.length;
            i < imax;
            i++
          ) {
            // repQuestion.options[i].content = $sce.trustAsHtml(
            //   repQuestion.options[i].content
            // );
          }
        }
        if (repQuestion.questionType === "FILL_UP") {
          if (repQuestion.answer && repQuestion.answer.indexOf("##") > -1) {
            //优先使用##
            repQuestion.answerList = repQuestion.answer.split("##");
          } else {
            repQuestion.answerList = [repQuestion.answer];
          }
        }

        transferWellNumberAndTrustInBody(repQuestion);
      };

      //判断是否为套题
      if (question.body) {
        transferWellNumberAndTrustInBody(question);
        for (
          var j = 0, jmax = question.questionUnitList.length;
          j < jmax;
          j++
        ) {
          initQuestion(question.questionUnitList[j]);
        }
        this.parentQuestionBody = question.body;
      } else {
        this.parentQuestionBody = null;
        initQuestion(
          question.questionUnitList[currentExamQuestion.subNumber - 1]
        );
      }

      this.question =
        question.questionUnitList[currentExamQuestion.subNumber - 1];

      {
        // cache next question content
        const currentOrder = currentExamQuestion.order;
        if (currentOrder < this.examQuestionList.length) {
          this.$http.get(
            "/api/ecs_oe_student/examQuestion/getQuestionContent?questionId=" +
              this.examQuestionList[currentOrder].questionId +
              "&exam_record_id=" +
              examRecordDataId
          );
        }
      }
    },
    async toggleSign() {
      this.updateExamQuestion({
        order: this.$route.params.order,
        isSign: !this.examQuestion.isSign
      });
    }
  },
  computed: {
    ...mapState(["examQuestionList"])
  },
  watch: {
    $route: function() {
      this.updateQuestion();
    }
  },
  components: {
    QuestionIndex,
    QuestionBody,
    SingleQuestionView,
    MultipleQuestionView,
    BooleanQuestionView,
    FillBlankQuestionView,
    TextQuestionView,
    NestedQuestionView
  }
};
</script>

<style scoped>
.question-view {
  padding: 20px;
  font-size: 16px;
  text-align: left;
  overflow: auto;
}

.question-header {
  display: flex;
  align-items: center;
}

.star {
  font-size: 36px;
}
.star:hover {
  cursor: pointer;
}

div.hr {
  border-bottom: 1px dashed gray;
}
</style>
