<!-- eslint-disable vue/no-v-html -->
<template>
  <div class="essayquestion-preview" v-if="show_preview">
    <div class="stem">
      <span class="question-number">{{ data.property.question_number }}.</span>
      <span v-html="sanitizeHTML(data.stem)"></span>
    </div>
    <div v-if="isEnable(data.property.is_enable_description)" class="description">{{ data.description }}</div>

    <el-input
      v-model="answer.answer_list[0].text"
      rows="12"
      resize="none"
      type="textarea"
      placeholder="请输入"
      :maxlength="1000"
      show-word-limit
      @input="handleInput"
      :readonly="isJudgingRightWrong"
    />
    <SoundRecordPreview :disabled="isJudgingRightWrong" :wav-blob.sync="answer.answer_list[0].audio_file_id" />
    <UploadFiles
      :fille-number="999"
      file-type-name="文件"
      :upload-type="'*'"
      :file-id-list="answer.answer_list[0].accessory_file_id_list"
      upload-title="上传附件："
      @upload="handleUpload"
      @deleteFile="handleDelete"
      :disabled="isJudgingRightWrong"
    />
    <div v-if="isEnable(data.property.is_enable_reference_answer) && isShowRightAnswer" class="reference-box">
      <h5 class="reference-title">参考答案</h5>
      <span class="reference-answer" v-html="sanitizeHTML(data.reference_answer)"></span>
    </div>
  </div>
</template>

<script>
import PreviewMixin from './components/PreviewMixin';
import SoundRecordPreview from './components/common/SoundRecordPreview.vue';
import UploadFiles from './components/common/UploadFiles.vue';

export default {
  name: 'EssayQuestionPreview',
  components: {
    SoundRecordPreview,
    UploadFiles,
  },
  mixins: [PreviewMixin],
  data() {
    return {
      show_preview: false,
    };
  },
  watch: {
    data: {
      handler(val) {
        if (!val || this.data.type !== 'essay_question') return;
        this.handleData();
      },
      deep: true,
      immediate: true,
    },
  },
  created() {},
  methods: {
    // 初始化数据
    handleData() {
      if (!this.isJudgingRightWrong) {
        let obj = {
          audio_file_id: '',
          text: '',
          accessory_file_id_list: [],
        };
        this.answer.answer_list.push(obj);
      }
      this.show_preview = true;
    },
    // 文件上传成功
    handleUpload(fileId) {
      this.answer.answer_list[0].accessory_file_id_list.push(fileId);
    },
    // 删除文件
    handleDelete(fileId) {
      this.answer.answer_list[0].accessory_file_id_list.splice(
        this.answer.answer_list[0].accessory_file_id_list.indexOf(fileId),
        1,
      );
    },
    handleInput(value) {
      if (value.length >= 1000) {
        this.$message.warning(`字数达到1000字！`);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@use '@/styles/mixin.scss' as *;

.essayquestion-preview {
  @include preview;

  :deep .sound-record-wrapper {
    justify-content: center;
  }

  .reference-box {
    padding: 12px;
    background: #f9f8f9;

    .reference-title {
      margin: 0 0 10px;
      font-size: 14px;
      font-weight: 400;
      line-height: 32px;
      color: #4e5969;
    }
  }
}
</style>
