'use client' import { useState } from 'react' import { Modal, Button, Input, Select, showToast } from '@/components/ui' const CATEGORIES = [ { value: 'ONCOLOGY', label: 'Oncology' }, { value: 'HOSPITAL', label: 'Hospital' }, { value: 'PHARMACY', label: 'Pharmacy' }, { value: 'INSURANCE', label: 'Insurance' }, { value: 'FAMILY', label: 'Family' }, { value: 'OTHER', label: 'Other' }, ] interface ContactFormData { name: string role: string category: string phone: string phone2: string email: string address: string hours: string notes: string isEmergency: boolean } interface ContactFormProps { open: boolean onClose: () => void onSaved: () => void workspaceId: string initialData?: Partial & { id?: string } } export function ContactForm({ open, onClose, onSaved, workspaceId, initialData }: ContactFormProps) { const isEdit = !!initialData?.id const [form, setForm] = useState({ name: initialData?.name || '', role: initialData?.role || '', category: initialData?.category || 'ONCOLOGY', phone: initialData?.phone || '', phone2: initialData?.phone2 || '', email: initialData?.email || '', address: initialData?.address || '', hours: initialData?.hours || '', notes: initialData?.notes || '', isEmergency: initialData?.isEmergency || false, }) const [saving, setSaving] = useState(false) const handleSave = async () => { if (!form.name.trim() || !form.role.trim() || !form.phone.trim()) { showToast('Name, role, and phone are required', 'error') return } setSaving(true) try { const url = isEdit ? `/api/workspaces/${workspaceId}/contacts/${initialData!.id}` : `/api/workspaces/${workspaceId}/contacts` const method = isEdit ? 'PATCH' : 'POST' const response = await fetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: form.name.trim(), role: form.role.trim(), category: form.category, phone: form.phone.trim(), phone2: form.phone2.trim() || null, email: form.email.trim() || null, address: form.address.trim() || null, hours: form.hours.trim() || null, notes: form.notes.trim() || null, isEmergency: form.isEmergency, }), }) if (!response.ok) throw new Error('Failed to save contact') showToast(isEdit ? 'Contact updated' : 'Contact added', 'success') onSaved() onClose() } catch { showToast('Failed to save contact', 'error') } finally { setSaving(false) } } const update = (field: keyof ContactFormData, value: string | boolean) => setForm((prev) => ({ ...prev, [field]: value })) return (
update('name', e.target.value)} placeholder="Dr. Smith" /> update('role', e.target.value)} placeholder="Oncologist" /> update('phone', e.target.value)} placeholder="+61 2 1234 5678" type="tel" /> update('phone2', e.target.value)} placeholder="Optional" type="tel" /> update('email', e.target.value)} placeholder="Optional" type="email" /> update('address', e.target.value)} placeholder="Optional" /> update('hours', e.target.value)} placeholder="Mon-Fri 8am-5pm" />