<template>
  <el-dialog
    modal-append-to-body
    width="70%"
    :visible="visible"
    @close="close"
  >
    <div slot="title">{{title}}</div>
    <div>
      <iframe
        width="100%"
        height="500px"
        frameborder="1"
        :src="src"
      >
        <html>
          <body style="background-color: rgb(38, 38, 38); height: 100%; width: 100%; overflow: hidden; margin: 0px;">
            <embed width="100%" height="100%" name="plugin" id="plugin" :src="src" type="application/pdf">
          </body>
        </html>
      </iframe>
    </div>
    <div slot="footer">
      <el-button type="primary" @click="toExport('EXCEL')">Excel</el-button>
      <el-button type="primary" @click="toExport('PDF')">PDF</el-button>
    </div>
  </el-dialog>
</template>

<script>
const PATH = '/app-web/report/iReportPrinterController/'
const USER_INFO = JSON.parse(window.localStorage.getItem('userInfo') || '{}')
const BASE_PARAMS = `orgId=${USER_INFO.orgId}&projectId=${USER_INFO.projectId}`
export default {
  name: 'Print',
  data () {
    return {
      visible: false, // 显示弹窗
      title: '打印', // 标题
      src: '', // 链接
      baseParams: '',
    }
  },
  methods: {
    // 传入参数展示打印弹窗
    preview (options) {
      this.options = options
      this.title = options.title
      if (options.proxyDefaultParams) {
        // 代理默认值则完全由传入参数控制链接, 目前在 \src\views\StockOut\DeliverGoods\components\head-todo.vue 中存在代理场景
        this.baseParams = ``
      } else {
        this.baseParams = `${BASE_PARAMS}&`
      }
      // ##兼容: IE window.location.origin
      if (!window.location.origin) {
        window.location.origin = `${window.location.protocol}//${window.location.hostname}${window.location.port ? ':' + window.location.port : ''}`
      }
      this.src = `${window.location.origin}${PATH}preview?${this.baseParams}${this.options.qs}`
      console.log('预览地址 src =', this.src)
      this.visible = true
    },
    // 导出操作
    toExport (type) {
      const url = `${PATH}export?${this.baseParams}${this.options.qs}&exportType=${type}`
      window.location.href = url
    },
    // 关闭
    close () {
      this.title = ''
      this.src = ''
      this.visible = false
    }
  }
}
</script>

<style lang="scss">
</style>
