'use client'; import { useEffect, useState } from 'react'; import { X } from 'lucide-react'; import { Button } from '@/components/ui/common/Button'; import { Dropdown } from '@/components/ui/common/Dropdown'; import { APPOINTMENT_PURPOSES, type AppointmentPurpose } from '@/types/appointment'; import { APPOINTMENT_PURPOSE_LABEL } from '@/components/ui/appointments/appointmentPurposeStyles'; import type { Patient } from '@/types/patient'; import { combineLocalDateAndTime, formatTimeForInput, isSameLocalCalendarDay, } from '@/lib/appointmentTime'; interface AppointmentBookingModalProps { open: boolean; scheduleDate: Date; patient: Patient | undefined; providerUserId: string | null; providerName: string; initialHour: number; onClose: () => void; onSubmit: (payload: { patientId: string; providerUserId: string; startAt: string; endAt: string; purpose: AppointmentPurpose; }) => Promise; loading?: boolean; } export function AppointmentBookingModal({ open, scheduleDate, patient, providerUserId, providerName, initialHour, onClose, onSubmit, loading = false, }: AppointmentBookingModalProps) { const [startTime, setStartTime] = useState('09:00'); const [endTime, setEndTime] = useState('10:00'); const [purpose, setPurpose] = useState('consultation'); const [error, setError] = useState(''); useEffect(() => { if (!open) { return; } const start = new Date( scheduleDate.getFullYear(), scheduleDate.getMonth(), scheduleDate.getDate(), initialHour, 0, 0, 0, ); const end = new Date( scheduleDate.getFullYear(), scheduleDate.getMonth(), scheduleDate.getDate(), initialHour < 23 ? initialHour + 1 : 23, initialHour < 23 ? 0 : 59, 0, 0, ); setStartTime(formatTimeForInput(start)); setEndTime(formatTimeForInput(end)); setPurpose('consultation'); setError(''); }, [open, scheduleDate, initialHour]); if (!open || !providerUserId) { return null; } const inputClass = 'w-full rounded-[var(--radius-md)] border border-border bg-background-secondary/90 text-text-primary px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary/35'; async function handleSubmit() { setError(''); if (!providerUserId) { return; } if (!patient) { setError('Select a patient first.'); return; } const startAt = combineLocalDateAndTime(scheduleDate, startTime); const endAt = combineLocalDateAndTime(scheduleDate, endTime); if (endAt <= startAt) { setError('End time must be after start time.'); return; } const now = new Date(); if (isSameLocalCalendarDay(scheduleDate, now) && startAt.getTime() < now.getTime()) { setError('Cannot schedule in the past.'); return; } await onSubmit({ patientId: patient.id, providerUserId, startAt: startAt.toISOString(), endAt: endAt.toISOString(), purpose, }); } return (

New appointment

Provider: {providerName}

{patient ? `${patient.firstName} ${patient.lastName}` : '—'}

setStartTime(e.target.value)} />
setEndTime(e.target.value)} />
setPurpose(e.target.value as AppointmentPurpose)} > {APPOINTMENT_PURPOSES.map((p) => ( ))} {error &&

{error}

}
); }