<template>
  <el-dialog class="show-file" :visible="dialogVisibleShowFile" width="900px" @close="dialogShowFileClose">
    <div slot="title">查看文件【{{ fileName }}】</div>

    <template v-if="fileType === 'pdf'">
      <pdf v-for="i in numPages" :key="i" :src="pdfSrc" :page="i" />
    </template>

    <template v-else-if="isImage(fileType)">
      <div class="image-parent">
        <el-image fit="contain" :src="fileUrl" />
      </div>
    </template>

    <div v-else-if="fileType === 'mp3'" class="audio-file">
      <audio :src="fileUrl" controls></audio>
    </div>

    <div v-else-if="fileType === 'mp4'" class="video-file">
      <video :src="fileUrl" controls></video>
    </div>

    <div v-else-if="fileType === 'txt'" class="text-file">
      <el-input v-model="text" type="textarea" :readonly="true" resize="none" />
    </div>

    <template v-else>
      <iframe
        :src="'https://view.officeapps.live.com/op/view.aspx?src=' + `${fileUrl}`"
        width="100%"
        height="490px"
        scrolling="no"
      />
    </template>

    <div slot="footer">
      <el-button @click="dialogShowFileClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import pdf from 'vue-pdf';
import { GetFileStoreInfo } from '@/api/app';
import { getFileType } from '@/utils/filter';

export default {
  components: { pdf },
  props: {
    fileName: {
      default: '',
      type: String
    },
    fileId: {
      default: '',
      type: String
    }
  },
  data() {
    return {
      dialogVisibleShowFile: false,
      pdfSrc: '',
      numPages: 1,
      fileUrl: '',
      text: ''
    };
  },
  computed: {
    fileType() {
      return getFileType(this.fileName);
    }
  },
  watch: {
    fileId(newVal) {
      if (newVal.length > 0) {
        this.getFileStoreInfo();
      }
    }
  },
  methods: {
    getFileStoreInfo() {
      GetFileStoreInfo({ file_id: this.fileId }).then(({ file_url_https, file_relative_path }) => {
        this.fileUrl = file_url_https;

        if (this.fileType === 'pdf') {
          this.getNumPages(file_relative_path);
        }

        if (this.fileType === 'txt') {
          fetch(`${process.env.VUE_APP_PDF}${file_relative_path}`).then(async res => {
            if (!res.ok) return;
            this.text = await res.text();
          });
        }
      });
    },

    getNumPages(url) {
      let loadingTask = pdf.createLoadingTask(`${process.env.VUE_APP_PDF}${url}`);
      loadingTask.promise
        .then(pdf => {
          this.pdfSrc = loadingTask;
          this.numPages = pdf.numPages;
        })
        .catch(err => {
          console.error('pdf加载失败', err);
          this.$message.error('pdf加载失败');
        });
    },

    showDialog() {
      this.dialogVisibleShowFile = true;
    },

    dialogShowFileClose() {
      this.dialogVisibleShowFile = false;
      this.$emit('dialogShowFileClose');
    },

    isImage(type) {
      return ['jpeg', 'gif', 'jpg', 'png', 'bmp', 'pic', 'svg'].includes(type);
    }
  }
};
</script>

<style lang="scss">
@import '~@/styles/mixin';

.show-file {
  @include dialog;

  .el-dialog__header {
    font-weight: bold;
  }

  %image-parent,
  .image-parent {
    display: flex;
    justify-content: center;
  }

  .audio-file {
    @extend %image-parent;
  }

  .video-file {
    @extend %image-parent;
  }

  .text-file {
    height: 60vh;

    .el-textarea,
    textarea {
      height: 100%;
    }
  }
}
</style>
