<!-- eslint-disable vue/no-v-html -->
<template>
  <div class="write-preview">
    <div class="stem">
      <span class="question-number" :style="{ fontSize: data.property.stem_question_number_font_size }">
        {{ questionNumberEndIsBracket(data.property.question_number) }}
      </span>
      <span v-html="sanitizeHTML(data.stem)"></span>
    </div>
    <div
      v-if="isEnable(data.property.is_enable_description)"
      class="description rich-text"
      v-html="sanitizeHTML(data.description)"
    ></div>
    <el-input
      class="write-input"
      v-model="answer.answer_list[0].text"
      rows="3"
      type="textarea"
      placeholder="请输入内容"
      :maxlength="data.property.word_num"
      show-word-limit
      :readonly="disabled"
      @input="handleInput"
      :autosize="{ minRows: 3 }"
    />

    <template v-if="isEnable(data.property.is_enable_upload_accessory)">
      <!-- 上传附件 -->
      <UploadFiles
        :fille-number="999"
        file-type-name="文件"
        :upload-type="'*'"
        :file-id-list="answer.answer_list[0].accessory_file_id_list"
        :disabled="disabled"
        @upload="handleUpload"
        @deleteFile="handleDelete"
      />
    </template>
    <template v-if="isEnable(data.property.is_enable_sample_text) && isShowRightAnswer">
      <el-divider content-position="center"
        ><span
          :class="['sample-text', show_sample_text ? 'sample-show' : 'sample-hide']"
          @click="show_sample_text = !show_sample_text"
          >{{ show_sample_text ? '隐藏范文' : '查看范文' }}</span
        ></el-divider
      >
      <div v-if="show_sample_text" class="article-content rich-text" v-html="sanitizeHTML(data.sample_text)"></div>
    </template>
  </div>
</template>

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

export default {
  name: 'WritePreview',
  components: {
    UploadFiles,
  },
  mixins: [PreviewMixin],
  data() {
    return {
      show_sample_text: false,
    };
  },
  watch: {
    isJudgingRightWrong: {
      handler(val) {
        if (!val) {
          this.answer.answer_list.push({
            text: '', // 用户文章
            accessory_file_id_list: [], // 上传文件列表
          });
        }
      },
      immediate: true,
    },
    data: {
      handler(val) {
        if (!val || this.data.type !== 'write') return;
      },
      deep: true,
      immediate: true,
    },
  },
  created() {},
  methods: {
    // 文件上传成功
    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 >= this.data.property.word_num) {
        this.$message.warning(`字数达到${value.length}字！`);
      }
    },
  },
};
</script>

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

.write-preview {
  @include preview;

  .el-divider--horizontal {
    margin: 12px 0;
  }

  .sample-text {
    font-size: 14px;
    font-weight: 400;
    line-height: 30px;
    color: #000;
    cursor: pointer;

    &.sample-show {
      color: $light-main-color;
    }
  }

  .article-content,
  .description {
    :deep p {
      margin: 0;
    }
  }

  .write-input {
    font-size: 16px;
  }

  :deep .el-textarea .el-input__count {
    font-size: 14px;
    background-color: #f2f3f5;
  }
}
</style>
