<template>
  <div class="detail-question-part">
    <div class="detail-question-header">
      <div>
        <el-tag size="mini" effect="dark" type="success">{{
          question.questionType | questionType
        }}</el-tag>
        <span class="inline-middle margin-left-10">小题分数：</span>
        <el-input-number
          v-model="score"
          placeholder="请输入小题分数"
          size="mini"
          :step="0.1"
          :min="0.1"
          :max="999"
          :controls="false"
          :precision="1"
          step-strictly
          @change="scoreChange"
        >
        </el-input-number>
      </div>
      <div>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-edit"
          title="编辑"
          @click.stop="editQuestion"
        ></el-button>
        <el-button
          class="btn-danger"
          size="mini"
          type="text"
          icon="el-icon-delete"
          title="删除"
          @click.stop="removeQuestion"
        ></el-button>
      </div>
    </div>
    <div v-if="quesBody" class="detail-question-body">
      <rich-text :text-json="quesBody"></rich-text>
    </div>
    <div class="detail-question-subqs">
      <template v-for="(subq, sindex) in question.subQuestions">
        <build-paper-question-base
          :key="subq.id"
          v-model="subq.score"
          :question="subq"
          :question-serialno="sindex + 1"
          :show-action="false"
        ></build-paper-question-base>
      </template>
    </div>
  </div>
</template>

<script>
import BuildPaperQuestionBase from "./BuildPaperQuestionBase.vue";
import { deepCopy } from "@/plugins/utils";

export default {
  name: "BuildPaperQuestionNested",
  components: { BuildPaperQuestionBase },
  props: {
    question: {
      type: Object,
      default() {
        return {};
      },
    },
    questionSerialno: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      score: "",
      quesBody: null,
    };
  },
  created() {
    if (this.questionSerialno) {
      if (this.question.quesBody) {
        this.quesBody = deepCopy(this.question.quesBody);
        this.quesBody.sections[0].blocks.unshift({
          type: "text",
          value: this.questionSerialno + "、",
        });
      } else {
        this.quesBody = {
          sections: [
            {
              blocks: [
                {
                  type: "text",
                  value: this.questionSerialno + "、",
                },
              ],
            },
          ],
        };
      }
    }
  },
  methods: {
    scoreChange() {
      // 这里通过引用关系直接修改了小题分值
      this.question.subQuestions.forEach((subq) => {
        subq.score = this.score;
      });
    },
    editQuestion() {
      this.$emit("on-edit", this.question);
    },
    removeQuestion() {
      this.$emit("on-remove", this.question);
    },
  },
};
</script>
