<script setup>
import PatientTypeSection from "./PatientSection.vue";
import PatientDataTable from "./PatientDataTable.vue";
import {NTabPane, NTabs} from "naive-ui";
import PatientTypeSectionItem from "./PatientSectionItem.vue";
import {computed, ref} from "vue";
import {usePatientColumns} from "../../../Composables/usePatientColumns.js";
import {router} from "@inertiajs/vue3";
import OperationInfoModal from "./OperationInfoModal.vue";

const props = defineProps({
    patients: {
        type: Array,
        default: () => ([])
    }
})

const operationsInModal = ref(null)
const showOperationsModal = ref(false)

const patientsByGroup = computed(() => {
    const groups = {
        urgent: [],
        planned: [],
        deceased: [],
        in_department: [],
        recipient: [],
        discharged: [],
        transferred: [],
        reanimations: [],
        observables: []
    }

    if (!props.patients.hasOwnProperty('data')) return groups

    for (const p of props.patients.data) {
        // Группировка по срочности
        if (p.patient_urgency === 'urgent') groups.urgent.push(p)
        else if (p.patient_urgency === 'planned') groups.planned.push(p)

        // Группировка по реанимации
        if (p.in_reanimation === true) groups.reanimations.push(p)
        // Группировка по наблюдению
        if (p.in_observable === true) groups.observables.push(p)

        // Группировка по статусу (дублирование нужно, если один пациент может быть в двух таблицах)
        if (groups.hasOwnProperty(p.patient_status)) {
            groups[p.patient_status].push(p)
        }
    }

    return groups
})

const {
    planOrEmergencyColumns, observableColumns, reanimationColumns, dischargedColumns,
    deceasedColumns, transferredColumns,
} = usePatientColumns({
    onAddObservable: (row) => {
    },
    onShowOperationModal: (operations) => {
        operationsInModal.value = operations
        showOperationsModal.value = true
    }
})
</script>

<template>
    <PatientTypeSection>
        <PatientTypeSectionItem label="Планово" :counter="patientsByGroup.planned.length">
            <PatientDataTable :data="patientsByGroup.planned" :columns="planOrEmergencyColumns" />
        </PatientTypeSectionItem>
        <PatientTypeSectionItem label="Экстренно" :counter="patientsByGroup.urgent.length">
            <PatientDataTable :data="patientsByGroup.urgent" :columns="planOrEmergencyColumns" />
        </PatientTypeSectionItem>
        <PatientTypeSectionItem label="Выбывшие" :counter="patientsByGroup.discharged.length + patientsByGroup.deceased.length">
            <NTabs type="segment" animated>
                <NTabPane name="1" :tab="`Выписанные (${patientsByGroup.discharged.length})`">
                    <PatientDataTable :data="patientsByGroup.discharged" :columns="dischargedColumns" />
                </NTabPane>
                <NTabPane name="2" :tab="`Умершие (${patientsByGroup.deceased.length})`">
                    <PatientDataTable :data="patientsByGroup.deceased" :columns="deceasedColumns" />
                </NTabPane>
                <NTabPane name="3" :tab="`Переведенные (${patientsByGroup.transferred.length})`">
                    <PatientDataTable :data="patientsByGroup.transferred" :columns="transferredColumns" />
                </NTabPane>
            </NTabs>
        </PatientTypeSectionItem>
    </PatientTypeSection>

    <OperationInfoModal :operations="operationsInModal" v-model:show="showOperationsModal" />
</template>

<style scoped>

</style>
