<template>
  <div>
    <Modal v-model="modalShow" title="打印" @on-ok="printAsync">
      <span>打印模板</span>
      <Select v-model="printTemplate" clearable>
        <Option
          v-for="item in printTemplateList"
          :value="item.name"
          :key="item.name"
          >{{ item.title }}</Option
        >
      </Select>
    </Modal>
  </div>
</template>
<script>
export default {
  props: {},
  data() {
    return {
      subName: null,
      modalShow: false,
      printTemplate: "",
      printTemplateList: [],
      billDefineName: null,
      billId: null,
    };
  },
  watch: {
    billDefineName: {
      handler(newVal, oldVal) {
        this.billDefineName = newVal;
        this.initSelect();
      },
    },
    subName: {
      handler(newVal, oldVal) {
        this.subName = newVal;
      },
    },
  },
  methods: {
    printAsync() {
      //打印接口
      return new Promise((resolve, reject) => {
        // let url=`/gms/bill/print/${this.billDefineName}/${this.billId}/${this.printTemplate}`;
        let url = `/rbcfunc/print/merge/${this.billDefineName}/${this.billId}/${this.printTemplate}?detailName=BXMXZB`;
        GMS.$http
          .post(url, JSON.stringify(), {
            responseType: "blob",
            headers: {
              "Content-Type": "application/pdf;charset=utf-8",
              "X-Requested-With": "XMLHttpRequest",
            },
          })
          .then((res) => {
            let blob = new Blob([res.data], {
              type: "application/pdf;charset=utf-8",
            });
            let downloadElement = document.createElement("a");
            let href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.href = href;
            window.open(href)  
            // let routeData = GMS.routerManager.getRouter().resolve({
            //   path: "/showPrint",
            //   query: { src: href },
            // });
            // window.open(routeData.href, "_blank");
          });
      });
    },
    initSelect() {
      //获取打印模板
      let url = `/common/print/${this.billDefineName}/template`;
      GMS.$http
        .get(url, JSON.stringify(), {
          headers: {
            GET: {
              "Content-Type": "application/json",
            },
          },
        })
        .then((res) => {
          this.printTemplateList = res.data;
        });
    },
  },
  mounted() {},
};
</script>
<style scoped lang="less"></style>
