'use client'; import { useEffect, useState } from 'react'; import { MapPin, Plus, Laptop, Loader2, Building2, Cpu, Phone, X, RefreshCw, } from 'lucide-react'; import { toast } from 'sonner'; import { systemConfigService, CrmLocation, DeviceTypeRegistry } from '@/services/api/systemConfigService'; type Tab = 'locations' | 'devices'; export default function StoreConfigPage() { const [tab, setTab] = useState('locations'); const [locations, setLocations] = useState([]); const [loadingLocations, setLoadingLocations] = useState(false); const [showLocForm, setShowLocForm] = useState(false); const [locCode, setLocCode] = useState(''); const [locName, setLocName] = useState(''); const [locAddress, setLocAddress] = useState(''); const [locCity, setLocCity] = useState(''); const [locState, setLocState] = useState(''); const [locPincode, setLocPincode] = useState(''); const [locPhone, setLocPhone] = useState(''); const [locIsPrimary, setLocIsPrimary] = useState(false); const [creatingLoc, setCreatingLoc] = useState(false); const [deviceTypes, setDeviceTypes] = useState([]); const [loadingDevices, setLoadingDevices] = useState(false); const [showDevForm, setShowDevForm] = useState(false); const [devName, setDevName] = useState(''); const [devCode, setDevCode] = useState(''); const [devCategory, setDevCategory] = useState('SMARTPHONE'); const [devBrand, setDevBrand] = useState('Apple'); const [creatingDev, setCreatingDev] = useState(false); const fetchLocations = async () => { setLoadingLocations(true); try { const data = await systemConfigService.getLocations(); setLocations(Array.isArray(data) ? data : []); } catch { setLocations([]); } finally { setLoadingLocations(false); } }; const fetchDeviceTypes = async () => { setLoadingDevices(true); try { const data = await systemConfigService.getDeviceTypes(); setDeviceTypes(Array.isArray(data) ? data : []); } catch { setDeviceTypes([]); } finally { setLoadingDevices(false); } }; useEffect(() => { fetchLocations(); fetchDeviceTypes(); }, []); const resetLocForm = () => { setLocCode(''); setLocName(''); setLocAddress(''); setLocCity(''); setLocState(''); setLocPincode(''); setLocPhone(''); setLocIsPrimary(false); setShowLocForm(false); }; const resetDevForm = () => { setDevName(''); setDevCode(''); setDevCategory('SMARTPHONE'); setDevBrand('Apple'); setShowDevForm(false); }; const handleCreateLocation = async (e: React.FormEvent) => { e.preventDefault(); if (!locCode.trim() || !locName.trim()) { toast.error('Code and name are required'); return; } setCreatingLoc(true); try { await systemConfigService.createLocation({ code: locCode.trim().toUpperCase(), name: locName.trim(), address: locAddress || undefined, city: locCity || undefined, state: locState || undefined, pincode: locPincode || undefined, phone: locPhone || undefined, is_primary: locIsPrimary, }); toast.success(`Location "${locName}" created`); resetLocForm(); fetchLocations(); } catch (err: any) { toast.error(err?.message || 'Failed to create location'); } finally { setCreatingLoc(false); } }; const handleCreateDevice = async (e: React.FormEvent) => { e.preventDefault(); if (!devName.trim() || !devCode.trim()) { toast.error('Device Type Name and Code are required'); return; } setCreatingDev(true); try { await systemConfigService.createDeviceType({ category: devName.trim(), brand: '', device_name: devName.trim(), code: devCode.trim().toUpperCase(), }); toast.success(`Device type "${devName}" registered`); resetDevForm(); fetchDeviceTypes(); } catch (err: any) { toast.error(err?.message || 'Failed to register device type'); } finally { setCreatingDev(false); } }; const fieldClass = 'w-full h-9 px-3 bg-card border border-border crm-radius-control text-[13px] text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-primary'; const primaryButton = 'inline-flex items-center justify-center gap-1.5 h-9 px-3 crm-radius-control bg-primary hover:bg-primary/90 text-white text-[13px] font-semibold cursor-pointer disabled:opacity-50'; const secondaryButton = 'inline-flex items-center justify-center gap-1.5 h-9 px-3 crm-radius-control border border-border bg-card text-foreground text-[13px] font-medium hover:bg-muted cursor-pointer'; const shell = 'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0'; const labelClass = 'block text-[11px] font-semibold text-muted-foreground uppercase mb-1.5'; const count = tab === 'locations' ? locations.length : deviceTypes.length; return (

Store Configuration

{count}
{(['locations', 'devices'] as Tab[]).map((t) => ( ))}
{tab === 'locations' && (

Store Locations

{showLocForm && (

New Store Location

setLocCode(e.target.value.toUpperCase())} placeholder="Code" maxLength={20} className={fieldClass} />
setLocName(e.target.value)} placeholder="Name" className={fieldClass} />
setLocCity(e.target.value)} placeholder="City" className={fieldClass} />
setLocState(e.target.value)} placeholder="State" className={fieldClass} />
setLocPincode(e.target.value)} placeholder="Pincode" maxLength={6} className={fieldClass} />
setLocPhone(e.target.value)} placeholder="Phone" className={fieldClass} />
setLocAddress(e.target.value)} placeholder="Address" className={fieldClass} />
)} {loadingLocations ? (
) : locations.length === 0 ? (
No locations yet
) : (
{locations.map((loc) => (

{loc.name}

{loc.code}
{(loc as any).is_primary && ( Primary )}
{loc.city && (
{[loc.city, loc.state].filter(Boolean).join(', ')}{loc.pincode ? ` – ${loc.pincode}` : ''}
)} {loc.address &&

{loc.address}

} {loc.phone && (
{loc.phone}
)}
{loc.is_active ? 'Active' : 'Inactive'}
))}
)}
)} {tab === 'devices' && (

Device Type Registry

{showDevForm && (

Register New Device Type

{ const val = e.target.value; setDevName(val); setDevCode(val.replace(/[^a-zA-Z0-9]/g, '_').toUpperCase()); }} placeholder="Device type" className={fieldClass} />
setDevCode(e.target.value.toUpperCase())} placeholder="Code" className={fieldClass} />
)} {loadingDevices ? (
) : deviceTypes.length === 0 ? (
No device types yet
) : (
{deviceTypes.map((dev) => (
{dev.is_active ? 'Active' : 'Inactive'}

{dev.device_name || dev.name || dev.code}

{[dev.brand, dev.category].filter(Boolean).join(' · ') || '—'}

{dev.code}
))}
)}
)}
); }