ifixkart-admin/app/(admin)/store-config/page.tsx

357 lines
17 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'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<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 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 (
<div className="flex flex-col gap-5 min-w-0">
<div className="flex flex-wrap items-center justify-between gap-3">
<div className="flex items-center gap-2 min-w-0">
<h1 className="text-lg font-semibold text-foreground">Store Configuration</h1>
<span className="inline-flex items-center justify-center min-w-5 h-5 px-1.5 crm-radius-toggle bg-primary/10 text-primary text-[11px] font-semibold leading-none">
{count}
</span>
</div>
<button
type="button"
onClick={() => { fetchLocations(); fetchDeviceTypes(); }}
className="w-8 h-8 crm-radius-control border border-border bg-card text-muted-foreground hover:text-foreground hover:bg-muted cursor-pointer flex items-center justify-center"
title="Reload"
>
<RefreshCw className={`w-3.5 h-3.5 ${(loadingLocations || loadingDevices) ? 'animate-spin' : ''}`} />
</button>
</div>
<div className="flex flex-wrap items-center gap-2">
{(['locations', 'devices'] as Tab[]).map((t) => (
<button
key={t}
type="button"
onClick={() => setTab(t)}
className={`inline-flex items-center gap-1.5 h-9 px-3 crm-radius-control text-[13px] font-semibold cursor-pointer ${
tab === t ? 'bg-primary text-white' : 'border border-border bg-card text-foreground hover:bg-muted'
}`}
>
{t === 'locations' ? <MapPin className="w-3.5 h-3.5" /> : <Cpu className="w-3.5 h-3.5" />}
{t === 'locations' ? 'Store Locations' : 'Device Registry'}
</button>
))}
</div>
<div>
{tab === 'locations' && (
<div className="space-y-5">
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<h2 className="text-[13px] font-semibold text-foreground">Store Locations</h2>
<button onClick={() => setShowLocForm((v) => !v)}
className={primaryButton}>
{showLocForm ? <X className="h-4 w-4" /> : <Plus className="h-4 w-4" />}
{showLocForm ? 'Cancel' : 'Add Location'}
</button>
</div>
{showLocForm && (
<form onSubmit={handleCreateLocation} className={`${shell} p-4 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={labelClass}>Code</label>
<input type="text" value={locCode} onChange={(e) => setLocCode(e.target.value.toUpperCase())} placeholder="Code" maxLength={20} className={fieldClass} />
</div>
<div>
<label className={labelClass}>Name</label>
<input type="text" value={locName} onChange={(e) => setLocName(e.target.value)} placeholder="Name" className={fieldClass} />
</div>
<div>
<label className={labelClass}>City</label>
<input type="text" value={locCity} onChange={(e) => setLocCity(e.target.value)} placeholder="City" className={fieldClass} />
</div>
<div>
<label className={labelClass}>State</label>
<input type="text" value={locState} onChange={(e) => setLocState(e.target.value)} placeholder="State" className={fieldClass} />
</div>
<div>
<label className={labelClass}>Pincode</label>
<input type="text" value={locPincode} onChange={(e) => setLocPincode(e.target.value)} placeholder="Pincode" maxLength={6} className={fieldClass} />
</div>
<div>
<label className={labelClass}>Phone</label>
<input type="tel" value={locPhone} onChange={(e) => setLocPhone(e.target.value)} placeholder="Phone" className={fieldClass} />
</div>
<div className="sm:col-span-2">
<label className={labelClass}>Address</label>
<input type="text" value={locAddress} onChange={(e) => setLocAddress(e.target.value)} placeholder="Address" className={fieldClass} />
</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={secondaryButton}>Cancel</button>
<button type="submit" disabled={creatingLoc}
className={primaryButton}>
{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={`${shell} py-16 text-center text-[13px] text-muted-foreground`}>No locations yet</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={`${shell} p-4`}>
<div className="flex items-start justify-between mb-3">
<div className="flex items-center gap-2">
<div className="h-9 w-9 rounded-full bg-primary/10 flex items-center justify-center shrink-0">
<Building2 className="h-4 w-4 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="inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold bg-primary/10 text-primary">
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={`inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold ${loc.is_active ? 'bg-success/15 text-success' : 'bg-muted text-muted-foreground'}`}>
{loc.is_active ? 'Active' : 'Inactive'}
</span>
</div>
</div>
))}
</div>
)}
</div>
)}
{tab === 'devices' && (
<div className="space-y-5">
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<h2 className="text-[13px] font-semibold text-foreground">Device Type Registry</h2>
<button onClick={() => setShowDevForm((v) => !v)}
className={primaryButton}>
{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={`${shell} p-4 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={labelClass}>Device type</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="Device type"
className={fieldClass}
/>
</div>
<div>
<label className={labelClass}>Code</label>
<input
type="text"
value={devCode}
onChange={(e) => setDevCode(e.target.value.toUpperCase())}
placeholder="Code"
className={fieldClass}
/>
</div>
</div>
<div className="flex justify-end gap-3 pt-2">
<button type="button" onClick={resetDevForm} className={secondaryButton}>Cancel</button>
<button type="submit" disabled={creatingDev}
className={primaryButton}>
{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={`${shell} py-16 text-center text-[13px] text-muted-foreground`}>No device types yet</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={`${shell} p-4`}>
<div className="flex items-start justify-between mb-2">
<div className="h-9 w-9 rounded-full bg-primary/10 flex items-center justify-center">
<Laptop className="h-4 w-4 text-primary" />
</div>
<span className={`inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold ${dev.is_active ? 'bg-success/15 text-success' : 'bg-muted text-muted-foreground'}`}>
{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(' ยท ') || 'โ€”'}</p>
<span className="inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-mono font-semibold bg-primary/10 text-primary mt-2">{dev.code}</span>
</div>
))}
</div>
)}
</div>
)}
</div>
</div>
);
}