<template>
  <el-dialog class="onLineView" :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag
    append-to-body>
    <!-- <div class="layout-file">
      <div class="layout-file-title">{{ docx }}</div>
      <vue-office-docx :src="docx" @rendered="rendered" />
    </div> -->
    <!-- <div class="layout-file">
      <div class="layout-file-title">{{ excel }}</div>
      <vue-office-excel :src="excel" @rendered="rendered" />
    </div> -->
    <div class="layout-file">
      <div class="layout-file-title">{{ pdf }}</div>
      <vue-office-pdf style="height: 100%;" :src="pdf"/>
    </div>  
    <el-button @click="dialogVisible = false">关 闭</el-button>
  </el-dialog>
</template>

<script>
import * as searchPageApi from '@/api/manager/digsearch';

//引入相关样式
import '@vue-office/docx/lib/index.css'
import '@vue-office/excel/lib/index.css'

//引入VueOffice组件
// import VueOfficeDocx from '@vue-office/docx'
import VueOfficePdf from '@vue-office/pdf'

export default {
    name: "pdf",
    components: {
      VueOfficePdf
    },
    data() {
      return {
        dialogTitle: "",
        dialogVisible: false,
        // 地址
        // pdf: 'https://501351981.github.io/vue-office/examples/dist/static/test-files/test.pdf',
        pdf: '',

        // docx: 'https://501351981.github.io/vue-office/examples/dist/static/test-files/test.docx',
      };
    },
    methods: {
      async open(data) {
        this.dialogVisible = true;
        this.rendered();
      },
      rendered() {
        const storedData = localStorage.getItem('detailData');
        this.detailData = JSON.parse(storedData);
        const searchParams = {
          filePath: this.detailData.file_path,
          sourcename: this.detailData.sourcename,
          fileName: this.detailData.file_name,
          fileType: this.detailData.file_type
        };
        searchPageApi.getDownload(searchParams)
          .then(response => {
            const blob = new Blob([response], { type: 'application/octet-stream' });
            // const blob = new Blob([response], { type: 'arraybuffer' });
            const url = URL.createObjectURL(blob);
            console.log('url=' + url);
            this.pdf = url;
          })
          .catch(error => {
            console.error('文件预览失败！:', error);
          });
      }

    }
  };
</script>
<style lang="scss" scoped>
.layout-file {
  width: 98%;
  height: 50%;
  float: left;
  margin-top: 0.8rem;
  margin-right: 0.8rem;
  position: relative;
  border: 1px solid #0f5de5;
  border-radius: 0.3rem;

  &:nth-child(1),
  &:nth-child(2) {
    margin-top: 0;
  }

  .layout-file-title {
    position: absolute;
    top: 0.5rem;
    right: 1.5rem;
    background-color: rgba(255, 0, 0, 0.8);
    padding: 0.1rem 0.4rem;
    z-index: 99;
    color: #fff;
    border-radius: 0.3rem;
    font-size: 12px;
  }
}

::v-deep .el-dialog {
  width: 96% !important;
  left: 2% !important;
}
</style>
