<template>
  <div class="upload-preview" :style="getAreaStyle()">
    <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />

    <div class="main">
      <template v-if="data.file_list.length > 0">
        <template
          v-if="
            data.file_list[0].file_name.indexOf('png') !== -1 ||
            data.file_list[0].file_name.indexOf('jpg') !== -1 ||
            data.file_list[0].file_name.indexOf('jpeg') !== -1
          "
        >
          <el-image
            style="width: 100%; height: 260px"
            :src="data.file_list[0].file_url"
            :fit="'contain'"
            :preview-src-list="[data.file_list[0].file_url]"
          >
          </el-image>
        </template>
        <template v-else-if="data.file_list[0].file_name.indexOf('pdf') !== -1">
          <iframe :src="data.file_list[0].newpath" id="ifm" width="100%" height="260px" frameborder="0"></iframe>
        </template>
      </template>
    </div>
  </div>
</template>

<script>
import UploadFile from './components/UploadView.vue';
import PreviewMixin from '../common/PreviewMixin';
import { getConfig } from '@/utils/auth';
import { GetFileURLMap } from '@/api/app';
const Base64 = require('js-base64').Base64;

import { getUploadPreviewData } from '@/views/book/courseware/data/uploadPreview';

export default {
  name: 'UploadControlPreview',
  components: { UploadFile },
  mixins: [PreviewMixin],
  data() {
    return {
      data: getUploadPreviewData(),
      file_preview_url: getConfig() ? getConfig().doc_preview_service_address : '',
    };
  },
  watch: {
    'data.file_list': {
      handler(val) {
        if (val.length > 0) {
          this.handleData();
        }
      },
      immediate: true,
    },
  },
  created() {},
  methods: {
    // 处理数据
    handleData() {
      this.data.file_list.forEach((item) => {
        GetFileURLMap({ file_id_list: [item.file_id] }).then(({ url_map }) => {
          this.$set(
            item,
            'newpath',
            `${this.file_preview_url}/onlinePreview?url=` + Base64.encode(url_map[item.file_id]),
          );
        });
      });
    },
  },
};
</script>

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

.upload-preview {
  @include preview-base;
}
</style>
