<template>
    <page-layer>
        <template #header height="45px;">
            <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期"
                end-placeholder="结束日期" :shortcuts="shortcuts"> </el-date-picker>
            <el-select v-model="mzFlag" style="width: 100px">
                <el-option label="按挂号" value="0"></el-option>
                <el-option label="按接诊" value="1"></el-option>
            </el-select>
            <el-button type="primary" icon="Search" @click="query" style="margin-left: 5px">查询</el-button>
            <el-button type="primary" icon="Download" @click="exportData" style="margin-left: 5px">导出</el-button>
        </template>
        <template #main>
            <el-table :data="returnData" :height="tableHeight" border highlight-current-row row-key="childKey" show-summary
                stripe>
                <el-table-column type="index" label="序号" align="center" header-align="center"></el-table-column>
                <el-table-column label="门诊" prop="mz" align="center" header-align="center">
                    <el-table-column label="科室" prop="ks_mz" align="center" header-align="center"></el-table-column>
                    <el-table-column label="人次" prop="rs_mz" align="center" header-align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="急诊" prop="jz" align="center" header-align="center">
                    <el-table-column label="科室" prop="ks_jz" align="center" header-align="center"></el-table-column>
                    <el-table-column label="人次" prop="rs_jz" align="center" header-align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="退号" prop="th" align="center" header-align="center">
                    <el-table-column label="科室" prop="ks_th" align="center" header-align="center"></el-table-column>
                    <el-table-column label="人次" prop="rs_th" align="center" header-align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="入院" prop="ry" align="center" header-align="center">
                    <el-table-column label="科室" prop="ks_ry" align="center" header-align="center"></el-table-column>
                    <el-table-column label="人次" prop="rs_ry" align="center" header-align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="出院" prop="cy" align="center" header-align="center">
                    <el-table-column label="科室" prop="ks_cy" align="center" header-align="center"></el-table-column>
                    <el-table-column label="人次" prop="rs_cy" align="center" header-align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="在院" prop="zy" align="center" header-align="center">
                    <el-table-column label="科室" prop="ks_zy" align="center" header-align="center"></el-table-column>
                    <el-table-column label="人次" prop="rs_zy" align="center" header-align="center"></el-table-column>
                </el-table-column>
            </el-table>
        </template>
    </page-layer>
</template>
<script setup name="OutHospUnsettl" >
import PageLayer from '@/layout/PageLayer.vue'
import { useStore } from 'vuex'
import { clone } from '@/utils/clone'
import { reactive, ref } from '@vue/reactivity'
import { shortcuts } from '@/data/shortcuts'
import { formatDate, getDateRangeFormatDate } from '@/utils/date'
import { ElMessage } from 'element-plus'
import { selectWorkloadStatistics } from '@/api/reports/workloadStatistics'
import { Export } from '@/utils/ExportExcel'

let nowDay = new Date()
nowDay.setTime(nowDay.getTime() - 24 * 60 * 60 * 1000)
let yf, inday
if (nowDay.getMonth() < 9) {
    yf = '0' + (nowDay.getMonth() + 1)
} else {
    yf = nowDay.getMonth() + 1
}
if (nowDay.getDate() < 10) {
    inday = '0' + nowDay.getDate()
} else {
    inday = nowDay.getDate()
}
let s1 = nowDay.getFullYear() + "-" + yf + "-" + inday
const start = formatDate(s1);
const end = formatDate(s1);
const dateRange = ref([]);
const mzFlag = ref('0');

const queryTerm = reactive({
    startTime: "",
    endTime: "",
    reportId: "all_gzltj",
    menuId: "168",
    type: "1",
    mzFlag: "0",
});

onMounted(() => {
    queryTerm.startTime = start + '00:00:00';
    queryTerm.endTime = end + " 23:59:59";
    dateRange.value = [start, end];
    query()
});

const storeU = useStore()
const windowSize = storeU.state.app.windowSize
const tableHeight = windowSize.h - 20

const returnData = ref([])
const totalData = ref([])

const query = async () => {
    if (dateRange.value) {
        let dateS = getDateRangeFormatDate(dateRange.value);
        queryTerm.startTime = dateS.startTime;
        queryTerm.endTime = dateS.endTime;
    } else {
        queryTerm.startTime = start;
        queryTerm.endTime = end;
        ElMessage({
            type: "info",
            message: "默认查询本月的数据",
            duration: 2500,
            showClose: true,
        });
    }

    queryTerm.mzFlag = mzFlag.value

    await selectWorkloadStatistics(queryTerm)
        .then((res) => {
            returnData.value = res.resultData
            totalData.value = res.totalMap
        });
};
const exportData = () => {
    if (returnData.value.length <= 0) {
        ElMessage({
            message: '没有可以导出的数据！',
            type: 'warning',
            duration: 2500,
            showClose: true,
        })
    }
    const data = clone(returnData.value)
    data.push(totalData.value)
    const title = {
        ks_mz: '门诊科室',
        rs_mz: '门诊人数',
        ks_jz: '急诊科室',
        rs_jz: '急诊人数',
        ks_th: '退号科室',
        rs_th: '退号人数',
        ks_ry: '入院科室',
        rs_ry: '入院人数',
        ks_cy: '出院科室',
        rs_cy: '出院人数',
        ks_zy: '在院科室',
        rs_zy: '在院人数'
    }

    Export(data, title, `工作量统计`)
}

</script>