<template>
  <el-button @click="daYing">打印</el-button>
  <el-button @click="copyClick" v-if="props.laiYuan===2">复制会诊结果</el-button>
  <div class="main-info-parent" style="padding: 0 20px; overflow: hidden">
    <div id="huiZhenXinXiDaYin">
      <h1 style="text-align: center; font-size: 30px">长沙泰和医院</h1>
      <h4 style="text-align: center; font-size: 17px; margin-top: 0">会 诊 记 录 单</h4>
      <div class="divHuanZheXinXi">
        <div class="huanZheXinXi" style="margin-left: 20px">住院号：{{ huanZheXinXi.inpatientNo }}</div>
        <div class="huanZheXinXi" style="margin-left: 25px">姓名：{{ huanZheXinXi.name }}</div>
        <div class="huanZheXinXi">性别：{{ cptSex(huanZheXinXi.sex) }}</div>
        <div class="huanZheXinXi" style="width: 90px">年龄：{{ huanZheXinXi.age }}</div>
        <div class="huanZheXinXi" style="width: 140px">科室：{{ huanZheXinXi.deptCodeName }}</div>
        <div class="huanZheXinXi">床号：{{ huanZheXinXi.bedNo }}</div>
      </div>

      <div class="huiZhenBody">
        <div>
          <div>
            请
            <div class="reqDept">{{ huanZheXinXi.reqDept1Name }}</div>
            会诊
            <div class="huiZhenJiBie">会诊类别：{{ hzLeiBie(huanZheXinXi.hzType) }}</div>
          </div>
        </div>
        <!-- 病情简介 -->
        <div class="zhenDuanBianJu">
          病情简介 <br/>
          <textarea v-model="huanZheXinXi.reqComment" class="zhenDuanTextarea" readonly="value"
                    style="height: 154px"></textarea>
        </div>

        <!-- 目前主要诊断 -->
        <div class="zhenDuanBianJu">
          目前主要诊断 <br/>
          <textarea v-model="huanZheXinXi.hzZd" class="zhenDuanTextarea" readonly style="height: 50px"></textarea>
        </div>

        <!-- 会诊目的 -->
        <div class="zhenDuanBianJu">
          会诊目的 <br/>
          <textarea v-model="huanZheXinXi.hzMd" class="zhenDuanTextarea" readonly style="height: 50px"></textarea>
        </div>
        <div class="qingQiuHuiZhenXinXi">
          <div class="div1">请求会诊科室：{{ huanZheXinXi.deptCodeName }}</div>
          <div class="div2">会诊申请医师：{{ huanZheXinXi.reqDoctor }}</div>
          <div>申请时间：{{ huanZheXinXi.reqDate }}</div>
        </div>
      </div>
      <!-- 会诊意见  -->
      <div class="huiZhenBody">
        <!-- 会诊意见 -->
        <div class="zhenDuanBianJu">
          会诊意见
          <br/>
          <textarea
              v-model="huanZheXinXi.hzComment"
              :readonly="laiYuan === 2 ? 'readonly' : false"
              autoHeight="true"
              class="zhenDuanTextarea"
              maxlength="1100"
              onkeydown="if(event.keyCode === 13)event.returnValue = false;"
              style="height: 329px"
          >
            </textarea>
        </div>
        <div class="qingQiuHuiZhenXinXi">
          <div class="div1">会诊科室：{{ huanZheXinXi.reqDept1Name }}</div>
          <div class="div2">
            医师：{{ props.laiYuan === 2 ? huanZheXinXi.hzDoctor2Name : store.state.user.info.name }}
          </div>
          <div>会诊时间：{{ huanZheXinXi.hzDate }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="JieShouHuiZhenDaYing">
import store from '@/store'
import {cptSex} from '@/utils/computed'
import '@/css/hui-zhen-ji-lv-dan.css'
import {computed, onMounted} from 'vue'
import {getLodop, initLodop} from '@/utils/c-lodop'
import {huiZhenJiLvDanCss} from '@/data/css-for-print'
import {getServerDateApi} from '@/api/public-api'
import {BizException, ExceptionEnum} from "@/utils/BizException";
import {
  copyEnum, delEmrCopy,
  getEmrCopy,
  setEmrCopy
} from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init";
import {xcMessage} from "@/utils/xiaochan-element-plus";

const props = defineProps({
  laiYuan: {
    type: Number,
    default: 1,
  },
})

const emit = defineEmits(['fanHuiXinXi'])

const windowSize = computed(() => {
  return store.state.app.windowSize
})

/**
 * 患者信息
 * @type {Ref<UnwrapRef<{}>>}
 */
const huanZheXinXi = ref({})

function shiJian() {
  if (props.laiYuan === 1) {
    getServerDateApi().then((res) => {
      huanZheXinXi.value.hzDate = res
    })
  }
}

const huiZhenXinXiXiangQing = (val) => {
  shiJian()
  huanZheXinXi.value = val
}

const daYing = () => {
  shiJian()
  setTimeout(() => {
    // 在vue 中使用 v-model 绑定的 都需要 这样不然没有数据
    const allTextArea = document.body.getElementsByTagName('textarea')
    const textInput = document.body.getElementsByTagName('input')
    for (let i = 0; i < textInput.length; i++) {
      textInput[i].setAttribute('value', textInput[i].value)
    }
    for (let i = 0; i < allTextArea.length; i++) {
      allTextArea[i].innerText = allTextArea[i].value
    }
    // A4: 210毫米 × 297毫米 = 793.70px *  1122.52px
    let LODOP = getLodop()
    // 导入css样式，以及 内容
    const strFormHtml = huiZhenJiLvDanCss + '<body>' + document.getElementById('huiZhenXinXiDaYin').innerHTML + '</body>'
    LODOP.PRINT_INIT('会诊记录单') // 初始化打印机 名字
    LODOP.SET_PRINT_PAGESIZE(1, '210mm', '297mm', '') // 设置纸张大小  A4
    LODOP.SET_PRINT_MODE('FULL_WIDTH_FOR_OVERFLOW', true) // 整宽不变形
    LODOP.ADD_PRINT_HTM('2mm', '5mm', '100%', '100%', strFormHtml) //要打印的内容
    LODOP.SET_PRINT_STYLE('ItemType', 3) //设置对象风格
    LODOP.ADD_PRINT_TEXT('290mm', '190mm', '20mm', '2mm', '第#页/共&页') //增加纯文本项
    LODOP.PREVIEW() // 关闭
  }, 100)
}

const copyClick = () => {
  if (!huanZheXinXi.value.inpatientNo) {
    BizException(ExceptionEnum.MESSAGE_ERROR, "请先获取患者信息。")
  }
  let temp = huanZheXinXi.value
  let data = {
    '会诊科室': [
      {code: temp.reqDept1, name: temp.reqDept1Name}
    ],
    '会诊医生': [
      {code: temp.hzDoctor2, name: temp.hzDoctor2Name}
    ],
    '会诊意见': temp.hzComment,
    '编辑者': [
      {code: store.state.user.info.code, name: store.state.user.info.name}
    ]
  }
  setEmrCopy(copyEnum.会诊, data)
  xcMessage.success('复制成功。')
}

const fanHuiXinXi = () => {
  emit('fanHuiXinXi', {huanZheXinXi})
}

onMounted(() => {
  initLodop()
})

defineExpose({
  huiZhenXinXiXiangQing
})

function hzLeiBie(val) {
  switch (val) {
    case '1':
      return '常规会诊'
    case '2':
      return '疑难病例会诊'
    case '3':
      return '急救会诊'
  }
}
</script>

<style scoped></style>
