'use client'; import { useEffect, useState } from 'react'; import { MapPin, Plus, Laptop, Loader2, CheckCircle, Building2, Cpu, Phone, X, RefreshCw, Globe, } 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 inputCls = "w-full rounded-lg border border-border bg-background px-3 py-2 text-[13px] text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-primary/30"; return (

Store Configuration

Manage store locations and device type registry

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

Store Locations

Physical stores, warehouses, and service centres

{showLocForm && (

New Store Location

setLocCode(e.target.value.toUpperCase())} placeholder="e.g. CHENNAI-01" maxLength={20} className={inputCls} />
setLocName(e.target.value)} placeholder="e.g. Chennai Main Store" className={inputCls} />
setLocCity(e.target.value)} placeholder="Chennai" className={inputCls} />
setLocState(e.target.value)} placeholder="Tamil Nadu" className={inputCls} />
setLocPincode(e.target.value)} placeholder="600001" maxLength={6} className={inputCls} />
setLocPhone(e.target.value)} placeholder="+91 9876543210" className={inputCls} />
setLocAddress(e.target.value)} placeholder="Street, area, landmark" className={inputCls} />
)} {loadingLocations ? (
) : locations.length === 0 ? (

No locations added yet

Click "Add Location" to register your first store

) : (
{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

Device categories for inventory, repairs, and POS

{showDevForm && (

Register New Device Type

{ const val = e.target.value; setDevName(val); setDevCode(val.replace(/[^a-zA-Z0-9]/g, '_').toUpperCase()); }} placeholder="e.g. Mobile, Tablet, Laptop, PC, Console" className={inputCls} />
setDevCode(e.target.value.toUpperCase())} placeholder="e.g. MOBILE, LAPTOP, PC" className={inputCls} />
)} {loadingDevices ? (
) : deviceTypes.length === 0 ? (

No device types registered yet

Register device categories used in repair and inventory workflows

) : (
{deviceTypes.map((dev) => (
{dev.is_active ? 'Active' : 'Inactive'}

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

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

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