import { useState } from 'react' import { motion, AnimatePresence } from 'framer-motion' import { X, Send } from 'lucide-react' interface ReferralFormModalProps { isOpen: boolean onClose: () => void } interface FormData { referringClinician: string organisationFrom: string presentingComplaint: string clinicalDetails: string contactEmail: string } const INITIAL_FORM: FormData = { referringClinician: '', organisationFrom: '', presentingComplaint: '', clinicalDetails: '', contactEmail: '', } type SubmitStatus = 'idle' | 'submitting' | 'success' | 'error' export function ReferralFormModal({ isOpen, onClose }: ReferralFormModalProps) { const [form, setForm] = useState(INITIAL_FORM) const [status, setStatus] = useState('idle') const [errorMessage, setErrorMessage] = useState('') const updateField = (field: keyof FormData, value: string) => { setForm(prev => ({ ...prev, [field]: value })) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setStatus('submitting') setErrorMessage('') try { const res = await fetch('/api/contact', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: form.referringClinician, organisation: form.organisationFrom, subject: form.presentingComplaint, message: form.clinicalDetails, email: form.contactEmail, }), }) const data = await res.json() if (!res.ok) { throw new Error(data.message || 'Failed to send referral') } setStatus('success') setTimeout(() => { setForm(INITIAL_FORM) setStatus('idle') onClose() }, 2000) } catch (err) { setStatus('error') setErrorMessage(err instanceof Error ? err.message : 'Failed to send referral. Please try again.') } } const labelStyle: React.CSSProperties = { display: 'block', fontFamily: 'var(--font-geist-mono)', fontSize: '11px', fontWeight: 500, color: 'var(--text-tertiary, #8DA8A5)', textTransform: 'uppercase', letterSpacing: '0.06em', marginBottom: '6px', } const inputStyle: React.CSSProperties = { width: '100%', padding: '10px 12px', minHeight: '44px', fontFamily: 'var(--font-ui)', fontSize: '14px', color: 'var(--text-primary, #1A2B2A)', backgroundColor: 'var(--surface, #FFFFFF)', border: '1px solid var(--border, #D1DDD9)', borderRadius: 'var(--radius-sm, 6px)', outline: 'none', transition: 'border-color 150ms ease', } const readOnlyStyle: React.CSSProperties = { ...inputStyle, backgroundColor: 'var(--bg-dashboard, #F0F5F4)', fontStyle: 'italic', color: 'var(--text-secondary, #5B7A78)', cursor: 'default', } return ( {isOpen && ( { if (e.target === e.currentTarget) onClose() }} > {/* Header */}
{/* Form body */}
{/* Referring Clinician */}
updateField('referringClinician', e.target.value)} placeholder="Your name" style={inputStyle} onFocus={(e) => { e.currentTarget.style.borderColor = 'var(--accent, #0D6E6E)' }} onBlur={(e) => { e.currentTarget.style.borderColor = 'var(--border, #D1DDD9)' }} />
{/* Organisation Referred From */}
updateField('organisationFrom', e.target.value)} placeholder="Your organisation" style={inputStyle} onFocus={(e) => { e.currentTarget.style.borderColor = 'var(--accent, #0D6E6E)' }} onBlur={(e) => { e.currentTarget.style.borderColor = 'var(--border, #D1DDD9)' }} />
{/* Organisation Referred To (read-only) */}
{/* Receiving Clinician (read-only) */}
{/* Presenting Complaint */}
updateField('presentingComplaint', e.target.value)} placeholder="Subject / reason for referral" style={inputStyle} onFocus={(e) => { e.currentTarget.style.borderColor = 'var(--accent, #0D6E6E)' }} onBlur={(e) => { e.currentTarget.style.borderColor = 'var(--border, #D1DDD9)' }} />
{/* Clinical Details */}