<template>
    <page-layer>
        <template #headerBlock>

            <el-form :inline="true" ref="formRef" :model="param">

                <el-form-item label="住院号" prop="patNo">
                    <el-input v-model="param.patNo" clearable
                              style="width: 120px"
                              @blur="param.patNo=$event.target.value.trim()"/>
                </el-form-item>

                <el-form-item label="姓名" prop="name">
                    <el-input v-model="param.name" clearable
                              style="width: 120px"
                              @blur="param.name=$event.target.value.trim()"/>
                </el-form-item>

                <el-form-item label="入院时间" prop="admissionTime">
                    <xc-date-picker style="width: 180px"
                                    type="daterange"
                                    clearable
                                    v-model="param.admissionTime"/>
                </el-form-item>

                <el-form-item label="出院时间" prop="dischargeTime">
                    <xc-date-picker style="width: 180px"
                                    type="daterange"
                                    clearable
                                    v-model="param.dischargeTime"/>
                </el-form-item>

                <el-form-item label="科室" prop="dept">
                    <select-v4 :data="deptWardList" style="width: 220px;" clearable v-model="param.dept"/>
                </el-form-item>

                <el-form-item label="病房" prop="ward">
                    <select-v4 :data="deptWardList" style="width: 220px;" clearable v-model="param.ward"/>
                </el-form-item>

                <el-form-item label="诊断代码" prop="diagnosticCode">
                    <el-select-v2 v-model="param.diagnosticCode"
                                  remote
                                  clearable
                                  filterable
                                  :remote-method="method"
                                  :options="diagnosisData"/>
                </el-form-item>

                <el-form-item label="是否主诊" prop="isPrimaryDiagnosis">
                    <el-checkbox v-model="param.isPrimaryDiagnosis"/>
                </el-form-item>

                <el-form-item label="出院" prop="leaveHospital">
                    <el-checkbox v-model="param.leaveHospital"/>
                </el-form-item>

                <el-form-item label="管床医生" prop="empCode">
                    <el-select-v2 v-model="param.empCode"
                                  clearable
                                  remote
                                  style="width: 90px"
                                  filterable
                                  :remote-method="empMethod"
                                  :options="empList"/>
                </el-form-item>

                <el-form-item label="出院科室" prop="disDept">
                    <select-v4 :data="deptWardList" style="width: 220px;" clearable v-model="param.disDept"/>
                </el-form-item>

                <el-form-item label="审核">
                    <el-select v-model="param.emrAudit"
                               style="width: 80px">
                        <el-option v-for="item in audit"
                                   :value="item.code"
                                   :label="item.name"/>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button @click="queryClick" type="primary">查询</el-button>
                    <el-button @click="reset" type="warning">重置</el-button>
                </el-form-item>
            </el-form>
        </template>

        <template #main="{height}">
            <pat-infomation-dialog
                    v-if="dialog"
                    @closed="dialog = false"
                    :admiss-date="rowData.admissDate"
                    :pat-no="rowData.patNo"
                    :times="rowData.admissTimes"
                    :leave-hospital="param.leaveHospital ? 2 : 1"
                    :dics="dics"/>
            <xc-table :local-data="patInfoList"
                      :final-height="getWindowSize.h / 1.15">
                <el-table-column label="操作" fixed="left" width="60">
                    <template #default="{row}">
                        <el-button @click="details(row)" text type="primary" plain>详情</el-button>
                        <el-button style="margin-left: 0" @click="openEmr(row)" text type="primary" plain>病历
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="住院号" prop="inpatientNo" width="60"/>
                <el-table-column label="次数" prop="admissTimes" width="30"/>
                <el-table-column label="床位" prop="bedNo" width="30"/>
                <el-table-column label="审核" width="30">
                    <template #default="{row}">
                        <span v-if="row.emrAudit > 0 ">
                            √
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="姓名" prop="name">
                    <template #default="{row}">
                        {{ row.name + '(' + row.sexName + ')' }}
                    </template>
                </el-table-column>
                <el-table-column label="入院时间" prop="admissDate" width="80"/>
                <el-table-column label="出院时间" prop="disDate" width="80"/>
                <el-table-column label="入院病区" prop="admissWardName"/>
                <el-table-column label="小科室" prop="zkWardName"/>
                <el-table-column label="年龄" prop="tempAge"/>
                <el-table-column label="医保身份" prop="responceName"/>
                <el-table-column label="总费用" prop="totalCharge"/>
                <el-table-column label="余额" prop="balance"/>
                <el-table-column label="入院诊断" prop="admissDiagStr"/>
            </xc-table>

        </template>
    </page-layer>
</template>

<script setup name='PatientInformationInquiry'>
import PageLayer from "@/layout/PageLayer.vue";
import {getHospWardAndDept, getPatientInfo, diagnosis, employeeList} from "@/api/zhu-yuan-yi-sheng/pat-info-query";
import SelectV4 from "@/components/xiao-chan/select-v4/SelectV4.vue";
import XcDatePicker from "@/components/xiao-chan/date-picker/XcDatePicker.vue";
import XcTable from "@/components/xiao-chan/xc-table/XcTable.vue";
import {getWindowSize} from "@/utils/window-size";
import {getAllDictionary} from "@/api/case-front-sheet";
import {operations} from "@/data";
import {autopsies, haveOrNot, yesOrNo} from "@/views/hospitalization/case-front-sheet/common";
import PatInfomationDialog from "@/components/pat-info-list/PatInfomationDialog.vue";
import {
    emrStateEnum,
    getEmrUrl,
    getInEmrUrl
} from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init";

const param = ref({
    patNo: '',
    name: '',
    leaveHospital: false,
    admissionTime: [],
    dischargeTime: [],
    dept: '',
    ward: '',
    diagnosticCode: '',
    isPrimaryDiagnosis: false,
    empCode: '',
    disDept: '',
    emrAudit: 0,
})

const deptWardList = ref([])
const formRef = ref(null)
const patInfoList = ref([])
const dialog = ref(false)
const rowData = ref()
const dics = ref([])

const diagnosisData = ref([])
const empList = ref([])


const queryClick = () => {
    getPatientInfo(param.value).then(res => {
        patInfoList.value = res
    })
}
const reset = () => {
    formRef.value?.resetFields()
}

const details = async (row) => {
    rowData.value = row
    dialog.value = true
}

const method = (val) => {
    diagnosis(val).then((res) => {
        diagnosisData.value = res
    })
}

const empMethod = (val) => {
    employeeList(val).then((res) => {
        empList.value = res
    })
}

const openEmr = (data) => {
    if (param.value.leaveHospital) {
        window.open(getInEmrUrl(data.inpatientNo, data.admissTimes, emrStateEnum.出院只读), '_blank')
    } else {
        window.open(getEmrUrl(data.inpatientNo, data.admissTimes, emrStateEnum.在院只读), '_blank')
    }
}

onMounted(async () => {
    getHospWardAndDept().then(res => {
        deptWardList.value = res
    })

    getAllDictionary().then(res => {
        res.getOperations = operations;
        res.getYesOrNo = yesOrNo;
        res.getHaveOrNot = haveOrNot;
        res.getAutopsies = autopsies;
        dics.value = res
    })
})


let audit = [
    {code: 0, name: '全部'},
    {code: 1, name: '已审'},
    {code: 2, name: '未审'}
]

</script>

<style lang="scss" scoped>


</style>
