<template>
  <ele-modal
    title="朔源码"
    :visible.sync="QRvisible"
    v-if="QRvisible"
    width="800px"
    :maxable="true"
  >
    <div id="printSection">
      <div
        style="
          position: relative;
          width: 98mm;
          height: 70mm;
          box-sizing: border-box;
          font-size: 10px;
          color: green;
          margin: 0 auto;
          font-weight: 700;
        "
      >
        <div
          style="
            display: flex;
            flex-direction: row;
            width: 260px;
            height: 260px;
          "
        >
          <img style="width: 100%; height: 100%" :src="url" />
        </div>
      </div>
    </div>

    <div slot="footer">
      <el-button @click="print">打印预览</el-button>
      <el-button @click="close">关闭</el-button>
    </div>
  </ele-modal>
</template>

<script>
  import QRCode from 'qrcode';
  export default {
    data() {
      return {
        batchNo: '',
        createDate: '',
        enforceStandards: '',
        layBy: '',
        level: '',
        netWeight: '',
        notice: '',
        productName: '',
        purchaseOrigins: '',
        specification: '',
        warrantyPeriod: '',
        warrantyPeriodUnit: '',
        weightUnit: '',
        url: '',
        workOrderId: '',
        qrCodeUrl: '',
        QRvisible: false
      };
    },
    mounted() {},

    methods: {
      open(workOrderId) {
        this.QRvisible = true;
        this.workOrderId = workOrderId;

        this.qrCodeUrl =
          window.location.origin + `/traceability?id=${this.workOrderId}`;
        // this.qrCodeUrl = `http://192.168.1.22:9999/traceability?id=${this.workOrderId}`;

        this.$nextTick(() => {
          this.generateQRCodes();
        });
      },

      generateQRCodes() {
        if (this.workOrderId) {
          QRCode.toDataURL(this.qrCodeUrl)
            .then((url) => {
              // item.qrcode = url;
              this.url = url;
              this.$forceUpdate();
            })
            .catch((err) => {
              console.error(err);
            });
        }
      },

      close() {
        this.QRvisible = false;
      },

      print() {
        const printSection = document.getElementById('printSection');
        // 创建打印任务
        const printWindow = window.open('', '_blank');
        printWindow.document.open();
        printWindow.document.write('<html><head><title>打印预览</title>');
        printWindow.document.write(
          '<link rel="stylesheet" href="your-stylesheet-url.css" type="text/css" />'
        );
        printWindow.document.write('</head><body>');
        printWindow.document.write(printSection.innerHTML);
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.onload = function () {
          printWindow.print();
        };
      }

      // print() {
      //   const printSection = document.getElementById('printSection');
      //   // 创建打印任务
      //   const printWindow = window.open('', '_blank');
      //   printWindow.document.open();
      //   printWindow.document.write('<html><head><title>打印预览</title>');
      //   printWindow.document.write(
      //     '<link rel="stylesheet" href="your-stylesheet-url.css" type="text/css" />'
      //   );
      //   printWindow.document.write('</head><body>');
      //   printWindow.document.write(printSection.innerHTML);
      //   printWindow.document.write('</body></html>');
      //   printWindow.document.close();
      //   // printWindow.onload = function () {
      //   //   printWindow.print();
      //   // };
      // }
    }
  };
</script>

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