<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-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 :summary-method="getSummaries">
                <el-table-column type="index" label="序号" align="center" header-align="center"></el-table-column>
                <el-table-column label="科室收入" prop="kssr" align="center" header-align="center">
                    <el-table-column label="科室" prop="ks_sr" align="center" header-align="center"></el-table-column>
                    <el-table-column label="全部" prop="all_sr" align="center" header-align="center"></el-table-column>
                    <el-table-column label="门诊" prop="mz_sr" align="center" header-align="center"></el-table-column>
                    <el-table-column label="住院" prop="zy_sr" align="center" header-align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="药占比" prop="yzb" align="center" header-align="center">
                    <el-table-column label="科室" prop="ks_yzb" align="center" header-align="center"></el-table-column>
                    <el-table-column label="全部" prop="all_yzb" align="center" header-align="center"></el-table-column>
                    <el-table-column label="门诊" prop="mz_yzb" align="center" header-align="center"></el-table-column>
                    <el-table-column label="住院" prop="zy_yzb" align="center" header-align="center"></el-table-column>
                </el-table-column>
                <el-table-column label="耗占比" prop="hzb" align="center" header-align="center">
                    <el-table-column label="科室" prop="ks_hzb" align="center" header-align="center"></el-table-column>
                    <el-table-column label="全部" prop="all_hzb" align="center" header-align="center"></el-table-column>
                    <el-table-column label="门诊" prop="mz_hzb" align="center" header-align="center"></el-table-column>
                    <el-table-column label="住院" prop="zy_hzb" 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 Dig from "@/utils/math";
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 { selectIncomeStatistics } from '@/api/reports/incomeStatistics'
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 queryTerm = reactive({
    startTime: "",
    endTime: "",
    reportId: "all_srltj",
    menuId: "169",
    type: "1",
});

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({
    mz_zfy: 0,
    mz_ypf: 0,
    mz_hcf: 0,
    zy_zfy: 0,
    zy_ypf: 0,
    zy_hcf: 0,
})

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,
        });
    }
    await selectIncomeStatistics(queryTerm)
        .then((res) => {
            returnData.value = res.resultData

            // 计算合计
            returnData.value.forEach((item) => {
                if (item.mz_zfy) {
                    const value = Number(item.mz_zfy)
                    if (!isNaN(value)) {
                        totalData.value.mz_zfy = Dig.add(totalData.value.mz_zfy, value)
                    }
                }
                if (item.mz_ypf) {
                    const value = Number(item.mz_ypf)
                    if (!isNaN(value)) {
                        totalData.value.mz_ypf = Dig.add(totalData.value.mz_ypf, value)
                    }
                }
                if (item.mz_hcf) {
                    const value = Number(item.mz_hcf)
                    if (!isNaN(value)) {
                        totalData.value.mz_hcf = Dig.add(totalData.value.mz_hcf, value)
                    }
                }
                if (item.zy_zfy) {
                    const value = Number(item.zy_zfy)
                    if (!isNaN(value)) {
                        totalData.value.zy_zfy = Dig.add(totalData.value.zy_zfy, value)
                    }
                }
                if (item.zy_ypf) {
                    const value = Number(item.zy_ypf)
                    if (!isNaN(value)) {
                        totalData.value.zy_ypf = Dig.add(totalData.value.zy_ypf, value)
                    }
                }
                if (item.zy_hcf) {
                    const value = Number(item.zy_hcf)
                    if (!isNaN(value)) {
                        totalData.value.zy_hcf = Dig.add(totalData.value.zy_hcf, value)
                    }
                }
            })
            totalData.value.mz_zfy = Number(totalData.value.mz_zfy.toFixed(2))
            totalData.value.mz_ypf = Number(totalData.value.mz_ypf.toFixed(2))
            totalData.value.mz_hcf = Number(totalData.value.mz_hcf.toFixed(2))
            totalData.value.zy_zfy = Number(totalData.value.zy_zfy.toFixed(2))
            totalData.value.zy_ypf = Number(totalData.value.zy_ypf.toFixed(2))
            totalData.value.zy_hcf = Number(totalData.value.zy_hcf.toFixed(2))
        });
};

