<template>
  <div>
    <a-button type="primary" icon="download" @click="downWord">下载</a-button>
    <a-button type="primary" style="left: 10px" @click="previewWord">预览并打印</a-button>

    <div style="width: 100%;height: 100%;">
      <pdf v-show="false" ref="pdfWord" :src="pdfSrc" class="pdf" style=""></pdf>
    </div>
  </div>

</template>

<script>
  const VALIDATE_NO_PASSED = Symbol()
  import pick from 'lodash.pick'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { getAction, downFile } from '@/api/manage'
  import pdf from 'vue-pdf-signature'
  import CMapReaderFactory from 'vue-pdf-signature/src/CMapReaderFactory'

  export default {
    name: 'ProcessMaterialsPlanStatement',
    mixins: [JeecgListMixin, mixinDevice],
    components: { pdf, CMapReaderFactory },
    props: {
      materWared: {
        type: String
      },
      /**/
      procInstId: {
        type: String,
        default: '',
        required: false
      },
      pictureId: {
        type: String,
        default: '',
        required: false
      },
      lcModa: {
        type: Object,
        required: false
      }
    },

    data() {
      return {
        showSessionId: '',
        srcHtml: '',
        modalLsVisible: false,
        pdfWord: '',
        pdfSrc: '',
        url: {
          list: '/hy/processUdgetPlan/getPlanTable',
          queryPlandDownWord: '/word/queryPlandTableDownWord',
          queryPlanPrevie: '/word/queryPlanTablePrevie'
        },
        Wslist: null
      }
    },
    mounted() {
      this.showSessionId = window.location.search
    },
    created() {
      downFile(this.url.queryPlanPrevie, { id: this.queryParam.id }).then((data) => {
        console.log(data)
        const blob = new Blob([data], { type: 'application/pdf' })
        this.pdfWord = window.URL.createObjectURL(blob)

      })
      // this.loadData();
    },
    activated() {

    },
    beforeMount() {

    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.queryPlanApply}`
      }
    },
    methods: {
      // 下载
      downWord() {
        downFile(this.url.queryPlandDownWord, { id: this.queryParam.id }).then((data) => {
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          const blob = new Blob([data]) // 把得到的结果用流对象转一下
          var a = document.createElement('a') //创建一个<a></a>标签
          a.href = URL.createObjectURL(blob) // 将流文件写入a标签的href属性值
          a.download = '采购申请表.docx' //设置文件名
          a.style.display = 'none' // 障眼法藏起来a标签
          document.body.appendChild(a) // 将a标签追加到文档对象中
          a.click() // 模拟点击了a标签，会触发a标签的href的读取，浏览器就会自动下载了
          a.remove()
        })

      },
      previewWord() {
        downFile(this.url.queryPlanPrevie, { id: this.queryParam.id }).then((data) => {
          if (!data) {
            this.$message.warning("文件预览失败")
            return
          }
          console.log(data)
          const blob = new Blob([data], { type: 'application/pdf' })
          this.pdfSrc = window.URL.createObjectURL(blob)
          console.log(this.pdfSrc)
          window.open(this.pdfSrc)//新窗口打开，借用浏览器去打印
        })
      },
      loadData() {
        this.queryParam.id = this.materWared
      },


    }
  }
</script>

<style lang="less" scoped>

</style>