<script setup>
import {NDataTable, NFlex, NText, NDatePicker} from 'naive-ui'
import AppLayout from "../../Layouts/AppLayout.vue";
import {h, ref} from "vue";
import DatePickerQuery from "../../Components/DatePickerQuery.vue";
import {Link, usePage} from "@inertiajs/vue3";

const props = defineProps({
    data: {
        type: Object,
        default: []
    },
    isHeadOrAdmin: {
        type: Boolean
    },
    date: {
        type: [Number, Array]
    },
    isOneDay: {
        type: Boolean
    }
})

const columns = ref([
    {
        title: 'Отделение',
        key: 'department',
        width: 240,
        titleAlign: 'center',
        colSpan: (row) => row.colspan,
        render(row) {
            if (row.isGroupHeader) {
                return h(NFlex, {
                    align: "center",
                    justify: "center"
                }, h(NText, { style: 'font-weight: 600;' }, row.groupName))
            }

            if (row.isTotalRow) {
                return h(NText, { style: 'font-weight: 600;' }, row.department)
            }

            // Получаем текущие query параметры
            const { url } = usePage()
            const currentUrl = new URL(url, window.location.origin)
            const searchParams = currentUrl.searchParams

            // Берем startAt и endAt из текущего URL
            const startAt = searchParams.get('startAt')
            const endAt = searchParams.get('endAt')

            const linkData = {}

            if (startAt)
                linkData.startAt = startAt
            if (endAt)
                linkData.endAt = endAt

            return h(Link, {
                href: `/report`,
                data: linkData,
                class: 'underline decoration-dashed'
            }, row.department)
        }
    },
    {
        title: 'Кол-во коек',
        key: 'beds',
        width: 60,
        titleAlign: 'center',
        align: 'center',
        render: (row) => {
            if (row.isTotalRow) {
                return h(NText, { style: 'font-weight: 600;' }, row.beds)
            } else {
                return h(NText, { }, row.beds)
            }
        }
    },
    {
        title: 'Поступило',
        key: 'recipients',
        titleAlign: 'center',
        children: [
            {
                title: 'Всего',
                key: 'recipients.all',
                width: 60,
                titleAlign: 'center',
                align: 'center',
                render: (row) => {
                    if (row.isTotalRow) {
                        return h(NText, { style: 'font-weight: 600;' }, row.recipients.all)
                    } else {
                        return h(NText, { }, row.recipients.all)
                    }
                }
            },
            {
                title: 'План',
                key: 'recipients.plan',
                width: 60,
                titleAlign: 'center',
                align: 'center',
                render: (row) => {
                    if (row.isTotalRow) {
                        return h(NText, { style: 'font-weight: 600;' }, row.recipients.plan)
                    } else {
                        return h(NText, { }, row.recipients.plan)
                    }
                }
            },
            {
                title: 'Экстр',
                key: 'recipients.emergency',
                width: 60,
                titleAlign: 'center',
                align: 'center',
                render: (row) => {
                    if (row.isTotalRow) {
                        return h(NText, { style: 'font-weight: 600;' }, row.recipients.emergency)
                    } else {
                        return h(NText, { }, row.recipients.emergency)
                    }
                }
            },
            {
                title: 'Перевод',
                key: 'recipients.transferred',
                width: 84,
                titleAlign: 'center',
                align: 'center',
                render: (row) => {
                    if (row.isTotalRow) {
                        return h(NText, { style: 'font-weight: 600;' }, row.recipients.transferred)
                    } else {
                        return h(NText, { }, row.recipients.transferred)
                    }
                }
            },
        ]
    },
    {
        title: 'Выбыло',
        key: 'outcome',
        width: 84,
        titleAlign: 'center',
        align: 'center',
        render: (row) => {
            if (row.isTotalRow) {
                return h(NText, { style: 'font-weight: 600;' }, row.outcome)
            } else {
                return h(NText, { }, row.outcome)
            }
        }
    },
    {
        title: 'Состоит',
        key: 'consist',
        width: 84,
        titleAlign: 'center',
        align: 'center',
        render: (row) => {
            if (row.isTotalRow) {
                return h(NText, { style: 'font-weight: 600;' }, row.consist)
            } else {
                return h(NText, { }, row.consist)
            }
        }
    },
    {
        title: '% загруженности',
        key: 'percentLoadedBeds',
        width: 84,
        titleAlign: 'center',
        align: 'center',
        render: (row) => {
            if (row.isTotalRow) {
                return h(NText, { style: 'font-weight: 600;' }, row.percentLoadedBeds)
            } else {
                return h(NText, { }, row.percentLoadedBeds)
            }
        }
    },
    {
        title: 'Операции',
        key: 'surgical',
        titleAlign: 'center',
        children: [
            {
                title: 'Э',
                key: 'surgical.emergency',
                width: 60,
                titleAlign: 'center',
                align: 'center',
                render: (row) => {
                    if (row.isTotalRow) {
                        return h(NText, { style: 'font-weight: 600;' }, row.surgical.emergency)
                    } else {
                        return h(NText, { }, row.surgical.emergency)
                    }
                }
            },
            {
                title: 'П',
                key: 'surgical.plan',
                width: 60,
                titleAlign: 'center',
                align: 'center',
                render: (row) => {
                    if (row.isTotalRow) {
                        return h(NText, { style: 'font-weight: 600;' }, row.surgical.plan)
                    } else {
                        return h(NText, { }, row.surgical.plan)
                    }
                }
            },
        ]
    },
    {
        title: 'Умерло',
        key: 'deceased',
        width: 84,
        titleAlign: 'center',
        align: 'center',
        render: (row) => {
            if (row.isTotalRow) {
                return h(NText, { style: 'font-weight: 600;' }, row.deceased)
            } else {
                return h(NText, { }, row.deceased)
            }
        }
    },
])

const rowProps = (row) => {
    if (row.isGroupHeader) return {
        style: `--n-merged-td-color: var(--n-merged-th-color)`
    }
    if (row.isTotalRow) return {
        style: `--n-merged-td-color: var(--n-merged-th-color); --n-text-color: var(--n-th-icon-color-active);`
    }
}
</script>

<template>
    <AppLayout>
        <template #headerExtra>
            <DatePickerQuery :is-head-or-admin="isHeadOrAdmin" :date="date" :is-one-day="isOneDay" />
        </template>
        <NDataTable :columns="columns"
                    :data="data"
                    size="small"
                    :single-line="false"
                    striped
                    min-height="calc(100vh - 48px - 70px)"
                    max-height="calc(100vh - 48px - 70px)"
                    :row-props="rowProps"
        >

        </NDataTable>
    </AppLayout>
</template>

<style scoped>
:deep(.n-data-table-th),
:deep(.n-data-table-td) {
    font-size: var(--n-font-size);
}
</style>
