<script setup>
import {
    NModal,
    NSteps,
    NStep,
    NTabs,
    NTabPane,
    NFlex,
    NGrid,
    NGi,
    NButton,
    NSpin,
    NSelect,
    NInput,
    NFormItemGi,
    NDatePicker
} from 'naive-ui'
import {computed, ref, watch} from "vue";
import AppRadio from "../../../Components/AppRadio.vue";
import axios from "axios";
const show = defineModel('show', { default: false })
const props = defineProps({
    historyId: {
        type: Number
    }
})

const form = ref({
    patient_source: 'mis',
    patient_id: null,
    full_name: '',
    urgency_id: 1,
    visit_result_id: null,
    birth_date: null,
    recipient_date: null,
    death_date: null,
    extract_date: null
})
const loading = ref(true)

const urgencyOptions = [
    {
        label: '0 - Не определено',
        value: 0
    },
    {
        label: '1 - Планово',
        value: 1
    },
    {
        label: '2 - Экстренно',
        value: 2
    }
]
const visitResultOptions = [
    {
        label: '0 - Не определено',
        value: 0
    },
    {
        label: '1 - Выписан',
        value: 1
    }
]

const submit = () => {
    axios.post(`/api/nurse/patients/${form.value.patient_id}/correction`, {
        ...form.value
    }).then(res => {
        console.log(res)
    })
}

const fetchPatient = async (historyId) => {
    loading.value = true
    await axios.get(`/api/nurse/patients/${historyId}`)
        .then(res => {
            form.value.patient_id = historyId
            form.value.full_name = res.data.full_name
            form.value.urgency_id = res.data.urgency_id
            form.value.visit_result_id = res.data.visit_result_id

            form.value.birth_date = res.data.birth_date
            form.value.death_date = res.data.death_date
            form.value.extract_date = res.data.extract_date
            form.value.recipient_date = res.data.recipient_date
        })
        .finally((e) => {
            loading.value = false
        })
}

watch(() => props.historyId, async (newHistoryId, historyId) => {
    await fetchPatient(newHistoryId)
})
</script>

<template>
    <NModal v-model:show="show"
            segmented
            preset="card"
            title="Редактирование пациента"
            class="max-w-xl min-h-[500px] relative"
            draggable
    >
        <div v-if="loading">
            <NSpin class="absolute top-1/2 left-1/2 -translate-x-1/2" />
        </div>
        <NGrid v-else cols="2" x-gap="8">
            <NFormItemGi span="2" label="ФИО">
                <NInput v-model:value="form.full_name" placeholder="Иванов Иван Иванович" />
            </NFormItemGi>
            <NFormItemGi span="1" label="Срочность">
                <NSelect filterable v-model:value="form.urgency_id" :options="urgencyOptions" />
            </NFormItemGi>
            <NFormItemGi span="1" label="Исход госпитализации">
                <NSelect filterable v-model:value="form.visit_result_id" :options="visitResultOptions" />
            </NFormItemGi>
            <NFormItemGi span="1" label="Дата рождения">
                <NDatePicker v-model:value="form.birth_date" class="w-full" />
            </NFormItemGi>
            <NFormItemGi span="1" label="Дата и время госпитализации">
                <NDatePicker v-model:value="form.recipient_date" type="datetime" class="w-full" />
            </NFormItemGi>
            <NFormItemGi span="1" label="Дата и время выписки">
                <NDatePicker v-model:value="form.extract_date" type="datetime" class="w-full" />
            </NFormItemGi>
            <NFormItemGi span="1" label="Дата и время смерти">
                <NDatePicker v-model:value="form.death_date" type="datetime" class="w-full" />
            </NFormItemGi>
        </NGrid>
        <template v-if="!loading" #action>
            <NFlex justify="end">
                <NButton secondary type="primary" @click="submit">
                    Сохранить
                </NButton>
            </NFlex>
        </template>
    </NModal>
</template>

<style scoped>

</style>
