<template>
  <el-dialog
    title="历史版本"
    :visible.sync="dialogVisible"
    width="50%"
    :before-close="handleClose"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    :append-to-body="true"
    v-loading.fullscreen.lock="fullscreenLoading"
  >
    <template>
      <el-table
        :data="tableData"
        style="width: 100%"
        stripe
        :header-cell-style="{ background: '#F7F7F7' }"
      >
        <el-table-column
          prop="name"
          align="center"
          show-overflow-tooltip
          label="文件名称"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="createUser"
          align="center"
          show-overflow-tooltip
          label="操作人"
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          align="center"
          show-overflow-tooltip
          label="操作时间"
        >
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="download(scope.row)">下载</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import * as api from '@/api/api'
import axios from 'axios'
export default {
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      fullscreenLoading: false
    }
  },
  methods: {
    handleClose() {
      this.dialogVisible = false
    },
    openClose(val) {
      this.dialogVisible = true
      this.listDataInit(val)
    },
    /**
     * @method 历史版本信息
     * **/
    listDataInit(val) {
      let code = {
        id: val.id
      }
      api.GET('/doc/getDocHistoryList', code).then(data => {
        if (data.code == 0) {
          this.tableData = data.data
        }
      })
    },
    /**
     * @method 下载
     * **/
    download(val) {
      this.fullscreenLoading = true
      axios.get(val.docUrl, { responseType: 'blob' }).then(res => {
        //  如果支持微软的文件下载方式(ie10+浏览器)
        if (window.navigator.msSaveBlob) {
          try {
            const blobObject = new Blob([res.data])
            window.navigator.msSaveBlob(blobObject, `${val.name}.pdf`)
          } catch (e) {
            console.log(e)
          }
        } else {
          //  其他浏览器
          const url = window.URL.createObjectURL(new Blob([res.data]))
          const link = document.createElement('a')
          link.href = url
          link.setAttribute('download', `${val.name}.pdf`)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
        this.fullscreenLoading = false
      })
    }
  }
}
</script>