'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { Modal } from '@/components/ui/modal' import { INPUT_CLASS as inp } from '@/components/ui/field' export type SiteWithZones = { id: string name: string address: string | null active: boolean zones: Array<{ id: string; name: string; qr_code_token: string; active: boolean }> } interface Props { sites: SiteWithZones[] } const lbl = 'block text-xs font-medium text-gray-600 mb-1' type DeleteTarget = { kind: 'site'; id: string; name: string } | { kind: 'zone'; id: string; name: string } export function SiteZoneManager({ sites }: Props) { const router = useRouter() const [siteName, setSiteName] = useState('') const [zoneName, setZoneName] = useState('') const [zoneSiteId, setZoneSiteId] = useState('') const [busy, setBusy] = useState(false) const [busyId, setBusyId] = useState(null) const [error, setError] = useState(null) const [showAddSite, setShowAddSite] = useState(false) const [showAddZone, setShowAddZone] = useState(false) const [confirmDelete, setConfirmDelete] = useState(null) const post = async (payload: Record) => { setBusy(true) setError(null) const res = await fetch('/api/admin/sites', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }) setBusy(false) if (!res.ok) { const data = await res.json().catch(() => ({})) setError(data.error ?? 'Save failed') return false } router.refresh() return true } const send = async (method: 'PATCH' | 'DELETE', payload: Record) => { const id = payload.id as string setBusyId(id) setError(null) const res = await fetch('/api/admin/sites', { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }) setBusyId(null) if (!res.ok) { const data = await res.json().catch(() => ({})) setError(data.error ?? 'Action failed') return false } router.refresh() return true } const handleDelete = async (target: DeleteTarget) => { await send('DELETE', target.kind === 'site' ? { kind: 'site', id: target.id } : { kind: 'zone', id: target.id } ) setConfirmDelete(null) } const totalZones = sites.reduce((n, s) => n + s.zones.length, 0) return (

Sites & Zones

{sites.length} site{sites.length !== 1 ? 's' : ''} · {totalZones} zone{totalZones !== 1 ? 's' : ''}

{error &&
{error}
}
{sites.map(site => (

{site.name}

{site.address &&

{site.address}

}
{site.zones.length > 0 && (
    {site.zones.map(z => (
  • {z.name}
    Report link
  • ))}
)} {site.zones.length === 0 && (

No zones

)}
))} {sites.length === 0 && (
No sites yet
)}
{/* Add Site Modal */} { setShowAddSite(false); setSiteName(''); setError(null) }} title="Add Site" width="sm" footer={ <> } > {error &&

{error}

}
setSiteName(e.target.value)} onKeyDown={e => e.key === 'Enter' && !busy && siteName.trim() && post({ kind: 'site', name: siteName }).then(ok => ok && (setSiteName(''), setShowAddSite(false)))} className={inp} autoFocus />
{/* Add Zone Modal */} { setShowAddZone(false); setZoneName(''); setZoneSiteId(''); setError(null) }} title="Add Zone" width="sm" footer={ <> } > {error &&

{error}

}
setZoneName(e.target.value)} className={inp} />
{/* Delete Confirm Modal */} setConfirmDelete(null)} title={`Delete ${confirmDelete?.kind === 'site' ? 'Site' : 'Zone'}`} width="sm" footer={ <> } >

Delete {confirmDelete?.name}? This cannot be undone.

) }