feat: make side panel Cultivos section read-only and place bed badge on the left
This commit is contained in:
@@ -0,0 +1,512 @@
|
||||
import { useState } from 'react';
|
||||
import { ArrowLeft, Save, X, Bed, Calendar, Stethoscope, User, Loader2 } from 'lucide-react';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { toast } from 'sonner';
|
||||
import type { Paciente, Cama, Grupo, Internacion } from '@/types';
|
||||
import { getNombreProfesional, computeSector } from '@/lib/utils';
|
||||
import { useHospitalStore } from '@/hooks/useHospitalStore';
|
||||
|
||||
interface NuevoIngresoProps {
|
||||
pacientes: Paciente[];
|
||||
camas: Cama[];
|
||||
grupos: Grupo[];
|
||||
onIniciarInternacion: (internacion: Omit<Internacion, 'id' | 'activa'>) => Promise<string>;
|
||||
onAgregarCama?: (cama: Omit<Cama, 'id'>) => Promise<string>;
|
||||
onAgregarPaciente?: (paciente: Omit<Paciente, 'id'>) => void;
|
||||
onActualizarPaciente?: (id: string, datos: Partial<Paciente>) => void;
|
||||
onVolver: () => void;
|
||||
getGrupoName: (grupoId: string | undefined) => string;
|
||||
internaciones: Internacion[];
|
||||
}
|
||||
|
||||
export function NuevoIngreso({ pacientes, camas, grupos, onIniciarInternacion, onAgregarCama, onVolver, getGrupoName, internaciones }: NuevoIngresoProps) {
|
||||
const { currentUser } = useHospitalStore();
|
||||
const [pacienteSeleccionado, setPacienteSeleccionado] = useState<string>('');
|
||||
const [busqueda, setBusqueda] = useState('');
|
||||
const [camaSeleccionada, setCamaSeleccionada] = useState('');
|
||||
const [camaInput, setCamaInput] = useState('');
|
||||
const [modoCama, setModoCama] = useState<'seleccionar' | 'escribir'>('seleccionar');
|
||||
const effectiveMedico = getNombreProfesional(currentUser);
|
||||
const [fechaIngresoHospital, setFechaIngresoHospital] = useState('');
|
||||
const [fechaIngresoClinica, setFechaIngresoClinica] = useState('');
|
||||
const [motivoConsulta, setMotivoConsulta] = useState('');
|
||||
const [diagnosticoIngreso, setDiagnosticoIngreso] = useState('');
|
||||
const [enfermedadActual, setEnfermedadActual] = useState('');
|
||||
const [antecedentesEnfermedadActual, setAntecedentesEnfermedadActual] = useState('');
|
||||
const [apache, setApache] = useState('');
|
||||
const [derivacion, setDerivacion] = useState('');
|
||||
const [grupoSeleccionado, setGrupoSeleccionado] = useState<string>('');
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
|
||||
const resetFormulario = () => {
|
||||
setPacienteSeleccionado('');
|
||||
setBusqueda('');
|
||||
setCamaSeleccionada('');
|
||||
setCamaInput('');
|
||||
setGrupoSeleccionado('');
|
||||
setFechaIngresoHospital('');
|
||||
setFechaIngresoClinica('');
|
||||
setMotivoConsulta('');
|
||||
setDiagnosticoIngreso('');
|
||||
setEnfermedadActual('');
|
||||
setAntecedentesEnfermedadActual('');
|
||||
setApache('');
|
||||
setDerivacion('');
|
||||
};
|
||||
|
||||
const pacientesSinInternar = pacientes.filter(p => {
|
||||
const internacionActiva = internaciones.find(i => i.pacienteId === p.id && i.activa);
|
||||
return !internacionActiva;
|
||||
});
|
||||
|
||||
const parseBedNumber = (numero: string) => {
|
||||
const match = numero.match(/^(\d{3})-(\d+)$/);
|
||||
if (!match) return { sala: 0, cama: 0 };
|
||||
return {
|
||||
sala: parseInt(match[1]),
|
||||
cama: parseInt(match[2])
|
||||
};
|
||||
};
|
||||
|
||||
const sortedCamas = [...camas].sort((a, b) => {
|
||||
const pa = parseBedNumber(a.numero);
|
||||
const pb = parseBedNumber(b.numero);
|
||||
|
||||
const getOrden = (sala: number) => {
|
||||
const grupo = Math.floor(sala / 100);
|
||||
const esPar = sala % 2 === 0;
|
||||
|
||||
if (grupo === 2) {
|
||||
if (esPar) return { grupo: 2, suborden: 0, sala };
|
||||
return { grupo: 1, suborden: 1, sala };
|
||||
}
|
||||
if (grupo === 3) {
|
||||
if (esPar) return { grupo: 3, suborden: 0, sala };
|
||||
return { grupo: 4, suborden: 1, sala };
|
||||
}
|
||||
if (grupo === 4) {
|
||||
if (esPar) return { grupo: 6, suborden: 0, sala };
|
||||
return { grupo: 5, suborden: 1, sala };
|
||||
}
|
||||
return { grupo: 10, suborden: 1, sala };
|
||||
};
|
||||
|
||||
const oa = getOrden(pa.sala);
|
||||
const ob = getOrden(pb.sala);
|
||||
|
||||
if (oa.grupo !== ob.grupo) return oa.grupo - ob.grupo;
|
||||
if (oa.suborden === ob.suborden) {
|
||||
if (oa.suborden === 0) return (oa.sala || pa.sala) - (ob.sala || pb.sala);
|
||||
return (ob.sala || 0) - (oa.sala || 0);
|
||||
}
|
||||
return oa.suborden - ob.suborden;
|
||||
});
|
||||
|
||||
const handleSubmit = async () => {
|
||||
// Validaciones explícitas con feedback claro
|
||||
if (!pacienteSeleccionado) {
|
||||
toast.error('Debe seleccionar un paciente de la lista');
|
||||
return;
|
||||
}
|
||||
|
||||
if (modoCama === 'seleccionar' && !camaSeleccionada) {
|
||||
toast.error('Debe seleccionar una cama para el paciente');
|
||||
return;
|
||||
}
|
||||
|
||||
if (modoCama === 'escribir' && !camaInput.trim()) {
|
||||
toast.error('Debe ingresar el número de cama');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!grupoSeleccionado) {
|
||||
toast.error('Debe seleccionar un grupo para el seguimiento');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!diagnosticoIngreso.trim()) {
|
||||
toast.error('Debe completar el Diagnóstico de Ingreso');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!enfermedadActual.trim()) {
|
||||
toast.error('Debe completar la Enfermedad Actual');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!effectiveMedico.trim()) {
|
||||
toast.error('Debe ingresar el nombre del Médico Ingresante');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSubmitting(true);
|
||||
|
||||
try {
|
||||
let camaId = '';
|
||||
|
||||
if (modoCama === 'escribir') {
|
||||
const numeroCama = camaInput.trim();
|
||||
const camaExistente = camas.find(c => c.numero === numeroCama);
|
||||
if (camaExistente) {
|
||||
toast.error(`La cama ${numeroCama} ya existe en el sistema. Seleccione la cama del listado o ingrese un número diferente.`);
|
||||
setIsSubmitting(false);
|
||||
return;
|
||||
} else if (onAgregarCama) {
|
||||
camaId = await onAgregarCama({
|
||||
numero: numeroCama,
|
||||
grupoId: grupoSeleccionado,
|
||||
tipo: 'General',
|
||||
estado: 'Ocupada',
|
||||
sector: computeSector(numeroCama)
|
||||
});
|
||||
} else {
|
||||
toast.error('No se puede crear la cama');
|
||||
setIsSubmitting(false);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
const camaElegida = sortedCamas.find(c => c.id === camaSeleccionada);
|
||||
if (!camaElegida) {
|
||||
toast.error('Cama no encontrada');
|
||||
setIsSubmitting(false);
|
||||
return;
|
||||
}
|
||||
camaId = camaSeleccionada;
|
||||
}
|
||||
|
||||
await onIniciarInternacion({
|
||||
pacienteId: pacienteSeleccionado,
|
||||
camaId: camaId,
|
||||
grupoId: grupoSeleccionado,
|
||||
medicoIngresante: effectiveMedico.trim(),
|
||||
diagnosticoIngreso: diagnosticoIngreso.trim(),
|
||||
motivoConsulta: motivoConsulta.trim() || undefined,
|
||||
enfermedadActual: enfermedadActual.trim(),
|
||||
antecedentesEnfermedadActual: antecedentesEnfermedadActual.trim() || undefined,
|
||||
fechaIngresoHospital: fechaIngresoHospital || undefined,
|
||||
fechaIngresoClinica: fechaIngresoClinica || undefined,
|
||||
apache: apache.trim() || undefined,
|
||||
derivacion: derivacion.trim() || undefined,
|
||||
});
|
||||
|
||||
toast.success('Ingreso registrado con éxito');
|
||||
resetFormulario();
|
||||
onVolver();
|
||||
} catch (err) {
|
||||
console.error('Error al guardar ingreso:', err);
|
||||
toast.error('Ocurrió un error al guardar el ingreso');
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const selectedPaciente = pacientes.find(p => p.id === pacienteSeleccionado);
|
||||
|
||||
return (
|
||||
<div className="space-y-6 dark:text-white">
|
||||
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<Button variant="ghost" size="icon" onClick={onVolver} disabled={isSubmitting} className="shrink-0">
|
||||
<ArrowLeft className="h-5 w-5" />
|
||||
</Button>
|
||||
<div>
|
||||
<h1 className="text-xl sm:text-2xl font-bold text-gray-900 dark:text-white flex items-center gap-2">
|
||||
<User className="h-6 w-6 text-blue-600" />
|
||||
Nuevo Ingreso
|
||||
</h1>
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">Formulario de internación</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 w-full sm:w-auto justify-end">
|
||||
<Button variant="outline" className="flex-1 sm:flex-initial" onClick={() => { resetFormulario(); onVolver(); }} disabled={isSubmitting}>
|
||||
<X className="h-4 w-4 mr-2" />
|
||||
Cancelar
|
||||
</Button>
|
||||
<Button className="bg-blue-600 hover:bg-blue-700 flex-1 sm:flex-initial" onClick={handleSubmit} disabled={isSubmitting}>
|
||||
{isSubmitting ? (
|
||||
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||||
) : (
|
||||
<Save className="h-4 w-4 mr-2" />
|
||||
)}
|
||||
Guardar Ingreso
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{/* Columna 1: Datos del Paciente */}
|
||||
<div className="space-y-6">
|
||||
<Card className="min-h-[200px]">
|
||||
<CardContent className="pt-6 space-y-4">
|
||||
<h3 className="font-semibold text-gray-900 dark:text-white flex items-center gap-2">
|
||||
<User className="h-4 w-4" />
|
||||
Datos del Paciente
|
||||
</h3>
|
||||
|
||||
{!pacienteSeleccionado ? (
|
||||
<>
|
||||
<Label>Buscar Paciente *</Label>
|
||||
<Input
|
||||
placeholder="Apellido, nombre o DNI"
|
||||
value={busqueda}
|
||||
onChange={(e) => setBusqueda(e.target.value)}
|
||||
/>
|
||||
<div className="max-h-48 overflow-auto border rounded-md divide-y dark:divide-gray-800">
|
||||
{(() => {
|
||||
const q = busqueda.trim().toLowerCase();
|
||||
if (!q) {
|
||||
return <div className="p-3 text-sm text-gray-500">Escriba para buscar</div>;
|
||||
}
|
||||
const matches = pacientesSinInternar.filter(p =>
|
||||
p.apellido.toLowerCase().includes(q) ||
|
||||
p.nombre.toLowerCase().includes(q) ||
|
||||
p.dni.includes(q)
|
||||
);
|
||||
if (matches.length === 0) {
|
||||
return <div className="p-3 text-sm text-gray-500">Sin resultados</div>;
|
||||
}
|
||||
return matches.map(p => (
|
||||
<button
|
||||
key={p.id}
|
||||
type="button"
|
||||
onClick={() => setPacienteSeleccionado(p.id)}
|
||||
className="w-full text-left p-3 hover:bg-gray-50 dark:hover:bg-gray-800 text-sm transition-colors"
|
||||
>
|
||||
<span className="font-medium">{p.apellido}, {p.nombre}</span> — DNI: {p.dni}
|
||||
</button>
|
||||
));
|
||||
})()}
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<div className="flex items-center gap-2 flex-wrap pt-1">
|
||||
<Badge className="bg-blue-100 text-blue-800 dark:bg-blue-950/80 dark:text-blue-300 dark:border dark:border-blue-800 text-sm py-1 px-3">
|
||||
{selectedPaciente?.apellido}, {selectedPaciente?.nombre}
|
||||
</Badge>
|
||||
<Badge variant="outline" className="text-sm py-1 px-3">DNI: {selectedPaciente?.dni}</Badge>
|
||||
<Button size="sm" variant="ghost" onClick={() => { setPacienteSeleccionado(''); setBusqueda(''); }}>
|
||||
Cambiar
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Columna 2: Cama y Grupo */}
|
||||
<div className="space-y-6">
|
||||
<Card className="min-h-[200px]">
|
||||
<CardContent className="pt-6 space-y-4">
|
||||
<h3 className="font-semibold text-gray-900 dark:text-white flex items-center gap-2">
|
||||
<Bed className="h-4 w-4" />
|
||||
Asignación de Cama y Grupo
|
||||
</h3>
|
||||
|
||||
<div className="flex flex-col sm:flex-row gap-2 mb-4">
|
||||
<Button
|
||||
className="flex-1 text-xs sm:text-sm"
|
||||
variant={modoCama === 'seleccionar' ? 'default' : 'outline'}
|
||||
onClick={() => setModoCama('seleccionar')}
|
||||
>
|
||||
Seleccionar Cama
|
||||
</Button>
|
||||
<Button
|
||||
className="flex-1 text-xs sm:text-sm"
|
||||
variant={modoCama === 'escribir' ? 'default' : 'outline'}
|
||||
onClick={() => {
|
||||
setModoCama('escribir');
|
||||
setGrupoSeleccionado('');
|
||||
}}
|
||||
>
|
||||
Cama Fuera Área
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{modoCama === 'escribir' && (
|
||||
<div className="space-y-2">
|
||||
<Label>Grupo de Seguimiento *</Label>
|
||||
<Select value={grupoSeleccionado} onValueChange={setGrupoSeleccionado}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Seleccionar grupo" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{grupos.map(g => (
|
||||
<SelectItem key={g.id} value={g.id}>
|
||||
{g.nombre}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{modoCama === 'seleccionar' ? (
|
||||
<>
|
||||
<div>
|
||||
<Label>Cama *</Label>
|
||||
<Select
|
||||
value={camaSeleccionada}
|
||||
onValueChange={(val) => {
|
||||
setCamaSeleccionada(val);
|
||||
const cama = camas.find(c => c.id === val);
|
||||
if (cama?.grupoId) {
|
||||
setGrupoSeleccionado(cama.grupoId);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Seleccionar cama" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{sortedCamas.map(c => (
|
||||
<SelectItem key={c.id} value={c.id}>
|
||||
{c.numero} - {getGrupoName(c.grupoId)}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<div>
|
||||
<Label>Número de Cama (formato XXX-YY) *</Label>
|
||||
<Input
|
||||
placeholder="201-1"
|
||||
value={camaInput}
|
||||
onChange={(e) => setCamaInput(e.target.value)}
|
||||
/>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1">
|
||||
{camaInput.trim() ? `Clasificación: ${computeSector(camaInput.trim())}` : 'Formato XXX-YY (Salas 3XX impares y 4XX son Fuera de Área)'}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardContent className="pt-6 space-y-4">
|
||||
<h3 className="font-semibold text-gray-900 dark:text-white flex items-center gap-2">
|
||||
<Calendar className="h-4 w-4" />
|
||||
Datos de Ingreso
|
||||
</h3>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<Label>Fecha Ingreso al Hospital</Label>
|
||||
<Input
|
||||
type="date"
|
||||
value={fechaIngresoHospital}
|
||||
onChange={(e) => setFechaIngresoHospital(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label>Fecha Ingreso a Clínica Médica</Label>
|
||||
<Input
|
||||
type="date"
|
||||
value={fechaIngresoClinica}
|
||||
onChange={(e) => setFechaIngresoClinica(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label>Derivado de</Label>
|
||||
<Input
|
||||
placeholder="Hospital o clínica de derivación"
|
||||
value={derivacion}
|
||||
onChange={(e) => setDerivacion(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label>Apache / Mortalidad</Label>
|
||||
<Input
|
||||
placeholder="Valor Apache o riesgo"
|
||||
value={apache}
|
||||
onChange={(e) => setApache(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardContent className="pt-6 space-y-4">
|
||||
<h3 className="font-semibold text-gray-900 dark:text-white">
|
||||
Datos Clínicos
|
||||
</h3>
|
||||
|
||||
<div>
|
||||
<Label>Motivo de Consulta *</Label>
|
||||
<textarea
|
||||
className="w-full p-2 border rounded-md text-sm min-h-[80px]"
|
||||
placeholder="Motivo por el cual consulta"
|
||||
value={motivoConsulta}
|
||||
onChange={(e) => setMotivoConsulta(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label>Diagnóstico de Ingreso *</Label>
|
||||
<textarea
|
||||
className="w-full p-2 border rounded-md text-sm min-h-[80px]"
|
||||
placeholder="Diagnóstico principal"
|
||||
value={diagnosticoIngreso}
|
||||
onChange={(e) => setDiagnosticoIngreso(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label>Enfermedad Actual *</Label>
|
||||
<textarea
|
||||
className="w-full p-2 border rounded-md text-sm min-h-[100px]"
|
||||
placeholder="Descripción de la enfermedad actual"
|
||||
value={enfermedadActual}
|
||||
onChange={(e) => setEnfermedadActual(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label>Antecedentes de la Enfermedad Actual</Label>
|
||||
<textarea
|
||||
className="w-full p-2 border rounded-md text-sm min-h-[80px]"
|
||||
placeholder="Antecedentes relevantes"
|
||||
value={antecedentesEnfermedadActual}
|
||||
onChange={(e) => setAntecedentesEnfermedadActual(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardContent className="pt-6 space-y-4">
|
||||
<h3 className="font-semibold text-gray-900 dark:text-white flex items-center gap-2">
|
||||
<Stethoscope className="h-4 w-4" />
|
||||
Médico Ingresante
|
||||
</h3>
|
||||
|
||||
<div>
|
||||
<Label>Médico Ingresante *</Label>
|
||||
<Input
|
||||
placeholder="Nombre del médico"
|
||||
value={getNombreProfesional(currentUser)}
|
||||
disabled
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user