<template>
    <page-layer>
        <template #header>
            住院号:
            <el-input style="width: 120px" v-model="patNo"/>
            住院次数:
            <el-input-number v-model="times" :min="0"/>
            <el-button @click="inquire">查询</el-button>
            <el-button @click="print">打印</el-button>
        </template>
        <template #main>
            <blood-sugar-table ref="printDiv" :data="tempData" :page="page" :patient-info="patientInfo"/>
        </template>
    </page-layer>
</template>

<script setup name='BloodSugarQuery'>
import {
    getPatientLoodSugar,
} from "@/api/blood-sugar/query";
import {getLodop, initLodop} from "@/utils/c-lodop";
import PageLayer from "@/layout/PageLayer.vue";
import router from "@/router";
import {isDev, nullToEmpty} from "@/utils/public";
import BloodSugarTable from "@/views/examination/BloodSugarQuery/components/BloodSugarTable.vue";
import Dig from "@/utils/math";

let patNo = $ref('')
let times = $ref(0)

let page = $ref({
    total: 0,
    currentPage: 1,
    totalPage: 1,
    pageSize: 10,
})

let data = $ref([])
let patientInfo = $ref()


const queryPatientSBloodSugarInformation = () => {
    getPatientLoodSugar(patNo, times).then((res) => {
        page.total = res.size
        data = res.bloodSugarData
        patientInfo = res.patientInfo
        page.totalPage = Math.ceil(Dig.division(data.length, page.pageSize))
    })
}

const inquire = () => {
    queryPatientSBloodSugarInformation()
}


const tempData = computed(() => {
    return data.slice((page.currentPage - 1) * page.pageSize, page.pageSize * page.currentPage);
})

const printDiv = ref(null)
let thead = ["空腹", "早餐后", "中餐前", "中餐后", "晚餐前", "晚餐后", "零点", "三点", "随机血糖1", "随机血糖2", "随机血糖3"]
const htmlTd = (index, data) => {
    return data[thead[index]]
}
const print = () => {
    let trStr = ''
    for (let i = 0, len = data.length; i < len; i++) {
        let item = data[i]
        let childStr = ``

        for (let j = 0; j < 11; j++) {
            let childItem = htmlTd(j, item.data)
            childStr += `
            <td>
             ${nullToEmpty(childItem?.formatTestTime)}
             <div style="border-top: 1px solid #000;width: 100%;margin-bottom: 3px"/>
             ${nullToEmpty(childItem?.testResult)}
            </td>
            <td>
            ${nullToEmpty(childItem?.nurseIdName)}
            </td>
            `
        }

        trStr += `
        <tr style="padding: 7.5pt;">
            <td>${item.date}</td>
            ${childStr}
        </tr>
        `
    }
    let a = `
<table style="width: 100%;">
            <thead>
            <tr class="no_border">
                <th colspan="23">长沙泰和医院</th>
            </tr>
            <tr class="no_border">
                <th colspan="3">
                    姓名:${nullToEmpty(patientInfo?.name)}
                </th>
                <th colspan="3">
                    性别:${nullToEmpty(patientInfo?.sexName)}
                </th>
                <th colspan="3">
                    年龄:${nullToEmpty(patientInfo?.age)}
                </th>
                <th colspan="3">
                    科室:${nullToEmpty(patientInfo?.zkWardName)}
                </th>
                <th colspan="3">
                    床号:${nullToEmpty(patientInfo?.bedNo)}
                </th>
                <th colspan="3">
                    住院号： ${nullToEmpty(patientInfo?.inpatientNo)}
                </th>
                <th colspan="3">
                    单位：mmol/L
                </th>
            </tr>
            <tr>
                <th style="min-width:70.5pt" rowspan="2">日期</th>
                <th colspan="2">
                    空腹
                </th>

                <th colspan="2">
                    早餐后
                </th>
                <th colspan="2">
                    中餐前
                </th>
                <th colspan="2">
                    中餐后
                </th>
                <th colspan="2">
                    晚餐前
                </th>
                <th colspan="2">
                    晚餐后
                </th>
                <th colspan="2">
                    零点
                </th>
                <th colspan="2">
                    三点
                </th>
                <th colspan="2">
                    随机血糖1
                </th>
                <th colspan="2">
                    随机血糖2
                </th>
                <th colspan="2">
                    随机血糖3
                </th>
            </tr>

            <tr>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
                <th>
                    时间
                </th>
                <th>
                    签名
                </th>
            </tr>
            </thead>
            <tbody>
             ${trStr}
            </tbody>

            <tfoot>
                <tr colspan="23">
                    <td colspan="23">
                     共&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${page.total}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;条&nbsp;&nbsp;&nbsp;&nbsp;
                     当前第&nbsp;&nbsp;&nbsp;<span tdata="pageNO" format="#">#</span>页
                     /共&nbsp;&nbsp;&nbsp;<span tdata="pageCount" format="#">##</span>页
                    </td>
                </tr>
            </tfoot>
        </table>
 `
    const LODOP = getLodop();
    LODOP.PRINT_INIT("血糖");
    LODOP.SET_PRINT_PAGESIZE(2, '210mm', '297mm', '')

    LODOP.SET_PRINT_MODE('RESELECT_PRINTER', true)
    LODOP.SET_PRINT_MODE('PRINT_PAGE_PERCENT', 'Full-Width')
    LODOP.SET_SHOW_MODE("LANDSCAPE_DEFROTATED", 1)

    const strStyle = "<style>.no_border th{border:0;}td{border:0.75pt solid #000;text-align:center;padding:0;}thead th{border:0.75pt solid #000;}tbody th{border:0.75pt solid #000;text-align:center;}table,tr,td{border-collapse:collapse;}</style>";
    LODOP.ADD_PRINT_TABLE('2mm', '5mm', '100%', '100%', strStyle + a);
    LODOP.PREVIEW();
}


onMounted(() => {
    initLodop()
    if (isDev) {
        patNo = '0372621'
        times = 8
        queryPatientSBloodSugarInformation()
    }
})


onActivated(() => {
    let data = router.currentRoute.value.params
    if (data.patNo) {
        patNo = data.patNo;
        times = parseInt(data.times)
        queryPatientSBloodSugarInformation()
    }
})

</script>

<style scoped lang="scss">
td {
  border: 1px solid #000;
  text-align: center;
  padding: 10px 0;
}

.tr th {
  border: 1px solid #000;
  text-align: center;
}

table, tr, td {
  border-collapse: collapse;
}
</style>
