'use client'; import { useSearchParams, useRouter, usePathname } from 'next/navigation'; import { Plus, Stethoscope } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { DiagnosisListItem } from './diagnosis-list-item'; import { DiagnosisDetailForm } from './diagnosis-detail-form'; import type { DiagnosisWithIntake, IntakeInfo } from '../actions'; interface DiagnosisMasterDetailProps { patientId: string; diagnoses: DiagnosisWithIntake[]; intakes: IntakeInfo[]; } export function DiagnosisMasterDetail({ patientId, diagnoses, intakes, }: DiagnosisMasterDetailProps) { const searchParams = useSearchParams(); const router = useRouter(); const pathname = usePathname(); const selectedId = searchParams.get('selected'); const isNew = selectedId === 'new'; const selectedDiagnosis = selectedId && !isNew ? diagnoses.find((d) => d.id === selectedId) || null : null; const updateSelection = (id: string | null) => { const params = new URLSearchParams(searchParams.toString()); if (id) { params.set('selected', id); } else { params.delete('selected'); } router.push(`${pathname}?${params.toString()}`, { scroll: false }); }; const handleNewDiagnosis = () => { updateSelection('new'); }; const handleSelectDiagnosis = (id: string) => { updateSelection(id); }; const handleSaved = () => { // Na opslaan blijven we op dezelfde selectie (of clear bij new) if (isNew) { updateSelection(null); } // Router refresh gebeurt automatisch door revalidatePath }; const handleDeleted = () => { updateSelection(null); }; // Sorteer: hoofddiagnoses eerst, dan actieve, dan op datum const sortedDiagnoses = [...diagnoses].sort((a, b) => { const aIsPrimary = a.category === 'primary-diagnosis'; const bIsPrimary = b.category === 'primary-diagnosis'; if (aIsPrimary && !bIsPrimary) return -1; if (!aIsPrimary && bIsPrimary) return 1; const aIsActive = a.clinical_status === 'active'; const bIsActive = b.clinical_status === 'active'; if (aIsActive && !bIsActive) return -1; if (!aIsActive && bIsActive) return 1; const aDate = a.recorded_date ? new Date(a.recorded_date).getTime() : 0; const bDate = b.recorded_date ? new Date(b.recorded_date).getTime() : 0; return bDate - aDate; }); return (
{/* Master: Lijst (2 kolommen) */}

{diagnoses.length} diagnose{diagnoses.length !== 1 ? 's' : ''}

{intakes.length === 0 && (
Er is nog geen intake voor deze patiƫnt. Maak eerst een intake aan.
)} {sortedDiagnoses.length === 0 && intakes.length > 0 ? (

Nog geen diagnoses

Klik op "Nieuw" om er een toe te voegen.

) : (
{sortedDiagnoses.map((diagnosis) => ( handleSelectDiagnosis(diagnosis.id)} /> ))}
)}
{/* Detail: Formulier (3 kolommen) */}
); }