'use client'; import { useMemo, useState } from 'react'; import { useTranslations } from 'next-intl'; import { Button } from '@/components/ui/shared/Button'; import { Checkbox } from '@/components/ui/shared/Checkbox'; import { FORM_SELECT_CLASS } from '@/components/shared/formSelectStyles'; import { TreatmentHistoryDetailLine } from '@/components/ui/treatment/TreatmentHistoryDetailLine'; import { DetailLabSendBadge } from '@/components/ui/treatment/DetailLabSendBadge'; import { filterTreatmentHistoryItems } from '@/components/treatment/treatmentHistoryFilters'; import type { LinkedOrganizationOption, PastTreatment } from '@/types/treatment'; import type { TreatmentCatalogEntry } from '@/types/treatment-catalog'; interface PastTreatmentsPanelProps { items: PastTreatment[]; currentDraft?: PastTreatment | null; patientName?: string; currentAppointmentId?: string | null; treatmentCatalog: TreatmentCatalogEntry[]; labDependentCodes: Set; orgs?: LinkedOrganizationOption[]; loading?: boolean; selectedPreviewId?: string | null; onSelectTreatment?: (treatment: PastTreatment) => void; } function formatHistoryTimestamp(iso: string): string { const date = new Date(iso); return date.toLocaleString(undefined, { weekday: 'short', month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: '2-digit', }); } export function PastTreatmentsPanel({ items, currentDraft = null, patientName, currentAppointmentId, treatmentCatalog, labDependentCodes, orgs, loading, selectedPreviewId, onSelectTreatment, }: PastTreatmentsPanelProps) { const t = useTranslations('treatment'); const [notShippedOnly, setNotShippedOnly] = useState(false); const [filterDate, setFilterDate] = useState(''); const hasActiveFilters = notShippedOnly || Boolean(filterDate); const displayedItems = useMemo( () => filterTreatmentHistoryItems(items, currentDraft, labDependentCodes, { notShippedOnly, date: filterDate, }), [items, currentDraft, labDependentCodes, notShippedOnly, filterDate], ); function clearFilters() { setNotShippedOnly(false); setFilterDate(''); } const filterInputClass = `${FORM_SELECT_CLASS} rounded-md px-2 py-1.5 text-xs min-w-[9.5rem]`; return (

{patientName ? t('historyPatientScope', { patientName }) : t('historyTitle')}

{t('historySubtitle')}

{loading &&

{t('loadingHistory')}

} {!loading && displayedItems.length === 0 && (

{hasActiveFilters ? t('historyFilterEmpty') : t('historyEmpty')}

)}
{displayedItems.map((treatment) => { const isSelected = selectedPreviewId === treatment.id; const isCurrentAppointment = Boolean(currentAppointmentId) && treatment.appointmentId === currentAppointmentId; const isLiveDraft = treatment.id === 'current-draft'; return (
onSelectTreatment?.(treatment)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); onSelectTreatment?.(treatment); } }} className={` border rounded-[var(--radius-sm)] px-2 py-1.5 cursor-pointer transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-primary/45 ${ isSelected ? 'border-primary bg-primary/5' : 'border-border/60 bg-background-secondary/30 hover:border-border hover:bg-background-secondary/50' } `} >
{isLiveDraft ? ( {t('labAttentionCurrentDraft')} ) : isCurrentAppointment ? ( {t('historyCurrentAppointment')} ) : null}
{treatment.details.length === 0 ? (

{t('noDetails')}

) : (
{treatment.details.map((detail, idx) => (
))}
)}
); })}
); }