<template>
  <div v-if="question && examQuestion" class="question-view">
    <div class="sign-button">
      <Icon :type="examQuestion.isSign ? 'ios-star':'ios-star-outline'" :style="{color: '#ffcc00'}" class="star" @click="toggleSign" />
    </div>
    <div v-if="parentQuestionBody" class="question-view">
      <question-body :questionBody="parentQuestionBody" :examQuestionId="examQuestion.id" style="margin-bottom: 20px" :key="this.examQuestion.order"></question-body>
      <div class="hr" />
    </div>
    <template v-if="question.questionType === 'SINGLE_CHOICE'">
      <single-question-view :question="question" :examQuestion="examQuestion" :key="this.examQuestion.order" />
    </template>
    <template v-if="question.questionType === 'MULTIPLE_CHOICE'">
      <multiple-question-view :question="question" :examQuestion="examQuestion" :key="this.examQuestion.order" />
    </template>
    <template v-if="question.questionType === 'TRUE_OR_FALSE'">
      <boolean-question-view :question="question" :examQuestion="examQuestion" :key="this.examQuestion.order" />
    </template>
    <template v-if="question.questionType === 'FILL_UP'">
      <fill-blank-question-view :question="question" :examQuestion="examQuestion" :key="this.examQuestion.order" />
    </template>
    <template v-if="question.questionType === 'ESSAY'">
      <text-question-view :question="question" :examQuestion="examQuestion" :key="this.examQuestion.order" />
    </template>
  </div>
</template>

<script>
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 { mapMutations } = createNamespacedHelpers("examingHomeModule");

export default {
  name: "QuestionView",
  data() {
    return {
      parentQuestionBody: null,
      question: null
    };
  },
  props: {
    examQuestion: Object
  },
  created() {
    this.updateQuestion();
  },
  methods: {
    ...mapMutations(["updateExamQuestion"]),
    async updateQuestion() {
      const res = await this.$http.get(
        "/api/ecs_oe_student/examQuestion/getQuestionContent?questionId=" +
          this.examQuestion.questionId
      );
      const question = res.data;

      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]);
        }
        // 对子题进行排序
        // if (question.questionUnitList && question.questionUnitList.length > 0) {
        //   question.questionUnitList.sort(function(a, b) {
        //     if (a.quesNumber > b.quesNumber) {
        //       return 1;
        //     } else if (a.quesNumber < b.quesNumber) {
        //       return -1;
        //     } else {
        //       return 0;
        //     }
        //   });
        // }
        this.parentQuestionBody = question.body;
      } else {
        this.parentQuestionBody = null;
        initQuestion(
          question.questionUnitList[this.examQuestion.subNumber - 1]
        );
      }

      this.question =
        question.questionUnitList[this.examQuestion.subNumber - 1];
    },
    async toggleSign() {
      await this.$http.post(
        "/api/ecs_oe_student/examQuestion/submitQuestionAnswer",
        [
          {
            order: this.examQuestion.order,
            isSign: !this.examQuestion.isSign
          }
        ]
      );
      this.updateExamQuestion({
        order: this.$route.params.order,
        isSign: !this.examQuestion.isSign
      });
    }
  },
  watch: {
    $route: function() {
      this.updateQuestion();
    }
  },
  components: {
    QuestionBody,
    SingleQuestionView,
    MultipleQuestionView,
    BooleanQuestionView,
    FillBlankQuestionView,
    TextQuestionView,
    NestedQuestionView
  }
};
</script>

<style scoped>
.question-view {
  padding: 20px;
  font-size: 16px;
  text-align: left;
}
.star {
  font-size: 36px;
}
.star:hover {
  cursor: pointer;
}

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