// 存储合计数据
const hjInfo = ref({
    ks_sr: '合计',
    all_sr: 0,
    mz_sr: 0,
    zy_sr: 0,
    ks_yzb: '',
    all_yzb: 0,
    mz_yzb: 0,
    zy_yzb: 0,
    ks_hzb: '',
    all_hzb: 0,
    mz_hzb: 0,
    zy_hzb: 0
})

const getSummaries = (param) => {
    const { columns, data } = param
    const sums = []

    columns.forEach((column, index) => {
        if (index === 0) {
            sums[index] = '合计'
            return
        }
        if (column.property.includes('ks')) {
            sums[index] = ''
            return
        }
        const values = data.map((item) => Number(item[column.property]))
        if (column.property === 'mz_yzb') {
            sums[index] = (Dig.division(totalData.value.mz_ypf, totalData.value.mz_zfy) * 100).toFixed(2)
            hjInfo.value.mz_yzb = sums[index]
        } else if (column.property === 'zy_yzb') {
            sums[index] = (Dig.division(totalData.value.zy_ypf, totalData.value.zy_zfy) * 100).toFixed(2)
            hjInfo.value.zy_yzb = sums[index]
        } else if (column.property === 'all_yzb') {
            sums[index] = (Dig.division(Dig.add(totalData.value.mz_ypf, totalData.value.zy_ypf), Dig.add(totalData.value.mz_zfy, totalData.value.zy_zfy)) * 100).toFixed(2)
            hjInfo.value.all_yzb = sums[index]
        } else if (column.property === 'mz_hzb') {
            sums[index] = (Dig.division(totalData.value.mz_hcf, totalData.value.mz_zfy) * 100).toFixed(2)
            hjInfo.value.mz_hzb = sums[index]
        } else if (column.property === 'zy_hzb') {
            sums[index] = (Dig.division(totalData.value.zy_hcf, totalData.value.zy_zfy) * 100).toFixed(2)
            hjInfo.value.zy_hzb = sums[index]
        } else if (column.property === 'all_hzb') {
            sums[index] = (Dig.division(Dig.add(totalData.value.mz_hcf, totalData.value.zy_hcf), Dig.add(totalData.value.mz_zfy, totalData.value.zy_zfy)) * 100).toFixed(2)
            hjInfo.value.all_hzb = sums[index]
        } else if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr)
                if (!isNaN(value)) {
                    return prev + curr
                } else {
                    return prev
                }
            }, 0).toFixed(2)

            if(column.property === 'all_sr'){
                hjInfo.value.all_sr = sums[index]
            } else if(column.property === 'mz_sr'){
                hjInfo.value.mz_sr = sums[index]
            } else if(column.property === 'zy_sr'){
                hjInfo.value.zy_sr = sums[index]
            }

        } else {
            sums[index] = 0
        }
    })
    return sums
}


const exportData = () => {
    if (returnData.value.length <= 0) {
        ElMessage({
            message: '没有可以导出的数据！',
            type: 'warning',
            duration: 2500,
            showClose: true,
        })
    }
    const data = clone(returnData.value)
    data.push(hjInfo.value)

    const title = {
        ks_sr: '收入科室',
        all_sr: '全部收入',
        mz_sr: '门诊收入',
        zy_sr: '住院收入',
        ks_yzb: '药占比科室',
        all_yzb: '全部药占比',
        mz_yzb: '门诊药占比',
        zy_yzb: '住院药占比',
        ks_hzb: '耗占比科室',
        all_hzb: '全部耗占比',
        mz_hzb: '门诊耗占比',
        zy_hzb: '住院耗占比'
    }

    Export(data, title, `收入量统计`)
}

</script>