<script setup>
import {computed, reactive, watch} from "vue";
import {NButton, NForm, NFormItem, NInput, NModal, NSelect} from "naive-ui";
import {useReportStore} from "../../../Stores/report.js";

const show = defineModel('show')
const props = defineProps({
    patient: {
        type: Object,
        default: null,
    },
})

const reportStore = useReportStore()
const form = reactive({
    indicator: '',
    comment: '',
})

const indicatorOptions = [
    { label: 'Стабильный', value: 'stable' },
    { label: 'Средней тяжести', value: 'moderate' },
    { label: 'Тяжелый', value: 'severe' },
    { label: 'Критический', value: 'critical' },
]

const isDisabled = computed(() => !props.patient?.medical_history_id)

watch(
    () => props.patient,
    (patient) => {
        form.indicator = patient?.reanimation_indicator ?? ''
        form.comment = patient?.reanimation_comment ?? ''
    },
    { immediate: true }
)

const handleSave = async () => {
    if (isDisabled.value || !form.indicator) {
        return
    }

    await reportStore.saveReanimationIndicator({
        medical_history_id: props.patient.medical_history_id,
        indicator: form.indicator,
        comment: form.comment || null,
    })

    show.value = false
}
</script>

<template>
    <NModal
        v-model:show="show"
        preset="card"
        title="Индикатор состояния"
        class="max-w-lg"
        :mask-closable="false"
    >
        <NForm :model="form">
            <NFormItem label="Состояние">
                <NSelect
                    v-model:value="form.indicator"
                    :options="indicatorOptions"
                    placeholder="Выберите состояние"
                />
            </NFormItem>
            <NFormItem label="Комментарий">
                <NInput
                    v-model:value="form.comment"
                    type="textarea"
                    :rows="4"
                    :resizable="false"
                    placeholder="Дополнительная информация"
                />
            </NFormItem>
        </NForm>

        <template #action>
            <div class="flex justify-end">
                <NButton
                    type="primary"
                    :disabled="isDisabled || !form.indicator"
                    @click="handleSave"
                >
                    Сохранить
                </NButton>
            </div>
        </template>
    </NModal>
</template>
