- Backend: FastAPI app with all models, services, schemas, routes - Admin Panel: Next.js dashboard with all modules (CRM, POS, repairs, warranties, billing, notifications, etc.) - Ecommerce: storefront (submodule) + product catalog - AI Agents: .agents/ skill library for Antigravity IDE - Docs: architecture, billing, notification gateway markdown - Submodules: Backend/OpenWA, Ecommerce/File 2/storefront
358 lines
19 KiB
TypeScript
358 lines
19 KiB
TypeScript
'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<Tab>('locations');
|
||
|
||
const [locations, setLocations] = useState<CrmLocation[]>([]);
|
||
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<DeviceTypeRegistry[]>([]);
|
||
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 (
|
||
<div className="min-h-screen bg-background text-foreground">
|
||
<div className="border-b border-border bg-card px-6 py-5">
|
||
<div className="flex items-center justify-between">
|
||
<div className="flex items-center gap-3">
|
||
<div className="flex h-10 w-10 items-center justify-center rounded-xl bg-primary/10">
|
||
<Globe className="h-5 w-5 text-primary" />
|
||
</div>
|
||
<div>
|
||
<h1 className="text-xl font-bold tracking-tight">Store Configuration</h1>
|
||
<p className="text-[13px] text-muted-foreground">Manage store locations and device type registry</p>
|
||
</div>
|
||
</div>
|
||
<button onClick={() => { fetchLocations(); fetchDeviceTypes(); }}
|
||
className="flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-[13px] text-muted-foreground hover:bg-muted transition-colors">
|
||
<RefreshCw className="h-4 w-4" /> Refresh
|
||
</button>
|
||
</div>
|
||
<div className="mt-5 flex gap-1 rounded-xl bg-muted p-1 w-fit">
|
||
{(['locations', 'devices'] as Tab[]).map((t) => (
|
||
<button key={t} onClick={() => setTab(t)}
|
||
className={`flex items-center gap-2 rounded-lg px-4 py-2 text-[13px] font-medium transition-all ${tab === t ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`}>
|
||
{t === 'locations' ? <MapPin className="h-4 w-4" /> : <Cpu className="h-4 w-4" />}
|
||
{t === 'locations' ? 'Store Locations' : 'Device Registry'}
|
||
</button>
|
||
))}
|
||
</div>
|
||
</div>
|
||
|
||
<div className="p-6">
|
||
{tab === 'locations' && (
|
||
<div className="space-y-5">
|
||
<div className="flex items-center justify-between">
|
||
<div>
|
||
<h2 className="font-semibold text-[15px]">Store Locations</h2>
|
||
<p className="text-[13px] text-muted-foreground mt-0.5">Physical stores, warehouses, and service centres</p>
|
||
</div>
|
||
<button onClick={() => setShowLocForm((v) => !v)}
|
||
className="flex items-center gap-2 rounded-lg bg-primary px-4 py-2 text-[13px] font-semibold text-primary-foreground hover:bg-primary/90 transition-colors">
|
||
{showLocForm ? <X className="h-4 w-4" /> : <Plus className="h-4 w-4" />}
|
||
{showLocForm ? 'Cancel' : 'Add Location'}
|
||
</button>
|
||
</div>
|
||
|
||
{showLocForm && (
|
||
<form onSubmit={handleCreateLocation} className="rounded-xl border border-border bg-card p-5 space-y-4">
|
||
<h3 className="font-semibold text-[14px] flex items-center gap-2">
|
||
<Building2 className="h-4 w-4 text-primary" /> New Store Location
|
||
</h3>
|
||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||
<div>
|
||
<label className="block text-[12px] font-medium text-muted-foreground mb-1">Code <span className="text-red-500">*</span></label>
|
||
<input type="text" value={locCode} onChange={(e) => setLocCode(e.target.value.toUpperCase())} placeholder="e.g. CHENNAI-01" maxLength={20} className={inputCls} />
|
||
</div>
|
||
<div>
|
||
<label className="block text-[12px] font-medium text-muted-foreground mb-1">Name <span className="text-red-500">*</span></label>
|
||
<input type="text" value={locName} onChange={(e) => setLocName(e.target.value)} placeholder="e.g. Chennai Main Store" className={inputCls} />
|
||
</div>
|
||
<div>
|
||
<label className="block text-[12px] font-medium text-muted-foreground mb-1">City</label>
|
||
<input type="text" value={locCity} onChange={(e) => setLocCity(e.target.value)} placeholder="Chennai" className={inputCls} />
|
||
</div>
|
||
<div>
|
||
<label className="block text-[12px] font-medium text-muted-foreground mb-1">State</label>
|
||
<input type="text" value={locState} onChange={(e) => setLocState(e.target.value)} placeholder="Tamil Nadu" className={inputCls} />
|
||
</div>
|
||
<div>
|
||
<label className="block text-[12px] font-medium text-muted-foreground mb-1">Pincode</label>
|
||
<input type="text" value={locPincode} onChange={(e) => setLocPincode(e.target.value)} placeholder="600001" maxLength={6} className={inputCls} />
|
||
</div>
|
||
<div>
|
||
<label className="block text-[12px] font-medium text-muted-foreground mb-1">Phone</label>
|
||
<input type="tel" value={locPhone} onChange={(e) => setLocPhone(e.target.value)} placeholder="+91 9876543210" className={inputCls} />
|
||
</div>
|
||
<div className="sm:col-span-2">
|
||
<label className="block text-[12px] font-medium text-muted-foreground mb-1">Full Address</label>
|
||
<input type="text" value={locAddress} onChange={(e) => setLocAddress(e.target.value)} placeholder="Street, area, landmark" className={inputCls} />
|
||
</div>
|
||
<div className="sm:col-span-2">
|
||
<label className="flex items-center gap-2 cursor-pointer">
|
||
<input type="checkbox" checked={locIsPrimary} onChange={(e) => setLocIsPrimary(e.target.checked)} className="rounded border-border accent-primary" />
|
||
<span className="text-[13px]">Set as primary / HQ location</span>
|
||
</label>
|
||
</div>
|
||
</div>
|
||
<div className="flex justify-end gap-3 pt-2">
|
||
<button type="button" onClick={resetLocForm} className="rounded-lg border border-border px-4 py-2 text-[13px] text-muted-foreground hover:bg-muted transition-colors">Cancel</button>
|
||
<button type="submit" disabled={creatingLoc}
|
||
className="flex items-center gap-2 rounded-lg bg-primary px-5 py-2 text-[13px] font-semibold text-primary-foreground hover:bg-primary/90 transition-colors disabled:opacity-50">
|
||
{creatingLoc ? <Loader2 className="h-4 w-4 animate-spin" /> : <Plus className="h-4 w-4" />}
|
||
{creatingLoc ? 'Creating...' : 'Create Location'}
|
||
</button>
|
||
</div>
|
||
</form>
|
||
)}
|
||
|
||
{loadingLocations ? (
|
||
<div className="flex items-center justify-center py-16"><Loader2 className="h-6 w-6 animate-spin text-primary" /></div>
|
||
) : locations.length === 0 ? (
|
||
<div className="rounded-xl border border-dashed border-border bg-card py-16 text-center">
|
||
<MapPin className="mx-auto h-10 w-10 text-muted-foreground/40 mb-3" />
|
||
<p className="text-[14px] font-medium text-muted-foreground">No locations added yet</p>
|
||
<p className="text-[13px] text-muted-foreground/70 mt-1">Click "Add Location" to register your first store</p>
|
||
</div>
|
||
) : (
|
||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||
{locations.map((loc) => (
|
||
<div key={loc.location_id} className="rounded-xl border border-border bg-card p-5 hover:border-primary/40 transition-colors">
|
||
<div className="flex items-start justify-between mb-3">
|
||
<div className="flex items-center gap-2">
|
||
<div className="h-9 w-9 rounded-lg bg-primary/10 flex items-center justify-center shrink-0">
|
||
<Building2 className="h-5 w-5 text-primary" />
|
||
</div>
|
||
<div>
|
||
<p className="font-semibold text-[14px] leading-tight">{loc.name}</p>
|
||
<span className="text-[11px] font-mono text-primary bg-primary/10 px-1.5 py-0.5 rounded">{loc.code}</span>
|
||
</div>
|
||
</div>
|
||
{(loc as any).is_primary && (
|
||
<span className="flex items-center gap-1 text-[11px] font-medium text-emerald-600 bg-emerald-50 dark:bg-emerald-950/40 border border-emerald-200 dark:border-emerald-800 px-2 py-0.5 rounded-full">
|
||
<CheckCircle className="h-3 w-3" /> Primary
|
||
</span>
|
||
)}
|
||
</div>
|
||
<div className="space-y-1.5 text-[12px] text-muted-foreground">
|
||
{loc.city && (
|
||
<div className="flex items-center gap-1.5">
|
||
<MapPin className="h-3.5 w-3.5 shrink-0" />
|
||
<span>{[loc.city, loc.state].filter(Boolean).join(', ')}{loc.pincode ? ` – ${loc.pincode}` : ''}</span>
|
||
</div>
|
||
)}
|
||
{loc.address && <p className="line-clamp-2 pl-5">{loc.address}</p>}
|
||
{loc.phone && (
|
||
<div className="flex items-center gap-1.5">
|
||
<Phone className="h-3.5 w-3.5 shrink-0" /><span>{loc.phone}</span>
|
||
</div>
|
||
)}
|
||
</div>
|
||
<div className="mt-3 pt-3 border-t border-border">
|
||
<span className={`text-[11px] font-medium px-2 py-0.5 rounded-full ${loc.is_active ? 'bg-emerald-50 dark:bg-emerald-950/40 text-emerald-600 border border-emerald-200 dark:border-emerald-800' : 'bg-muted text-muted-foreground border border-border'}`}>
|
||
{loc.is_active ? 'Active' : 'Inactive'}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
|
||
{tab === 'devices' && (
|
||
<div className="space-y-5">
|
||
<div className="flex items-center justify-between">
|
||
<div>
|
||
<h2 className="font-semibold text-[15px]">Device Type Registry</h2>
|
||
<p className="text-[13px] text-muted-foreground mt-0.5">Device categories for inventory, repairs, and POS</p>
|
||
</div>
|
||
<button onClick={() => setShowDevForm((v) => !v)}
|
||
className="flex items-center gap-2 rounded-lg bg-primary px-4 py-2 text-[13px] font-semibold text-primary-foreground hover:bg-primary/90 transition-colors">
|
||
{showDevForm ? <X className="h-4 w-4" /> : <Plus className="h-4 w-4" />}
|
||
{showDevForm ? 'Cancel' : 'Register Device Type'}
|
||
</button>
|
||
</div>
|
||
|
||
{showDevForm && (
|
||
<form onSubmit={handleCreateDevice} className="rounded-xl border border-border bg-card p-5 space-y-4">
|
||
<h3 className="font-semibold text-[14px] flex items-center gap-2">
|
||
<Cpu className="h-4 w-4 text-primary" /> Register New Device Type
|
||
</h3>
|
||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||
<div>
|
||
<label className="block text-[12px] font-medium text-muted-foreground mb-1">Device Type Name <span className="text-red-500">*</span></label>
|
||
<input
|
||
type="text"
|
||
value={devName}
|
||
onChange={(e) => {
|
||
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}
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label className="block text-[12px] font-medium text-muted-foreground mb-1">Device Code <span className="text-red-500">*</span></label>
|
||
<input
|
||
type="text"
|
||
value={devCode}
|
||
onChange={(e) => setDevCode(e.target.value.toUpperCase())}
|
||
placeholder="e.g. MOBILE, LAPTOP, PC"
|
||
className={inputCls}
|
||
/>
|
||
</div>
|
||
</div>
|
||
<div className="flex justify-end gap-3 pt-2">
|
||
<button type="button" onClick={resetDevForm} className="rounded-lg border border-border px-4 py-2 text-[13px] text-muted-foreground hover:bg-muted transition-colors">Cancel</button>
|
||
<button type="submit" disabled={creatingDev}
|
||
className="flex items-center gap-2 rounded-lg bg-primary px-5 py-2 text-[13px] font-semibold text-primary-foreground hover:bg-primary/90 transition-colors disabled:opacity-50">
|
||
{creatingDev ? <Loader2 className="h-4 w-4 animate-spin" /> : <Plus className="h-4 w-4" />}
|
||
{creatingDev ? 'Registering...' : 'Register Device Type'}
|
||
</button>
|
||
</div>
|
||
</form>
|
||
)}
|
||
|
||
{loadingDevices ? (
|
||
<div className="flex items-center justify-center py-16"><Loader2 className="h-6 w-6 animate-spin text-primary" /></div>
|
||
) : deviceTypes.length === 0 ? (
|
||
<div className="rounded-xl border border-dashed border-border bg-card py-16 text-center">
|
||
<Cpu className="mx-auto h-10 w-10 text-muted-foreground/40 mb-3" />
|
||
<p className="text-[14px] font-medium text-muted-foreground">No device types registered yet</p>
|
||
<p className="text-[13px] text-muted-foreground/70 mt-1">Register device categories used in repair and inventory workflows</p>
|
||
</div>
|
||
) : (
|
||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
|
||
{deviceTypes.map((dev) => (
|
||
<div key={dev.device_type_id} className="rounded-xl border border-border bg-card p-4 hover:border-primary/40 transition-colors">
|
||
<div className="flex items-start justify-between mb-2">
|
||
<div className="h-9 w-9 rounded-lg bg-primary/10 flex items-center justify-center">
|
||
<Laptop className="h-5 w-5 text-primary" />
|
||
</div>
|
||
<span className={`text-[11px] font-medium px-2 py-0.5 rounded-full ${dev.is_active ? 'bg-emerald-50 dark:bg-emerald-950/40 text-emerald-600 border border-emerald-200 dark:border-emerald-800' : 'bg-muted text-muted-foreground border border-border'}`}>
|
||
{dev.is_active ? 'Active' : 'Inactive'}
|
||
</span>
|
||
</div>
|
||
<p className="font-semibold text-[14px] leading-tight mt-2">{dev.device_name || dev.name || dev.code}</p>
|
||
<p className="text-[12px] text-muted-foreground mt-0.5">{[dev.brand, dev.category].filter(Boolean).join(' · ') || 'System Registered'}</p>
|
||
<span className="inline-block text-[11px] font-mono text-primary bg-primary/10 px-1.5 py-0.5 rounded mt-2">{dev.code}</span>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|