<template>
  <div>
    <a-button
      key="1"
      type="default"
      v-if="$hasPermissions('B_transferReturnPrint')"
      id="editGrap-db-print-btn"
      :disabled="disabled"
      @click="handlePrint('views')">打印预览</a-button>
    <a-button
      key="2"
      type="primary"
      class="button-info"
      v-if="$hasPermissions('B_transferReturnPrint')"
      id="editGrap-dbfl-print-btn"
      :disabled="disabled"
      @click="handlePrint('print')">快捷打印</a-button>
    <a-divider type="vertical" />
    <a-button
      key="3"
      type="primary"
      class="button-warning"
      v-if="$hasPermissions('B_transferReturnExport')"
      id="editGrap-export-btn"
      :disabled="disabled"
      @click="handleExcel">导出Excel</a-button>
    <!-- 打印 -->
    <div id="print"></div>
  </div>
</template>

<script>
import { hdExportExcel } from '@/libs/exportExcel'
import { allocateReturnExcel, allocateReturnPrint } from '@/api/allocateReturn'
export default {
  props: {
    params: {
      type: Object,
      default: function () {
        return {}
      }
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handlePrint (type) {
      const _this = this
      const params = {
        allocateReturnSn: this.params.allocateReturnSn || ''
      }
      this.$emit('loading')
      allocateReturnPrint(params).then(res => {
        _this.spinning = false
        if (res.type == 'application/json') {
          var reader = new FileReader()
          reader.addEventListener('loadend', function () {
            const obj = JSON.parse(reader.result)
            _this.$notification.error({
              message: '提示',
              description: obj.message
            })
          })
          reader.readAsText(res)
        } else {
          this.print(res, type)
        }
        _this.$emit('unloading')
      })
    },
    print (data, type) {
      if (!data) {
        return
      }
      const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
      console.log(url)
      document.getElementById('print').innerHTML = '<iframe id="printftoe" name="printftoe" src="' + url + '" hidden></iframe>'
      if (type == 'views') { //  预览
        window.open(url)
      } else if (type == 'print') { //  打印
        window.frames['printftoe'].focus()
        window.frames['printftoe'].print()
      }
    },
    // 导出
    handleExcel () {
      const _this = this
      this.$emit('loading')
      hdExportExcel(allocateReturnExcel, { allocateReturnSn: this.params.allocateReturnSn || '' }, '调拨退货单明细', function () {
        _this.$emit('unloading')
      })
    }
  }
}
</script>

<style>
</style>
