<template>
  <div ref="print">
    <div class="printInjectCard">
      <div :id="'div1'">
        <div style="width: 300px; text-align: center">
          <span style="font-weight: bolder; font-size: 18px; line-height: 36px">{{ printData.greenText }}</span>
          <span style="font-weight: bolder; font-size: 18px; line-height: 36px">分诊单</span>
        </div>
        <div style="position: absolute; top: 135px; text-align: center; width: 300px">{{ printData.hisId }}</div>
        <div style="position: absolute; top: 155px; text-align: center; width: 300px">
          {{ printData.triageLevel }}（{{ printData.dept }}）
        </div>
        <div
          style="
            position: absolute;
            top: 180px;
            left: 15px;
            display: block;
            width: 300px;
            height: 10px;
            border-bottom: 1px solid #5a5a5a;
          "
        />
      </div>
      <div :id="'div2'">
        <div style="width: 320px; margin-left: 25px">
          <div style="font-size: 15px">
            <span>姓名：</span>
            <span>{{ printData.patientName }}</span>
            <span style="margin-left: 15px">性别：</span>
            <span>{{ printData.sex }}</span>
            <span style="margin-left: 15px">年龄：</span>
            <span>{{ printData.age }}</span>
          </div>
          <div
            style="
              position: absolute;
              top: 15px;
              left: 15px;
              display: block;
              width: 300px;
              height: 10px;
              border-bottom: 1px solid #5a5a5a;
            "
          />
        </div>
      </div>
      <div :id="'div3'">
        <div style="margin-left: 15px; vertical-align: center; line-height: 18px">
          <div>
            <div style="display: inline-block; font-size: 15px">
              <span>Temp：</span>
              <span>{{ printData.observation ? printData.observation.temperature : '' }}℃</span>
            </div>
            <div style="display: inline-block; position: absolute; left: 180px; font-size: 15px">
              <span>P：</span>
              <span>{{ printData.observation ? printData.observation.sphygmus : '' }}次/分</span>
            </div>
          </div>
          <div style="display: block">
            <div style="display: inline-block; font-size: 15px">
              <span>R：</span>
              <span>{{ printData.observation ? printData.observation.breath : '' }}次/分</span>
            </div>
            <div style="display: inline-block; position: absolute; left: 180px; font-size: 15px">
              <span>BP：</span>
              <span>{{ getBloodPressure(printData.observation) }}mmHg</span>
            </div>
          </div>
          <div style="display: block; font-size: 15px">
            <span>SPO2：</span>
            <span>{{ printData.observation ? printData.observation.bloodOxygen : '' }}%</span>
          </div>
          <div
            style="
              position: absolute;
              top: 48px;
              left: 5px;
              display: block;
              width: 300px;
              height: 10px;
              border-bottom: 1px solid #5a5a5a;
            "
          />
        </div>
      </div>
      <div :id="'div4'">
        <div style="margin-left: 15px; font-size: 15px">
          <div>
            <span>分诊时间：</span>
            <span>{{ printData.triageTime }}</span>
          </div>
          <div>
            <span>联系电话：</span>
            <span>{{ printData.tel }}</span>
          </div>
        </div>
        <div
          style="
            position: absolute;
            top: 35px;
            left: 5px;
            display: block;
            width: 300px;
            height: 10px;
            border-bottom: 1px solid #5a5a5a;
          "
        />
        <div style="margin-left: 15px">
          <div style="font-size: 14px; margin-top: 15px; font-weight: bolder">请仔细核对个人信息后进行挂号</div>
          <div style="margin-top: 5px; font-size: 14px">为了您家人和其他患者的健康</div>
          <div style="font-size: 14px">请您保持就诊秩序保持诊区安静</div>
          <div style="font-size: 14px">祝您早日康复</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
// 迁移到 hiprint
import { simplePrint, PRINT_TEMPLATE } from '@/utils/printUtils.js'

export default {
  name: 'VuePrintNb',
  props: {
    printData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      lastId: ''
    }
  },
  mounted() {},

  methods: {
    // 获取血压值
    getBloodPressure(pressure) {
      if (!pressure) return ''
      else if (pressure.bloodPressureShrinkOne === null) return ''
      return pressure.bloodPressureShrinkOne + '/' + pressure.bloodPressureDiastoleOne
    },
    /**
     * 使用 hiprint 执行打印
     * @param {string} printerName 打印机名称
     */
    async printTriage(printerName) {
      console.log(this.printData, 'printData')
      try {
        // 构建打印数据
        const printData = {
          hisId: this.printData.hisId,
          triageLevel: this.printData.triageLevel,
          dept: this.printData.dept,
          patientName: this.printData.patientName,
          sex: this.printData.sex,
          age: this.printData.age,
          temperature: this.printData.observation ? this.printData.observation.temperature : '',
          sphygmus: this.printData.observation ? this.printData.observation.sphygmus : '',
          breath: this.printData.observation ? this.printData.observation.breath : '',
          bloodPressure: this.getBloodPressure(this.printData.observation),
          bloodOxygen: this.printData.observation ? this.printData.observation.bloodOxygen : '',
          triageTime: this.printData.triageTime,
          tel: this.printData.tel,
          greenText: this.printData.greenText || ''
        }
        // 使用 hiprint 打印
        await simplePrint(PRINT_TEMPLATE.TRIAGE_TICKET, printData, printerName)
      } catch (error) {
        console.error('分诊条打印失败:', error)
        if (this.openMesBox) {
          this.openMesBox('6', '打印失败: ' + (error.message || '未知错误'))
        }
      }
    }
  }
}
</script>
<style lang="less">
.printInjectCard {
  display: grid;
  width: 400px;
  grid-template-rows: 60px 205px 30px;
  border: solid #555 1px;
  background-color: #ffffff;
}
@page {
  size: auto;
  margin: 32px;
}
</style>
