<template>
  <div class="question-view">
    <question-body :questionBody="question.body" :examQuestionId="examQuestion.id"></question-body>
    <div class="ops">
      <div class="score">({{examQuestion.questionScore}}分)</div>
    </div>
    <div class="option">
      <textarea v-model="studentAnswer" class="stu-answer" @blur="answerQuestion" type="text" />
      </div>
    <div class="reset">
      <i-button type="warning" size="large" @click="() => this.answerQuestion(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"]),
    async answerQuestion(reset) {
      if (reset === null) {
        this.studentAnswer = null;
      }
      const order = this.examQuestion.order;
      const studentAnswer = this.studentAnswer;
      await this.$http.post(
        "/api/ecs_oe_student/examQuestion/submitQuestionAnswer",
        [
          {
            order: order,
            studentAnswer: studentAnswer
          }
        ]
      );
      this.updateExamQuestion({
        order: order,
        studentAnswer: studentAnswer
      });
    }
  },
  watch: {
    examQuestion(cur, prev) {
      this.studentAnswer = this.examQuestion.studentAnswer;
    }
  },
  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>
