<template>
  <el-container>
    <el-aside width="210px">
      <PatientBaseList
        :yzType="yzType"
        @selectPatientInfo="selectPatientInfo"
      ></PatientBaseList>
    </el-aside>
    <el-container>
      <el-header style="height: 150px">
        <PatientInfo :patientInfo="patientInfo"></PatientInfo>
      </el-header>
      <div>
        <el-row>
          <el-col :span="8">
            <el-button icon="Search" type="primary" @click="queryInfo"
              >查询</el-button
            >
            <el-button icon="Printer" type="success" @click="daYingClick"
              >打印预览</el-button
            >
            <el-button icon="Printer" type="success" @click="printClick"
              >打印</el-button
            >
          </el-col>
          <el-col :span="8">
            <el-radio-group
              @change="initPageNumList"
              v-model="yzOrderParam.printType"
            >
              <el-radio :label="1">打印新增</el-radio>
              <el-radio :label="2">打印单页</el-radio>
              <el-radio :label="3">打印某页后</el-radio>
            </el-radio-group>
          </el-col>
          <el-col :span="4">
            医嘱页号:
            <el-select
              v-model="yzOrderParam.pageNum"
              placeholder="请选择"
              style="width: 60px"
            >
              <el-option
                v-for="item in pageNumList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="clearPrintPageClick"
              >清空打印页号</el-button
            >
          </el-col>
        </el-row>
      </div>
      <el-main>
        <el-scrollbar height="100%">
          <div id="cqYz">
            <table id="pageTable">
              <thead>
                <tr>
                  <td
                    colspan="7"
                    style="
                      width: 100%;
                      text-align: center;
                      padding: 16px 0;
                      border: none;
                      font-size: 23px;
                      font-weight: bold;
                    "
                  >
                    {{ env.VITE_HOSPITAL_NAME }}<br />临时医嘱单
                  </td>
                </tr>
                <tr>
                  <td colspan="7" style="border: none">
                    <div>
                      <div style="width: 15%;float: left;">姓名：{{ patientInfo.name }}</div>
                      <div style="width: 15%;float: left;">
                        性别：{{ patientInfo.sexName }}
                      </div>
                      <div style="width: 15%;float: left;">
                        年龄：{{ companyFunc(patientInfo.age, "岁") }}
                      </div>
                      <div style="width: 15%;float: left;">
                        科别：{{ patientInfo.admissWardName }}
                      </div>
                      <div style="width: 18%;float: left;">
                        床号：{{ patientInfo.bedNo }}
                      </div>
                      <div>住院号：{{ patientInfo.inpatientNo }}</div>
                    </div>
                  </td>
                </tr>
                <tr>
                  <th style="width: 40px; height: 40px">日期</th>
                  <th style="width: 38px">时间</th>
                  <th>临时医嘱</th>
                  <th style="width: 58px">医师签名</th>
                  <th style="width: 85px">核对护士签名</th>
                  <th style="width: 80px">执行时间</th>
                  <th style="width: 85px">执行护士签名</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="(item, index) in yzPrintVOList">
                  <td
                    v-if="item.pageFlag == '1'"
                    style="border: none"
                    colspan="7"
                  >
                    第{{ item.printPageOnce }}页
                  </td>
                  <td v-if="item.pageFlag != '1'" style="height: 40px">
                    {{ item.date }}
                  </td>
                  <td v-if="item.pageFlag != '1'">{{ item.time }}</td>
                  <td v-if="item.pageFlag != '1'" style="text-align: left">
                    {{ item.orderGroupNo }} {{ item.newOrderName }}
                    {{ item.newOrderNameCode }} {{ item.frequCode }}
                  </td>
                  <td v-if="item.pageFlag != '1'" style="text-align: left">
                      <el-image v-if="item.doctorCode" :src="getSign(item.doctorCode)"/>
                  </td>
                  <td v-if="item.pageFlag != '1'" style="text-align: left">
                      <el-image v-if="item.nurseCode" :src="getSign(item.nurseCode)"/>
                  </td>
                  <td v-if="item.pageFlag != '1'">{{ item.execTimeStr }}</td>
                  <td v-if="item.pageFlag != '1'" style="text-align: left">
                      <el-image v-if="item.execId" :src="getSign(item.execId)"/>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </el-scrollbar>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup name="lsYzPrint">
import PatientInfo from "@/components/medical-advice/PatientInfo.vue";
import PatientBaseList from "@/components/medical-advice/PatientBaseList.vue";
import {
  lsYzPrint,
  getTotalPageNUm,
  recPrint,
  clearPrintPageNo,
} from "@/api/medical-advice/medical-advice-management";
import { getLodop, initLodop } from "@/utils/c-lodop";
import { onMounted } from "vue";
import { stringNotBlank } from "@/utils/blank-utils";
import { ElMessage, ElMessageBox } from "element-plus";
import env from "../../../utils/setting";
// 基本信息
const patientInfo = ref({});
const yzType = ref("cq");
const yzPrintVOList = ref([]);
const companyFunc = (val, company) => {
  if (stringNotBlank(val)) {
    return val + company;
  } else {
    return "";
  }
};

