<template>

</template>

<script>
import {formMixin} from '@/mixins/form'
import {hiprint} from "vue-plugin-hiprint";
import {mapMutations} from "vuex";

export default {
  mixins: [formMixin],
  components: {
    print
  },
  name: 'wx-print',
  data() {
    return {
      templateList: [],
      // docs:{
      //   // 销售订单
      //   'order':{'docName':this.$config.docCode.order},
      //   // 入库单
      //   'entry':{'docName':this.$config.docCode.entry},
      //   // 出库单
      //   'out':{'docName':this.$config.docCode.out},
      //   // 库存调整
      //   'ivt_transfer':{'docName':this.$config.docCode.ivt_transfer},
      //   // 库存冻结
      //   'ivt_freeze':{'docName':this.$config.docCode.ivt_freeze},
      // },
    }
  },
  methods: {
    ...mapMutations(['setSvcCode']),

    dataURLtoFile: (dataurl, filename) => {
      let arr = dataurl.split(',');
      let mime = arr[0].match(/:(.*?);/)[1];
      let bstr = atob(arr[1]);
      let n = bstr.length;
      let u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      //转换成file对象
      return new File([u8arr], filename, {type: mime});
    }
  },
  created() {
  },
  mounted() {
    console.log('this.$route.query',this.$route.query)
    if (!this.$route.query.layout_id) {
      this.$Message.warning('请先选择一个模板再打印')
      return
    }
    if (!this.$route.query.data_id) {
      this.$Message.warning('请先选择一条数据再打印')
      return
    }
    if (!this.$route.query.doc) {
      this.$Message.warning('请先选择单据类型再打印')
      return
    }
    if (!this.$route.query.uuid) {
      this.$Message.warning('请先设置唯一id')
      return
    }
    if (!this.$route.query.svc_code) {
      this.$Message.warning('请先设置svc_code')
      return
    }

    this.setSvcCode(this.$route.query.svc_code)
    let doc = this.$route.query.doc;
    let layout = this.excute(this.$service.printLayoutService, this.$service.printLayoutService.selectByCond, { flgValid: true, docName: this.$config.docCode[doc], })
    let data = this.excute(this.$service.printLayoutService,this.$service.printLayoutService.getPrintData,{dataId:this.$route.query.data_id,objectCode:this.$route.query.doc})
    // let header=this.excuteNoParam(this.docs[this.$route.query.doc].service, this.docs[this.$route.query.doc].service.selectById,[this.$route.query.data_id])
    // let detail=this.excuteNoParam(this.docs[this.$route.query.doc].service, this.docs[this.$route.query.doc].service.selectOrderInfoById, [this.$route.query.data_id])
    Promise.all([data,layout])
      .then((results) => {
        console.log('data',data)
        console.log('layout',layout)

        // 在这里执行接下来的操作
        if(results?.[0]?.code === this.$config.SUCCESS_CODE){
          let header =results?.[0]?.data
          let detail =results?.[0]?.data.itemList
          let layout =results?.[1]?.data?.list.find(it => it.layoutId == this.$route.query.layout_id)?.layoutData
          this.$set(header, 'table', detail)

          let printData = {...header}

          let hiprintTemplate = new hiprint.PrintTemplate({
            template: layout,
          })
          hiprintTemplate.toPdf(printData,'单据打印',{isDownload:false,type:'datauristring'}).then(dataURL=>{
            let pdf = this.dataURLtoFile(dataURL, "pdf");
            let formData = new FormData();
            formData.append('file', pdf);
            this.excute(this.$service.fileService, `upload?folder=pdf&uuid=${this.$route.query.uuid}`,formData).then(res => {
              if (res.code === this.$config.SUCCESS_CODE) {
                console.log('upload-res',res)
              }
            })

          })
        }else {
          this.$Message.warning(this.$t('W_006'))
        }
      })
  }

}
</script>

<style scoped>

</style>
