<!-- eslint-disable vue/no-v-html -->
<template>
  <div class="writepicture-preview">
    <div class="stem">
      <span class="question-number">{{ data.property.question_number }}.</span>
      <span v-html="sanitizeHTML(data.stem)"></span>
    </div>
    <div class="article-content" v-html="sanitizeHTML(data.article)"></div>
    <div v-if="isEnable(data.property.is_enable_description)" class="description">
      {{ data.description }}
    </div>
    <div class="content">
      <div class="content-left">
        <el-carousel
          type="card"
          height="276px"
          :autoplay="false"
          indicator-position="none"
          arrow="always"
          @change="changeImg"
        >
          <el-carousel-item v-for="(item, index) in data.option_list" :key="index">
            <el-image
              v-if="pic_list[item.picture_file_id]"
              style="width: 370px; height: 276px"
              :src="pic_list[item.picture_file_id]"
              fit="contain"
            />
          </el-carousel-item>
        </el-carousel>
        <h3 class="pic-title" v-html="sanitizeHTML(data.option_list[active_index].picture_title)"></h3>
        <p class="pic-info" v-html="sanitizeHTML(data.option_list[active_index].picture_info)"></p>
      </div>
      <div class="content-right">
        <el-input
          v-model="answer_list[active_index].value"
          rows="12"
          resize="none"
          type="textarea"
          placeholder="请输入"
          :maxlength="data.property.word_num"
          show-word-limit
          @input="handleInput"
        />
      </div>
    </div>
    <template v-if="isEnable(data.property.is_enable_upload_accessory)">
      <!-- 上传附件 -->
      <UploadFiles
        :fille-number="999"
        file-type-name="文件"
        :upload-type="'*'"
        :file-id-list="answer_list[active_index].accessory_file_id"
        upload-title="上传附件："
        @upload="handleUpload"
        @deleteFile="handleDelete"
      />
    </template>
    <template v-if="isEnable(data.property.is_enable_sample_text)">
      <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" v-html="sanitizeHTML(data.sample_text)"></div>
    </template>
  </div>
</template>

<script>
import PreviewMixin from './components/PreviewMixin';
import { GetFileStoreInfo } from '@/api/app';

import UploadFiles from './components/common/UploadFiles.vue';

export default {
  name: 'WritePicturePreview',
  components: {
    UploadFiles,
  },
  mixins: [PreviewMixin],
  data() {
    return {
      show_sample_text: false,
      pic_list: {},
      active_index: 0,
      answer_list: [],
    };
  },
  created() {
    this.handleData();
  },
  methods: {
    // 初始化数据
    handleData() {
      this.answer_list = [];
      this.pic_list = {};
      this.active_index = 0;
      this.data.file_id_list.forEach((item) => {
        GetFileStoreInfo({ file_id: item }).then(({ file_id, file_url }) => {
          this.$set(this.pic_list, file_id, file_url);
        });
      });
      this.data.option_list.forEach((item) => {
        let obj = {
          mark: item.mark,
          value: '',
          accessory_file_id: [], // 上传文件列表
        };
        this.answer_list.push(obj);
      });
    },
    changeImg(index) {
      this.active_index = index;
    },
    // 文件上传成功
    handleUpload(fileId) {
      this.user_answer[this.active_index].accessory_file_id.push(fileId);
    },
    // 删除文件
    handleDelete(fileId) {
      this.user_answer[this.active_index].accessory_file_id.splice(
        this.user_answer.accessory_file_id.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 *;

.writepicture-preview {
  @include preview;

  :deep p {
    margin: 0;
  }

  .content {
    display: flex;
    column-gap: 24px;

    &-left {
      flex-shrink: 0;
      width: 478px;

      :deep .el-carousel__item--card {
        width: 77%;
        margin-left: -13.5%;
      }

      .el-image {
        opacity: 0.2;
      }

      .el-carousel__item--card.is-active {
        .el-image {
          background: #fff;
          opacity: 1;
        }
      }

      .pic-title {
        margin: 8px 0 4px;
        font-size: 12px;
        font-weight: 600;
        line-height: 20px;
        color: #000;
        word-break: break-word;
      }

      .pic-info {
        margin: 0;
        font-size: 12px;
        font-weight: 400;
        line-height: 20px;
        color: #000;
        word-break: break-word;
      }
    }

    :deep .el-carousel__arrow:focus {
      outline: none;
    }

    &-right {
      flex: 1;

      .el-textarea {
        height: 276px;
        margin-bottom: 16px;
      }
    }
  }

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

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

  .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 {
    :deep p {
      margin: 0;
    }
  }

  :deep .el-textarea .el-input__count {
    bottom: 15px;
    background-color: #f2f3f5;
  }
}
</style>
