<template>
  <div class="printWrist">
    <div id="div1" class="printView_content">
      <div style="margin: 1px;font-size: 12px">
        <span>姓名: </span>
        <span>{{ printData.patientName }}</span>
        <span style="position: absolute; left: 110px">病历号:</span>
        <span style="position: absolute; left: 155px">{{ printData.hisId }}</span>
        <span style="position: absolute; left: 250px">入院时间:</span>
        <span style="position: absolute; left: 305px">{{ printData.checkInWardTime? printData.checkInWardTime.substr(0,16):'' }}</span>
      </div>
      <div style="margin: 1px;font-size: 12px">
        <span>性别: </span>
        <span>{{ printData.gender? printData.gender.display:'' }}</span>
        <span style="position: absolute; left: 110px">科室:</span>
        <span style="position: absolute; left: 140px">{{ printData.dept }}</span>
      </div>
      <div style="margin-top: 2px;font-size: 12px">
        <span>床号: </span>
        <span>{{ printData.bedName }}</span>
        <span style="position: absolute; left: 110px">分级:</span>
        <span style="position: absolute; left: 140px">{{ printData.triageLevel }}</span>
      </div>
    </div>
    <div id="qrcode" ref="refQr" style="padding-top: 1px" />
  </div>
</template>
<script>
import QRCode from 'qrcodejs2'
// 迁移到 hiprint
import { simplePrint, PRINT_TEMPLATE } from '@/utils/printUtils.js'

export default {
  name: 'WristPrint',
  data() {
    return {
      printData: {
        patientName: '',
        deptName: '',
        triageLevel: '',
        triageTime: '',
        hisId: '11111',
        lastId: ''
      }
    }
  },
  mounted() {
  },
  updated() {
    this.initBarCode()
  },
  methods: {
    initBarCode() {
      this.$nextTick(() => {
        if (this.lastId !== this.printData.hisId) {
          new QRCode('qrcode', {
            text: this.printData.hisId,
            width: 40,
            height: 40,
            colorDark: '#000000',
            colorLight: '#ffffff',
            correctLevel: QRCode.CorrectLevel.H
          })
        }
        this.lastId = this.printData.hisId
      })
    },
    clear() {
      document.getElementById('qrcode').innerHTML = ''
    },
    /**
     * 使用 hiprint 执行打印
     * @param {boolean} preview 是否预览（hiprint暂不支持预览参数，保留兼容性）
     * @param {string} printerName 打印机名称
     */
    async execPrint(preview, printerName) {
      try {
        // 构建打印数据
        const printData = {
          patientName: this.printData.patientName,
          hisId: this.printData.hisId,
          gender: this.printData.gender ? this.printData.gender.display : '',
          dept: this.printData.dept,
          bedName: this.printData.bedName,
          triageLevel: this.printData.triageLevel,
          checkInWardTime: this.printData.checkInWardTime
        }
        // 使用 hiprint 打印
        await simplePrint(PRINT_TEMPLATE.WRIST_BAND, printData, printerName)
      } catch (error) {
        console.error('腕带打印失败:', error)
        if (this.openMesBox) {
          this.openMesBox('6', '打印失败: ' + (error.message || '未知错误'))
        }
      }
    }
  }
}
</script>
<style scoped lang="less">
  .printWrist {
    display: grid;
    width: 460px;
    grid-template-columns:  330px 40px;
    height: 45px;

   .printView_content{
    display: grid;
    padding-top: 1px;
    padding-left: 60px;
    grid-template-rows: repeat(3,14px);
   }

  }
</style>
