<template>
  <el-dialog :title="params.name" :visible.sync="dialogVisible" custom-class="pdf-dlg" :fullscreen="isFullscreen" :show-close="false">
    <span slot="title" class="dialog-title">
      <el-row>
        <el-col :span="21">{{params.name}}</el-col>
        <el-col :span="3" style="text-align:end">
          <i v-if="!isFullscreen" class="fa fa-window-maximize" aria-hidden="true" @click="isFullscreen=true" style="cursor: pointer"></i>
          <i v-else class="fa fa-window-restore" aria-hidden="true" @click="isFullscreen=false" style="cursor: pointer"></i>&nbsp;&nbsp;
          <i class="fa fa-refresh" aria-hidden="true" @click="getFileFullPath" style="cursor: pointer"></i>&nbsp;&nbsp;
          <i class="fa fa-times" aria-hidden="true" @click="dialogVisible=false" style="cursor: pointer"></i>
        </el-col>
      </el-row>
    </span>
    <div class="pdf-content">
      <pdf
        v-if="pdfParams.src"
        :src="pdfParams.src"
        :page="pdfParams.pageIndex"
        @progress ='onProgress'
        @password='onPassword'
        @num-pages="pdfParams.pageSize=$event"
        @page-loaded="pdfParams.pageIndex=$event"
      >
      </pdf>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-pagination background layout="prev, pager, next" :page-size="1" :total="pdfParams.pageSize" @current-change="pdfParams.pageIndex=$event" ></el-pagination>
    </span>
  </el-dialog>
</template>
<script>
import pdf from '@src/components/PDF.vue'
export default {
  props: {},
  data() {
    return {
      isFullscreen: false,
      pdfParams: {
        src: '',
        pageSize: 1,
        pageIndex: 1
      },
      pdfParamsBak: {},
      params: {},
      dialogVisible: false,
      loadPercent: 0
    }
  },
  watch: {},
  methods: {
    init(params) {
      Object.assign(this.params, params)
      this.dialogVisible = true
      this.isFullscreen = false
      this.getFileFullPath()
    },
    async getFileFullPath() {
      Object.assign(this.pdfParams, this.pdfParamsBak)
      let res = await axios.get('/ui/core/file/filefullpath', { params: this.params })
      let data = res.data
      if (data.status === 1) {
        this.pdfParams.src = data.data.fullpath
      // console.log(this.pdfParams.src)
      } else {
        this.pdfParams.src = ''
        this.$alert('获取文件路径失败 ！')
      }
    },
    onProgress(percent) {
      this.loadPercent = (percent * 100).toFixed()
    },
    onPassword(pwd, r) {
      let input = prompt(r === 'INCORRECT_PASSWORD' ? '密码错误 ！ 请重新输入密码：' : '请输入文档密码:')
      if (input === null) {
        this.dialogVisible = false
        return
      }
      pwd(input)
    }
  },
  components: {
    pdf
  },
  created() {
    Object.assign(this.pdfParamsBak, this.pdfParams)
  }
}
</script>
<style>
@media (min-width: 1025px) {
  .pdf-dlg .el-dialog__body {
    max-height: -webkit-fill-available !important;
  }
}
</style>
