<template>
  <div v-if="isSyncState" class="question-view">
    <question-body :questionBody="question.body" :examQuestion="examQuestion"></question-body>
    <div class="ops">
      <div class="score">({{examQuestion.questionScore}}分)</div>
    </div>
    <div class="option">
      <textarea v-model="studentAnswer" maxlength="5000" class="stu-answer" type="text" />
      </div>
    <div class="reset">
      <i-button type="warning" size="large" @click="studentAnswer=null">重置答案</i-button>
    </div>
  </div>
</template>

<script>
import QuestionBody from "./QuestionBody";
import { createNamespacedHelpers } from "vuex";
const { mapMutations } = createNamespacedHelpers("examingHomeModule");

export default {
  name: "TextQuestionView",
  data() {
    return {
      studentAnswer: this.examQuestion.studentAnswer
    };
  },
  props: {
    question: Object,
    examQuestion: Object
  },
  methods: {
    ...mapMutations(["updateExamQuestion"])
  },
  watch: {
    examQuestion() {
      // console.log(this.examQuestion.studentAnswer);
      this.studentAnswer = this.examQuestion.studentAnswer;
    },
    studentAnswer() {
      let realAnswer = null;
      if (this.studentAnswer) {
        // 如果有实际内容
        realAnswer = this.studentAnswer
          .replace(/<script/gi, "&lt;script")
          .replace(/script>/gi, "script&gt;");
      }
      if (realAnswer !== this.examQuestion.studentAnswer) {
        this.updateExamQuestion({
          order: this.examQuestion.order,
          studentAnswer: realAnswer
        });
      }
    }
  },
  computed: {
    isSyncState() {
      return this.examQuestion.order == this.$route.params.order;
    }
  },
  components: {
    QuestionBody
  }
};
</script>

<style scoped>
.question-view {
  display: grid;
  grid-row-gap: 10px;
}

.question-body {
  font-size: 18px;
  margin-bottom: 10px;
}

.ops {
  display: flex;
  align-items: flex-end;
}

.stu-answer {
  width: 500px;
  min-height: 300px;
}

.option {
  display: flex;
}
.question-options {
  text-align: left;
}
</style>
