'use client' import { useState, useEffect } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs' import { Label } from '@/components/ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' export default function EnrollmentPage() { const [adminKey, setAdminKey] = useState('') const [isAuthenticated, setIsAuthenticated] = useState(false) const [activeTab, setActiveTab] = useState<'staff' | 'kiosks'>('staff') const [locations, setLocations] = useState([]) const [loading, setLoading] = useState(false) const [message, setMessage] = useState<{ type: 'success' | 'error', text: string } | null>(null) const [staffForm, setStaffForm] = useState({ location_id: '', role: 'staff', display_name: '', email: '', password: '', first_name: '', last_name: '', phone: '' }) const [kioskForm, setKioskForm] = useState({ location_id: '', device_name: '', display_name: '', ip_address: '' }) const [staffList, setStaffList] = useState([]) const [kioskList, setKioskList] = useState([]) useEffect(() => { const savedKey = localStorage.getItem('admin_enrollment_key') if (savedKey) { setAdminKey(savedKey) setIsAuthenticated(true) fetchLocations(savedKey) } }, []) const authenticate = async () => { if (!adminKey) { setMessage({ type: 'error', text: 'Please enter the admin enrollment key' }) return } setLoading(true) setMessage(null) try { const response = await fetch('/api/admin/locations', { headers: { 'Authorization': `Bearer ${adminKey}` } }) if (response.ok) { localStorage.setItem('admin_enrollment_key', adminKey) setIsAuthenticated(true) const data = await response.json() setLocations(data.locations) setMessage({ type: 'success', text: 'Authenticated successfully!' }) } else { setMessage({ type: 'error', text: 'Invalid admin enrollment key' }) } } catch (error) { setMessage({ type: 'error', text: 'Authentication failed' }) } finally { setLoading(false) } } const fetchLocations = async (key: string) => { try { const response = await fetch('/api/admin/locations', { headers: { 'Authorization': `Bearer ${key}` } }) const data = await response.json() setLocations(data.locations) } catch (error) { console.error('Failed to fetch locations:', error) } } const createStaff = async () => { setLoading(true) setMessage(null) try { const response = await fetch('/api/admin/users', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${adminKey}` }, body: JSON.stringify(staffForm) }) const data = await response.json() if (response.ok) { setMessage({ type: 'success', text: data.message || 'Staff member created successfully!' }) fetchStaff() setStaffForm({ location_id: '', role: 'staff', display_name: '', email: '', password: '', first_name: '', last_name: '', phone: '' }) } else { setMessage({ type: 'error', text: data.error || 'Failed to create staff member' }) } } catch (error) { setMessage({ type: 'error', text: 'Failed to create staff member' }) } finally { setLoading(false) } } const createKiosk = async () => { setLoading(true) setMessage(null) try { const response = await fetch('/api/admin/kiosks', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${adminKey}` }, body: JSON.stringify(kioskForm) }) const data = await response.json() if (response.ok) { setMessage({ type: 'success', text: data.message || 'Kiosk created successfully!' }) fetchKiosks() setKioskForm({ location_id: '', device_name: '', display_name: '', ip_address: '' }) } else { setMessage({ type: 'error', text: data.error || 'Failed to create kiosk' }) } } catch (error) { setMessage({ type: 'error', text: 'Failed to create kiosk' }) } finally { setLoading(false) } } const fetchStaff = async () => { try { const response = await fetch('/api/admin/users', { headers: { 'Authorization': `Bearer ${adminKey}` } }) const data = await response.json() setStaffList(data.staff || []) } catch (error) { console.error('Failed to fetch staff:', error) } } const fetchKiosks = async () => { try { const response = await fetch('/api/admin/kiosks', { headers: { 'Authorization': `Bearer ${adminKey}` } }) const data = await response.json() setKioskList(data.kiosks || []) } catch (error) { console.error('Failed to fetch kiosks:', error) } } useEffect(() => { if (isAuthenticated) { fetchStaff() fetchKiosks() } }, [isAuthenticated]) if (!isAuthenticated) { return (
Admin Enrollment Enter your admin enrollment key to access the user management system
setAdminKey(e.target.value)} />
{message && (
{message.text}
)}
) } return (

User Enrollment System

Create staff members and kiosks for your salon locations

{message && (
{message.text}
)} setActiveTab(v as 'staff' | 'kiosks')} className="mb-8"> Staff Members Kiosks Create Staff Member Add a new staff member to a location
setStaffForm({ ...staffForm, display_name: e.target.value })} />
setStaffForm({ ...staffForm, first_name: e.target.value })} />
setStaffForm({ ...staffForm, last_name: e.target.value })} />
setStaffForm({ ...staffForm, email: e.target.value })} />
setStaffForm({ ...staffForm, phone: e.target.value })} />
setStaffForm({ ...staffForm, password: e.target.value })} />
{staffList.length > 0 && ( Existing Staff Members {staffList.length} staff members found
{staffList.map((staff) => (

{staff.display_name}

{staff.role} • {staff.location?.name}

{staff.is_active ? 'Active' : 'Inactive'}

{new Date(staff.created_at).toLocaleDateString()}

))}
)}
Create Kiosk Add a new kiosk to a location
setKioskForm({ ...kioskForm, device_name: e.target.value })} />
setKioskForm({ ...kioskForm, display_name: e.target.value })} />
setKioskForm({ ...kioskForm, ip_address: e.target.value })} />
{message?.type === 'success' && message.text.includes('API key') && (

⚠️ Important: Save your API Key

The API key will only be shown once. Make sure to save it securely and add it to your environment variables.

)}
{kioskList.length > 0 && ( Existing Kiosks {kioskList.length} kiosks found
{kioskList.map((kiosk) => (

{kiosk.display_name}

{kiosk.device_name} • {kiosk.location?.name}

{kiosk.ip_address || 'No IP restriction'}

{kiosk.is_active ? 'Active' : 'Inactive'}

{new Date(kiosk.created_at).toLocaleDateString()}

))}
)}
) }