'use client'; import { useTranslations } from 'next-intl'; import { Button } from '@/components/ui/shared/Button'; import { Checkbox } from '@/components/ui/shared/Checkbox'; import { Dropdown } from '@/components/ui/shared/Dropdown'; import { SearchBar } from '@/components/ui/shared/SearchBar'; import { formatCaseSentSummary } from '@/components/treatment/caseSendLabel'; import { CaseSentLabel } from '@/components/ui/treatment/CaseSentLabel'; import { TREATMENT_TYPE_KEYS, treatmentTypeLabelKey } from '@/components/ui/treatment/treatmentTypeDisplay'; import type { LabCaseDraft, LinkedOrganizationOption, TreatmentDetailDraft } from '@/types/treatment'; interface LabCasesDispatchPanelProps { details: TreatmentDetailDraft[]; labCases: LabCaseDraft[]; labDependentCodes: Set; activeLabCaseId: string | null; onActiveLabCaseChange: (id: string) => void; onLabCasesChange: (labCases: LabCaseDraft[]) => void; disabled: boolean; canEdit: boolean; orgs: LinkedOrganizationOption[]; organizationSearch: string; onOrganizationSearchChange: (value: string) => void; recentOrganizationIds: string[]; onRecentOrganizationPick: (orgId: string) => void; sendBusyId: string | null; onAddLabCase: () => void; onSendLabCase: (labCase: LabCaseDraft) => void; } export function LabCasesDispatchPanel({ details, labCases, labDependentCodes, activeLabCaseId, onActiveLabCaseChange, onLabCasesChange, disabled, canEdit, orgs, organizationSearch, onOrganizationSearchChange, recentOrganizationIds, onRecentOrganizationPick, sendBusyId, onAddLabCase, onSendLabCase, }: LabCasesDispatchPanelProps) { const t = useTranslations('treatment'); const activeLinkedOrganizations = orgs.filter((o) => o.active); const filteredOrganizations = (() => { const q = organizationSearch.trim().toLowerCase(); if (!q) return activeLinkedOrganizations; return activeLinkedOrganizations.filter((o) => o.name.toLowerCase().includes(q)); })(); const recentOrganizations = recentOrganizationIds .map((id) => activeLinkedOrganizations.find((o) => o.id === id)) .filter(Boolean) as LinkedOrganizationOption[]; const labEligibleDetails = details.filter((d) => labDependentCodes.has(d.treatmentType)); const activeLabCase = labCases.find((lc) => lc.clientId === activeLabCaseId) ?? labCases[0] ?? null; const sent = Boolean(activeLabCase?.sentAt); function detailSummary(d: TreatmentDetailDraft, idx: number) { const typeKey = treatmentTypeLabelKey(d.treatmentType); const typeLabel = d.treatmentType in TREATMENT_TYPE_KEYS ? t(typeKey as 'typeEndo') : d.treatmentType; const teeth = d.teeth.length ? d.teeth.join(', ') : t('teethNone'); return `${t('detailLabel', { n: idx + 1 })} · ${typeLabel} · ${teeth}`; } function updateActiveLabCase(patch: Partial) { if (!activeLabCase) return; onLabCasesChange( labCases.map((lc) => (lc.clientId === activeLabCase.clientId ? { ...lc, ...patch } : lc)), ); } function toggleDetailInActiveLabCase(detailClientId: string, checked: boolean) { if (!activeLabCase || sent) return; onLabCasesChange( labCases.map((lc) => { if (lc.sentAt) return lc; if (lc.clientId === activeLabCase.clientId) { const set = new Set(lc.detailClientIds); if (checked) set.add(detailClientId); else set.delete(detailClientId); return { ...lc, detailClientIds: [...set] }; } if (checked) { return { ...lc, detailClientIds: lc.detailClientIds.filter((id) => id !== detailClientId), }; } return lc; }), ); } function detailAssignedElsewhere(detailClientId: string): boolean { if (!activeLabCase) return false; return labCases.some( (lc) => !lc.sentAt && lc.clientId !== activeLabCase.clientId && lc.detailClientIds.includes(detailClientId), ); } if (labEligibleDetails.length === 0) { return (

{t('labDispatchTitle')}

{t('noLabDetails')}

); } return (

{t('labDispatchTitle')}

{t('labDispatchSubtitle')} {t('labDispatchSendHint')}

{labCases.length === 0 ? (

{t('labDispatchEmpty')}

) : ( <>
{labCases.map((lc, idx) => { const sentSummary = formatCaseSentSummary( lc.sends, { organizationIds: lc.destinationOrganizationId ? [lc.destinationOrganizationId] : [], sentAt: lc.sentAt ?? null, orgs, }, t, ); return ( ); })}
{activeLabCase && (

{t('includeDetails')}

{labEligibleDetails.map((d, idx) => { const assignedElsewhere = detailAssignedElsewhere(d.clientId); const inSentCase = labCases.some( (lc) => lc.sentAt && lc.detailClientIds.includes(d.clientId), ); const checked = activeLabCase.detailClientIds.includes(d.clientId); const itemDisabled = disabled || sent || inSentCase || assignedElsewhere; return ( toggleDetailInActiveLabCase(d.clientId, next)} label={detailSummary(d, idx)} /> ); })}