const yzOrderParam = ref({
  patNo: "",
  times: null,
  inOutStatusFlag: "",
  type: 2,
  printType: 1,
  pageNum: "",
});

const pageNumList = ref([]);

const getSign = (code) => {
    // return env.RESOURCE_BASE_URL + "/doctorSignatureImage/" + code + ".png";
    return "http://130.150.161.72:8080" + "/doctorSignatureImage/" + code + ".png";
}

//
const initPageNumList = () => {
  yzOrderParam.value.pageNum = "";
  yzPrintVOList.value = [];
  if (yzOrderParam.value.printType === 1) {
    pageNumList.value = [];
    return;
  }
  getTotalPageNUm(yzOrderParam.value).then(res => {
    if (res.total) {
      pageNumList.value = [];
      for (let i = 0; i < res.total; i++) {
        let param = { code: i + 1 + "", name: i + 1 + "" };
        pageNumList.value.push(param);
      }
    } else {
      pageNumList.value = [];
    }
  });
};

const yzPrintCss = `
#pageTable tr td, #pageTable tr th {
border: 1px solid black;
text-align: center;
font-size: 12px;
padding: 0 0;

}
#pageTable tr td{
height: 27px;
}

#pageTable {
border-collapse: collapse;
}

`;
//清空
const clearPrintPageClick = () => {
  clearPrintPageNo(yzOrderParam.value).then(res => {
    yzPrintVOList.value = [];
    pageNumList.value = [];
  });
};

const selectPatientInfo = val => {
  if (!stringNotBlank(val.patientInfo.inpatientNo)) {
    return ElMessage.error("请选中一个病人");
  }
  yzOrderParam.value.patNo = val.patientInfo.inpatientNo;
  yzOrderParam.value.times = val.patientInfo.admissTimes;
  yzOrderParam.value.inOutStatusFlag = val.inOutStatusFlag;
  clearInfo();
};

const queryInfo = () => {
  lsYzPrint(yzOrderParam.value).then(res => {
    patientInfo.value = res.patientInfo;
    yzPrintVOList.value = res.yzPrintVOList;
  });
};

const clearInfo = () => {
  yzPrintVOList.value = [];
  yzOrderParam.value.pageNum = "";
  yzOrderParam.value.printType = 1;
  pageNumList.value = [];
};

onMounted(() => {
  initLodop();
});
const daYingClick = () => {
  let LODOP = getLodop();
  LODOP.PRINT_INIT("长期医嘱单"); // 初始化打印机 名字
  LODOP.SET_PRINT_PAGESIZE(1, "210mm", "297mm", ""); // 设置纸张大小  A4
  LODOP.SET_PRINT_MODE("FULL_WIDTH_FOR_OVERFLOW", true); // 整宽不变形
  LODOP.ADD_PRINT_TABLE(
    "2mm",
    "5mm",
    "205mm",
    "220mm",
    "<style>" +
      yzPrintCss +
      "</style>" +
      "<body>" +
      document.getElementById("cqYz").innerHTML +
      "</body>"
  ); //要打印的内容
  LODOP.PREVIEW(); // 关闭
};

const printClick = () => {
  if (yzPrintVOList.value.length <= 0) {
    return ElMessage.error("没有打印数据");
  }
  if (yzOrderParam.value.printType === 1) {
    recPrint(yzOrderParam.value).then(res => {});
  }
  let LODOP = getLodop();
  LODOP.PRINT_INIT("长期医嘱单"); // 初始化打印机 名字
  LODOP.SET_PRINT_PAGESIZE(1, "210mm", "297mm", ""); // 设置纸张大小  A4
  LODOP.SET_PRINT_MODE("FULL_WIDTH_FOR_OVERFLOW", true); // 整宽不变形
  LODOP.ADD_PRINT_TABLE(
    "2mm",
    "5mm",
    "205mm",
    "220mm",
    "<style>" +
      yzPrintCss +
      "</style>" +
      "<body>" +
      document.getElementById("cqYz").innerHTML +
      "</body>"
  ); //要打印的内容
  LODOP.PRINT(); // 关闭
  queryInfo();
};
</script>

<style scoped>
.nav {
  margin: 0 auto;
  width: 100%;
}
ul {
  list-style: none;
  width: 100%;
}
.nav li {
  float: left;
  margin-left: 10px;
}
#cqYz {
  width: 753px;
    height: 100%;
  padding: 0 0px;
    background: #fff;
}

#pageTable tr td,
#pageTable tr th {
  border: 1px solid black;
  text-align: center;
  font-size: 12px;
}
#pageTable {
  border-collapse: collapse;
}

#pageTable tr td {
  height: 27px;
}
</style>
