<!--
 * @Description:
 * @Autor: renchao
 * @LastEditTime: 2024-03-05 16:43:23
-->
<template>
  <div>
    <div class="zsdy-content" v-Loading="loading">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="120px">
        <el-form-item label="印刷序列号:" prop="ysxlh">
          <el-row>
            <el-col :span="18">
              <el-select v-model="ruleForm.ysxlh" @change="handleSelect" :disabled="disabled" placeholder="请选择">
                <el-option
                  v-for="item in ysxlh"
                  :key="item.ysxlh"
                  :label="item.ysxlh"
                  :value="item.ysxlh">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4" v-if="this.formData.bdcqzlx==1">
              <el-tabs v-model="activeTitle">
                <el-tab-pane label="第一页" name="title1"></el-tab-pane>
                <el-tab-pane label="第二页" name="title2"></el-tab-pane>
              </el-tabs>
            </el-col>
            <el-col :span="2" v-if="this.formData.ysxlh" style="float:right">
              <el-button type="primary" @click="handleZF" v-if="disabled">作废</el-button>
            </el-col>
          </el-row>

        </el-form-item>
      </el-form>
      <div class="zs-content">
        <canvas ref="zs1" width="1024" v-show="this.formData.bdcqzlx==1 && activeTitle=='title1'" height="739"></canvas>
        <canvas ref="zs" width="1024" v-show="this.formData.bdcqzlx==1 && activeTitle=='title2'" height="739"></canvas>
        <canvas ref="zm" width="1169" v-show="this.formData.bdcqzlx==2" height="828"></canvas>
      </div>
    </div>
    <!-- 打印模板需要此模块 -->
    <object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" v-show="false">
      <embed id="LODOP_EM" type="application/x-print-lodop" width="1180" height="720" pluginspage="install_lodop32.exe" />
    </object>
    <div class="text-center pt-10">
      <el-button @click="$popupCacel">取消</el-button>
      <el-button type="primary" @click="handlePrint" v-if="disabled">打印</el-button>
      <el-button type="primary" @click="handleSubmit" v-else>确定</el-button>
    </div>

    <el-dialog title="证书作废" :visible.sync="invalidDiglog" width="40%" :modal-append-to-body="false" top="30vh">
      <div class="invalid-diglog">
        <div class="invalid-title">
          <i class="el-icon-question invalid-icon"></i>
          <div class="invalid-body">您确定作废证书并再次打印?印刷序列号{{bdcqz.ysxlh}}</div>
        </div>
        <div class="invalid-reson">作废原因:</div>
        <el-input v-model="zfyy" placeholder="请输入作废原因" type="textarea" :rows="4"></el-input>
        <div class="text-center pt-10">
          <el-button @click="closeInvalidDiglog">取 消</el-button>
          <el-button type="primary" @click="confirmInvalid">确 定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import QRCode from 'qrcode'
  import store from '@/store/index.js'
  import { datas } from "../../javascript/zsyl.js";
  import { getPrintTemplateByCode } from "@/api/print";
  import { getLodop } from "@/utils/LodopFuncs"
  import { readYsxlh, certificate, getBdcqzDetail, invalidCertificate } from "@/api/bdcqz.js";
  export default {
    props: {
      formData: {
        type: Object,
        default: () => {
          return {}
        }
      }
    },
    data () {
      return {
        activeTitle: 'title1',
        // 详细信息
        bdcqz: {},
        key: 0,
        disabled: false,
        // 不动产证书图片地址
        imgSrc1: require('@/image/bdcqz/bdcqzs1.jpg'),
        imgSrc: require('@/image/bdcqz/bdcqzs2.jpg'),
        bdczmSrc: require('@/image/bdcqz/bdczm.jpg'),
        loading: false,
        //印刷序列号集合
        ysxlh: [],
        //列名称对象
        columns: [],
        //tab选择绑定值
        activeName: '',
        invalidDiglog: false,
        zfyy: "",
        ruleForm: {
          bsmBdcqz: "",
          szmc: "不动产权证书",
          bdcqzlx: "",
          szzh: "",
          ysxlh: ""
        }
      }
    },
    mounted () {
      store.dispatch('user/refreshPage', false)
      this.columns = datas.columns()
      this.ysxlhList()
      // 获取详细信息
      this.getDetail()
      if (this.formData.ysxlh) {
        this.disabled = true
      } else {
        this.disabled = false
      }
    },
    methods: {
      /**
       * @description: 获取详细信息
       * @author: renchao
       */
      getDetail () {
        this.loading = true
        getBdcqzDetail(this.formData.bsmBdcqz).then(res => {
          this.bdcqz = res.result
          this.loading = false
          if (this.bdcqz.ysxlh) this.ruleForm.ysxlh = this.bdcqz.ysxlh
          if (this.bdcqz.bdcqzlx == 1) {
            this.drawTextOnImage()
          } else {
            this.drawTextzmImage()
          }
        })
      },
      /**
       * @description: 获取印刷序列号列表
       * @author: renchao
       */
      ysxlhList () {
        readYsxlh({ zslx: this.formData.bdcqzlx }).then((res) => {
          if (res.code === 200) {
            this.ysxlh = res.result;
          }
        })
      },
      /**
       * @description: handleSelect
       * @param {*} val
       * @author: renchao
       */
      handleSelect (val) {
        this.bdcqz.ysxlh = this.ruleForm.ysxlh
        if (this.bdcqz.bdcqzlx == 1) {
          this.drawTextOnImage()
        } else {
          this.drawTextzmImage()
        }
      },
      /**
      * @description: closeInvalidDiglog
      * @author: renchao
      */
      closeInvalidDiglog () {
        this.zfyy = "";
        this.invalidDiglog = false
      },
      /**
       * @description: handleZF
       * @author: renchao
       */
      handleZF () {
        this.invalidDiglog = true
      },
      /**
       * @description: 作废缮证信息
       * @author: renchao
       */
      confirmInvalid () {
        store.dispatch('user/reWorkFresh', false)
        invalidCertificate({ bsmBdcqz: this.bdcqz.bsmBdcqz, zfyy: this.zfyy }).then((res) => {
          if (res.code === 200) {
            this.$message.success("作废成功");
            this.disabled = false
            this.invalidDiglog = false;
            this.zfyy = ''
            this.ruleForm.ysxlh = ''
            this.bdcqz.ysxlh = ''
            if (this.bdcqz.bdcqzlx == 1) {
              this.drawTextOnImage()
            } else {
              this.drawTextzmImage()
            }
            store.dispatch('user/reWorkFresh', true)
          } else {
            this.$message.error(res.message);
          }
        })
      },
      /**
       * @description: 不动产证书
       * @author: renchao
       */
      drawTextOnImage1 () {
        const canvas = this.$refs.zs1;
        const context = canvas.getContext('2d');
        const image = new Image();
        image.onload = () => {
          context.drawImage(image, 0, 0);
          context.font = '18px 楷体';
          context.fillStyle = '#000000';
          let date = this.bdcqz.djsj.split(' ')[0].split('/');
          let nian = date[0]
          let yue = date[1]
          let ri = date[2]
          this.bdcqz.nian = nian
          this.bdcqz.yue = yue
          this.bdcqz.ri = ri
          context.fillText(nian ? nian : '', 780, 499);
          context.fillText(yue ? yue : '', 840, 499);
          context.fillText(ri ? ri : '', 885, 499);
          QRCode.toDataURL(this.bdcqz.bdcqzh, { margin: 0 })
            .then(url => {
              const qrImage = new Image();
              this.bdcqz.qrImage = url; // 将二维码图片的 URL 存储到 bdcqz 对象的 qrImage 属性中
              qrImage.onload = () => {
                context.drawImage(qrImage, 670, 400, 100, 100);
              };
              qrImage.src = url;
            })
            .catch(error => {
              console.error(error);
            });
          context.fillText(this.bdcqz.bdcqzbm ? this.bdcqz.bdcqzbm : '', 745, 633);
        };
        image.src = this.imgSrc1;
      },
      drawTextOnImage () {
        this.drawTextOnImage1()
        function getByteLen (val) {
          var len = 0;
          if (!val) return len;
          for (var i = 0; i < val.length; i++) {
            var length = val.charCodeAt(i);
            if (length >= 0 && length <= 128) {
              len += 1;
            } else {
              len += 2;
            }
          }
          return len;
        }
        const canvas = this.$refs.zs;
        const context = canvas.getContext('2d');
        const image = new Image();
        image.onload = () => {
          context.drawImage(image, 0, 0);
          context.font = '18px 楷体';
          context.fillStyle = '#000000';
          context.fillText(this.bdcqz.sjjc ? this.bdcqz.sjjc : '', 60, 56);
          context.fillText(this.bdcqz.djnd ? this.bdcqz.djnd : '', 113, 56);
          // context.fillText(this.bdcqz.sxqc ? this.bdcqz.sxqc : '', 180, 56);

          if (getByteLen(this.bdcqz.sxqc) > 14) {
            const originalFont = context.font;
            // 设置新的字体大小
            context.font = '14px 楷体'; // 替换为你想要的字体和大小
            // 绘制 bdcdyh
            context.fillText(this.bdcqz.sxqc ? this.bdcqz.sxqc : '', 165, 56);
            // 恢复原始字体设置
            context.font = originalFont;
          } else {
            context.fillText(this.bdcqz.sxqc ? this.bdcqz.sxqc : '', 170, 56);
          }

          context.fillText(this.bdcqz.sxh ? this.bdcqz.sxh : '', 370, 56);
          context.fillText(this.bdcqz.qlr ? this.bdcqz.qlr : '', 129, 97);
          context.fillText(this.bdcqz.gyqk ? this.bdcqz.gyqk : '', 129, 136);

          this.bdcdyh = this.bdcqz.bdcdyh.slice(0, 6) + ' ' + this.bdcqz.bdcdyh.slice(6, 12) + ' ' +
            this.bdcqz.bdcdyh.slice(12, 19) + ' ' + this.bdcqz.bdcdyh.slice(19, this.bdcqz.bdcdyh.length)
          context.fillText(this.bdcdyh ? this.bdcdyh : '', 129, 223);



          context.fillText(this.bdcqz.qllx ? this.bdcqz.qllx : '', 129, 263);
          context.fillText(this.bdcqz.qlxz ? this.bdcqz.qlxz : '', 129, 303);
          context.fillText(this.bdcqz.yt ? this.bdcqz.yt : '', 129, 346);
          let lines6 = this.bdcqz.mj ? this.bdcqz.mj.split(' ') : [];
          if (getByteLen(this.bdcqz.mj) > 37) {
            lines6.forEach((line, index) => {
              const y = 378 + (index * 27); // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= 336) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 129, y + (index * 20)); // 调整行高
              })
            })
          } else {
            lines6.forEach((line, index) => {
              const y = 386 + (index * 27); // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= 336) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 129, y + (index * 20)); // 调整行高
              })
            })
          }
          // 权利其他状态
          const maxWidth = 332; // 最大宽度限制
          let lines = this.bdcqz.qlqtzk ? this.bdcqz.qlqtzk.split('\n').filter(i => i && i.trim()) : [];
          for (let i = 0; i < lines.length; i++) {
            let num = Math.ceil(getByteLen(lines[i]) / 37)
            if (getByteLen(lines[i]) > 37) {
              let currentLine = '';
              let arr = [];
              for (let word of lines[i]) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= maxWidth) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              if (i > 0) {
                arr.forEach((line, index) => {
                  context.fillText(line, 129, 495 + (29 * (i - 1)) + 4 * num + (index * 14)); // 调整行高
                })
              } else {
                arr.forEach((line, index) => {
                  context.fillText(line, 129, 493 + (26 * (i - 1)) + (index * 14)); // 调整行高
                })
              }
            } else {
              if (i > 0) {
                context.fillText(lines[i] ? lines[i] : '', 129, 500 + 4 * num + (29 * (i - 1)));
              } else {
                context.fillText(lines[i] ? lines[i] : '', 129, 505 + (27 * (i - 1)));
              }
            }
          }

          let lines1 = this.bdcqz.fj ? this.bdcqz.fj.split('\n').filter(i => i && i.trim()) : [];
          lines1.forEach((line, index) => {
            const y = 100 + (index * 30); // 每行文本的垂直位置
            let currentLine = '';
            let arr = [];
            for (let word of line) {
              const testLine = currentLine + word;
              const lineWidth = context.measureText(testLine).width;
              if (lineWidth <= 395) {
                currentLine = testLine;
              } else {
                arr.push(currentLine);
                currentLine = word;
              }
            }
            arr.push(currentLine);
            arr.forEach((line, index) => {
              context.fillText(line, 580, y + (index * 30)); // 调整行高
            })
          })
          let lines3 = this.bdcqz.syqx ? this.bdcqz.syqx.split(' ') : [];
          if (getByteLen(this.bdcqz.syqx) > 37) {
            lines3.forEach((line, index) => {
              const y = 423 + (index * 27); // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= 330) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 129, y + (index * 20)); // 调整行高
              })
            })
          } else {
            lines3.forEach((line, index) => {
              const y = 430 + (index * 27); // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= 315) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 129, y + (index * 20)); // 调整行高
              })
            })
          }

          let lines2 = this.bdcqz.zl ? this.bdcqz.zl.split(' ') : [];
          if (getByteLen(this.bdcqz.zl) > 37) {
            lines2.forEach((line, index) => {
              const y = 170 + (index * 20); // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= 336) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 129, y + (index * 20)); // 调整行高
              })
            })
          } else {
            lines2.forEach((line, index) => {
              const y = 180 + (index * 20); // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= 336) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 129, y + (index * 20)); // 调整行高
              })
            })
          }
        }
        image.src = this.imgSrc
      },
      /**
       * @description: 不动产证明
       * @author: renchao
       */
      drawTextzmImage () {
        function getByteLen (val) {
          var len = 0;
          if (!val) return len;
          for (var i = 0; i < val.length; i++) {
            var length = val.charCodeAt(i);
            if (length >= 0 && length <= 128) {
              len += 1;
            } else {
              len += 2;
            }
          }
          return len;
        }

        const canvas = this.$refs.zm;
        const context = canvas.getContext('2d');
        const image = new Image();
        image.onload = () => {
          context.drawImage(image, 0, 0);
          context.font = '18px 楷体';
          context.fillStyle = '#000000';
          // ysxlh
          context.fillText(this.bdcqz.ysxlh ? this.bdcqz.ysxlh : '', 280, 712);
          // djsj
          if (this.bdcqz.djsj) {
            let djsjList = this.bdcqz.djsj.split(' ')[0].split('/')
            context.fillText(djsjList[0] ? djsjList[0] : '', 327, 580);
            context.fillText(djsjList[1] ? djsjList[1] : '', 393, 580);
            context.fillText(djsjList[2] ? djsjList[2] : '', 443, 580);
          }
          context.fillText(this.bdcqz.sjjc ? this.bdcqz.sjjc : '', 620, 125);
          context.fillText(this.bdcqz.djnd ? this.bdcqz.djnd : '', 665, 125);
          // context.fillText(this.bdcqz.sxqc ? this.bdcqz.sxqc : '', 750, 125);

          if (getByteLen(this.bdcqz.sxqc) > 14) {
            const originalFont = context.font;
            // 设置新的字体大小
            context.font = '14px 楷体'; // 替换为你想要的字体和大小
            // 绘制 bdcdyh
            context.fillText(this.bdcqz.sxqc ? this.bdcqz.sxqc : '', 725, 125);
            // 恢复原始字体设置
            context.font = originalFont;
          } else {
            context.fillText(this.bdcqz.sxqc ? this.bdcqz.sxqc : '', 755, 125);
          }

          context.fillText(this.bdcqz.sxh ? this.bdcqz.sxh : '', 960, 123);
          context.fillText(this.bdcqz.zmqlhsx ? this.bdcqz.zmqlhsx : '', 775, 180);
          // context.fillText(this.bdcqz.qlr ? this.bdcqz.qlr : '', 775, 228);
          // 权利人
          let qlrlines = this.bdcqz.qlr
          if (getByteLen(this.bdcqz.qlr) > 32) {
            let currentLine = '';
            let arr = [];
            for (let word of qlrlines) {
              const testLine = currentLine + word;
              const lineWidth = context.measureText(testLine).width;
              if (lineWidth <= 295) {
                currentLine = testLine;
              } else {
                arr.push(currentLine);
                currentLine = word;
              }
            }
            arr.push(currentLine); // 将最后一行添加到数组
            // 绘制所有行
            for (let i = 0; i < arr.length; i++) {
              context.fillText(arr[i], 775, 218 + i * 20); // lineHeight 为行高
            }
          } else {
            context.fillText(this.bdcqz.qlr ? this.bdcqz.qlr : '', 775, 228);
          }
          // 义务人
          // context.fillText(this.bdcqz.ywr ? this.bdcqz.ywr : '', 775, 275);

          let ywrlines = this.bdcqz.ywr
          if (getByteLen(this.bdcqz.ywr) > 32) {
            let currentLine = '';
            let arr = [];
            for (let word of ywrlines) {
              const testLine = currentLine + word;
              const lineWidth = context.measureText(testLine).width;
              if (lineWidth <= 295) {
                currentLine = testLine;
              } else {
                arr.push(currentLine);
                currentLine = word;
              }
            }
            arr.push(currentLine); // 将最后一行添加到数组
            // 绘制所有行
            for (let i = 0; i < arr.length; i++) {
              context.fillText(arr[i], 775, 268 + i * 20); // lineHeight 为行高
            }
          } else {
            context.fillText(this.bdcqz.ywr ? this.bdcqz.ywr : '', 775, 275);
          }


          let lines2 = this.bdcqz.zl ? this.bdcqz.zl.split(' ') : [];
          if (getByteLen(this.bdcqz.zl) > 32) {
            lines2.forEach((line, index) => {
              const y = 315 + (index * 20); // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= 295) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 775, y + (index * 20)); // 调整行高
              })
            })
          } else {
            lines2.forEach((line, index) => {
              const y = 325 + (index * 20); // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= 295) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 775, y + (index * 20)); // 调整行高
              })
            })
          }
          // bdcdyh
          this.bdcdyh = this.bdcqz.bdcdyh.slice(0, 6) + ' ' + this.bdcqz.bdcdyh.slice(6, 12) + ' ' +
            this.bdcqz.bdcdyh.slice(12, 19) + ' ' + this.bdcqz.bdcdyh.slice(19, this.bdcqz.bdcdyh.length)
          // 保存当前字体设置
          const originalFont = context.font;
          // 设置新的字体大小
          context.font = '15px 楷体'; // 替换为你想要的字体和大小
          // 绘制 bdcdyh
          context.fillText(this.bdcdyh ? this.bdcdyh : '', 775, 373);
          // 恢复原始字体设置
          context.font = originalFont;
          // qlqtzk
          function getByteLenBdcdy (val) {
            var encoder = new TextEncoder('utf-8');
            return encoder.encode(val).length;
          }
          const maxWidth = 290; // 最大宽度限制
          let lines = this.bdcqz.qlqtzk ? this.bdcqz.qlqtzk.split('\n').filter(i => i && i.trim()) : [];
          let bdcdyNum = Math.ceil(getByteLenBdcdy(lines[0]) / 43)
          // 单独处理不动产单元号
          let linesBdcdy = lines[0] ? lines[0].split(' ') : [];
          if (getByteLenBdcdy(lines[0]) > 43) {
            linesBdcdy.forEach((line, index) => {
              const y = 412 + (index * 17); // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= maxWidth) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 770, y + (index * 17)); // 调整行高
              })
            })
          } else {
            linesBdcdy.forEach((line, index) => {
              const y = 418 + (index * 17);  // 每行文本的垂直位置
              let currentLine = '';
              let arr = [];
              for (let word of line) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= maxWidth) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              arr.forEach((line, index) => {
                context.fillText(line, 770, y + (index * 17)); // 调整行高
              })
            })
          }

          for (let i = 1; i < lines.length; i++) {
            let num = Math.ceil(getByteLen(lines[i]) / 32)
            if (getByteLen(lines[i]) > 32) {
              let currentLine = '';
              let arr = [];
              for (let word of lines[i]) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= maxWidth) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              if (i > 0) {
                arr.forEach((line, index) => {
                  context.fillText(line, 770, 408 + (bdcdyNum * 15) + (24 * (i - 1)) + 5 * num + (index * 17)); // 调整行高
                })
              } else {
                arr.forEach((line, index) => {
                  context.fillText(line, 770, 408 + (bdcdyNum * 15) + (24 * (i - 1)) + (index * 17)); // 调整行高
                })
              }
            } else {
              if (i > 0) {
                context.fillText(lines[i] ? lines[i] : '', 770, 417 + (bdcdyNum * 15) + 6 * num + (25 * (i - 1)));
              } else {
                context.fillText(lines[i] ? lines[i] : '', 770, 417 + (bdcdyNum * 15) + (25 * (i - 1)));
              }
            }
          }
          // fj
          let lines1 = this.bdcqz.fj ? this.bdcqz.fj.split('\n').filter(i => i && i.trim()) : [];
          for (let i = 0; i < lines1.length; i++) {
            let num = Math.ceil(getByteLen(lines1[i]) / 37)
            if (getByteLen(lines1[i]) > 37) {
              let currentLine = '';
              let arr = [];
              for (let word of lines1[i]) {
                const testLine = currentLine + word;
                const lineWidth = context.measureText(testLine).width;
                if (lineWidth <= maxWidth) {
                  currentLine = testLine;
                } else {
                  arr.push(currentLine);
                  currentLine = word;
                }
              }
              arr.push(currentLine);
              if (i > 0) {
                arr.forEach((line, index) => {
                  context.fillText(line, 770, 605 + (24 * (i - 1)) + 5 * num + (index * 17)); // 调整行高
                })
              } else {
                arr.forEach((line, index) => {
                  context.fillText(line, 770, 605 + (24 * (i - 1)) + (index * 17)); // 调整行高
                })
              }
            } else {
              if (i > 0) {
                context.fillText(lines1[i] ? lines1[i] : '', 770, 616 + 6 * num + (25 * (i - 1)));
              } else {
                context.fillText(lines1[i] ? lines1[i] : '', 770, 616 + (25 * (i - 1)));
              }
            }
          }
        }
        image.src = this.bdczmSrc;
      },
      /**
       * @description: 打印
       * @author: renchao
       */
      handlePrint () {
        let that = this
        if (this.ruleForm.ysxlh == '') {
          this.$message.error('请选择印刷序列号');
          return;
        }
        store.dispatch('user/reWorkFresh', false)
        if (this.bdcqz.bdcqzlx == 1) {
          if (this.activeTitle == 'title1') {
            getPrintTemplateByCode({ tmpno: 'zsdy1' }).then(resInfo => {
              if (resInfo.code == 200) {
                //打开模板设计
                let LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
                LODOP.ADD_PRINT_DATA("ProgramData", resInfo.result.tmpcontent); //装载模板
                console.log(that.bdcqz);
                for (let key in that.bdcqz) {
                  LODOP.SET_PRINT_STYLEA(key, "CONTENT", that.bdcqz[key]);
                }
                LODOP.PREVIEW();
              } else {
                that.$message.error(resInfo.message)
              }
              that.$popupCacel()
              //刷新列表
              store.dispatch('user/reWorkFresh', true)
            })
          } else {
            getPrintTemplateByCode({ tmpno: 'zsdy' }).then(resInfo => {
              if (resInfo.code == 200) {
                //打开模板设计
                let LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
                LODOP.ADD_PRINT_DATA("ProgramData", resInfo.result.tmpcontent); //装载模板

                that.bdcqz.bdcdyh1 = that.bdcqz.bdcdyh.slice(0, 6) + ' ' + that.bdcqz.bdcdyh.slice(6, 12) + ' ' +
                  that.bdcqz.bdcdyh.slice(12, 19) + ' ' + that.bdcqz.bdcdyh.slice(19, that.bdcqz.bdcdyh.length)
                //todo 调取后端接口获取数据 循环set
                for (let key in that.bdcqz) {
                  LODOP.SET_PRINT_STYLEA(key, "CONTENT", that.bdcqz[key]);
                }
                LODOP.PREVIEW();
              } else {
                that.$message.error(resInfo.message)
              }
              that.$popupCacel()
              //刷新列表
              store.dispatch('user/reWorkFresh', true)
            })
          }
        } else {
          getPrintTemplateByCode({ tmpno: 'zmdy' }).then(resInfo => {
            if (resInfo.code == 200) {
              //打开模板设计
              let LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
              LODOP.ADD_PRINT_DATA("ProgramData", resInfo.result.tmpcontent); //装载模板

              // 年月日
              this.bdcqz.nian = this.bdcqz.djsj.split(' ')[0].split('/')[0]
              this.bdcqz.yue = this.bdcqz.djsj.split(' ')[0].split('/')[1]
              this.bdcqz.ri = this.bdcqz.djsj.split(' ')[0].split('/')[1]
              this.bdcqz.bdcdyh1 = this.bdcqz.bdcdyh.slice(0, 6) + ' ' + this.bdcqz.bdcdyh.slice(6, 12) + ' ' +
                this.bdcqz.bdcdyh.slice(12, 19) + ' ' + this.bdcqz.bdcdyh.slice(19, this.bdcqz.bdcdyh.length)
              //todo 调取后端接口获取数据 循环set
              for (let key in this.bdcqz) {
                LODOP.SET_PRINT_STYLEA(key, "CONTENT", this.bdcqz[key]);
              }
              LODOP.PREVIEW();
            } else {
              this.$message.error(resInfo.message)
            }
            that.$popupCacel()
            //刷新列表
            store.dispatch('user/reWorkFresh', true)
          })
        }
      },
      /**
       * @description: 保存打印记录
       * @author: renchao
       */
      handleSubmit () {
        let that = this
        if (this.ruleForm.ysxlh == '') {
          this.$message.error('请选择印刷序列号');
          return;
        }
        store.dispatch('user/reWorkFresh', false)
        this.ruleForm.bsmBdcqz = this.bdcqz.bsmBdcqz;
        this.ruleForm.bdcqzlx = this.bdcqz.bdcqzlx;
        this.ruleForm.szzh = this.bdcqz.bdcqzh;
        this.bdcqz.ysxlh = this.ruleForm.ysxlh
        certificate(this.ruleForm).then((res) => {
          if (res.code === 200) {
            that.$popupCacel()
            this.handlePrint()
          } else {
            this.$message.error(res.message)
          }
          store.dispatch('user/reWorkFresh', true)
        })
      }
    }
  }
</script>
<style scoped lang="scss">
  @import "~@/styles/mixin.scss";
  /deep/.el-tabs__nav-wrap::after {
    display: none;
  }
  /deep/.el-tabs__header {
    margin: 0;
  }
  /deep/.el-form-item--small.el-form-item {
    margin-bottom: 0;
  }
  .zsdy-content {
    height: 80vh;
    overflow-y: scroll;
  }
  .zs-content {
    text-align: center;
  }
  .invalid-diglog {
    padding-bottom: 20px;
    font-size: 16px;
    font-weight: bold;
    color: rgb(99, 99, 99);

    .invalid-title {
      display: flex;
      align-content: center;

      .invalid-icon {
        color: rgb(254, 148, 0);
        font-size: 34px;
        margin-right: 10px;
      }

      .invalid-body {
        line-height: 40px;
        margin-bottom: 10px;
      }
    }

    .invalid-reson {
      margin-bottom: 10px;
    }

    .dialog-footer {
      margin-top: 10px;
      display: flex;
      justify-content: flex-end;
    }
  }
</style>