New pages: - app/(admin)/warranties/ — full warranty management with claims - app/(admin)/refunds/ — refund management - app/(admin)/notification-templates/ — email/SMS template editor - app/(admin)/user-pos-analytics/ — per-user POS analytics New components: - components/quotations/ — quotation components - components/ui/PartNumbersModal.tsx — part numbers selection New services: - creditService.ts, deliverySettingsService.ts - notificationTemplateService.ts, roleNotificationSettingsService.ts Updated: - Sidebar navigation, dashboard, orders, POS sessions, products - Bulk quotations, CRM leads/quotations, notifications gateway - settings, service invoices, quotes, technician, users pages - adminService, billingFinanceService, catalogService, client - inventoryPosService, notificationGatewayService, openwaService, userService - Removed pos-sync page (merged into POS sessions)
1484 lines
71 KiB
TypeScript
1484 lines
71 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useMemo, useState, type ReactNode } from 'react';
|
|
import {
|
|
Settings,
|
|
Save,
|
|
Loader2,
|
|
User,
|
|
Shield,
|
|
FileText,
|
|
Building,
|
|
RefreshCw,
|
|
Upload,
|
|
Image as ImageIcon,
|
|
KeyRound,
|
|
LayoutDashboard,
|
|
Search,
|
|
Copy,
|
|
Check,
|
|
Lock,
|
|
} from 'lucide-react';
|
|
import { parseJwt, getAccessToken } from '@/services/api/client';
|
|
import { userService, UserResponse } from '@/services/api/userService';
|
|
import { toast } from 'sonner';
|
|
import { format } from 'date-fns';
|
|
|
|
import { systemConfigService, CrmLocation, DeviceTypeRegistry } from '@/services/api/systemConfigService';
|
|
import { creditService, CreditSettings } from '@/services/api/creditService';
|
|
import { deliverySettingsService, DeliverySettings } from '@/services/api/deliverySettingsService';
|
|
import { MapPin, Laptop, Plus, Coins, Percent, Clock, Sparkles, Truck } from 'lucide-react';
|
|
|
|
type SettingsBlade = 'overview' | 'credits' | 'delivery' | 'locations' | 'devices' | 'profile' | 'security' | 'branding';
|
|
|
|
function PropertyRow({ label, children }: { label: string; children: ReactNode }) {
|
|
return (
|
|
<div className="grid grid-cols-1 sm:grid-cols-[118px_minmax(0,1fr)] gap-x-3 gap-y-1 py-3 border-b border-border items-start sm:items-center min-w-0">
|
|
<dt className="text-[13px] text-muted-foreground leading-5 truncate">{label}</dt>
|
|
<dd className="text-[13px] font-semibold text-foreground leading-5 min-w-0 overflow-hidden">
|
|
{typeof children === 'string' ? (
|
|
<span className="block truncate" title={children}>
|
|
{children}
|
|
</span>
|
|
) : (
|
|
children
|
|
)}
|
|
</dd>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function SettingsPage() {
|
|
const [userRole, setUserRole] = useState('');
|
|
const [blade, setBlade] = useState<SettingsBlade>('overview');
|
|
const [navSearch, setNavSearch] = useState('');
|
|
const [copiedId, setCopiedId] = useState(false);
|
|
|
|
const [profile, setProfile] = useState<UserResponse | null>(null);
|
|
const [loadingProfile, setLoadingProfile] = useState(true);
|
|
const [firstName, setFirstName] = useState('');
|
|
const [lastName, setLastName] = useState('');
|
|
const [userName, setUserName] = useState('');
|
|
const [phone, setPhone] = useState('');
|
|
const [email, setEmail] = useState('');
|
|
const [savingProfile, setSavingProfile] = useState(false);
|
|
|
|
const [currentPassword, setCurrentPassword] = useState('');
|
|
const [newPassword, setNewPassword] = useState('');
|
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
const [savingPassword, setSavingPassword] = useState(false);
|
|
|
|
const [invoiceLogoUrl, setInvoiceLogoUrl] = useState('https://ifixkart.com/logo.png');
|
|
const [invoiceCompanyName, setInvoiceCompanyName] = useState('iFixKart Solutions Platform');
|
|
const [invoiceGstin, setInvoiceGstin] = useState('33AAAAA0000A1Z5');
|
|
const [invoiceStoreAddress, setInvoiceStoreAddress] = useState(
|
|
'Offline Main Store Counter, Chennai | +91 9876543210'
|
|
);
|
|
const [invoiceGstRate, setInvoiceGstRate] = useState('18.0');
|
|
const [uploadingLogo, setUploadingLogo] = useState(false);
|
|
|
|
const [creditSettings, setCreditSettings] = useState<CreditSettings | null>(null);
|
|
const [loadingCredits, setLoadingCredits] = useState(false);
|
|
const [savingCredits, setSavingCredits] = useState(false);
|
|
const [triggeringSweep, setTriggeringSweep] = useState(false);
|
|
|
|
const [deliverySettings, setDeliverySettings] = useState<DeliverySettings | null>(null);
|
|
const [loadingDelivery, setLoadingDelivery] = useState(false);
|
|
const [savingDelivery, setSavingDelivery] = useState(false);
|
|
|
|
const isSuperAdmin =
|
|
userRole === 'super admin' || userRole === 'super_admin' || userRole === 'superadmin';
|
|
|
|
const fetchBrandingSettings = async () => {
|
|
if (typeof window === 'undefined') return;
|
|
const savedBranding = localStorage.getItem('ifixkart_invoice_branding');
|
|
if (savedBranding) {
|
|
try {
|
|
const parsed = JSON.parse(savedBranding);
|
|
if (parsed.logoUrl) setInvoiceLogoUrl(parsed.logoUrl);
|
|
if (parsed.companyName) setInvoiceCompanyName(parsed.companyName);
|
|
if (parsed.gstin) setInvoiceGstin(parsed.gstin);
|
|
if (parsed.storeAddress) setInvoiceStoreAddress(parsed.storeAddress);
|
|
if (parsed.gstRate !== undefined) setInvoiceGstRate(String(parsed.gstRate));
|
|
} catch {
|
|
/* ignore corrupt local branding */
|
|
}
|
|
}
|
|
|
|
try {
|
|
const token = getAccessToken();
|
|
const backendUrl = '';
|
|
const res = await fetch(`${backendUrl}/api/v1/settings/key/invoice_branding`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
const val = data.setting_value;
|
|
if (val && typeof val === 'object') {
|
|
if (val.logoUrl) setInvoiceLogoUrl(val.logoUrl);
|
|
if (val.companyName) setInvoiceCompanyName(val.companyName);
|
|
if (val.gstin) setInvoiceGstin(val.gstin);
|
|
if (val.storeAddress) setInvoiceStoreAddress(val.storeAddress);
|
|
if (val.gstRate !== undefined) setInvoiceGstRate(String(val.gstRate));
|
|
}
|
|
}
|
|
} catch {
|
|
/* branding endpoint optional */
|
|
}
|
|
};
|
|
|
|
const fetchUserProfile = async () => {
|
|
setLoadingProfile(true);
|
|
try {
|
|
const token = getAccessToken();
|
|
if (!token) return;
|
|
|
|
const payload = parseJwt(token);
|
|
const userId = payload?.sub as string;
|
|
if (!userId) return;
|
|
|
|
const user = await userService.getProfile(userId);
|
|
setProfile(user);
|
|
setFirstName(user.first_name || '');
|
|
setLastName(user.last_name || '');
|
|
setUserName(user.display_name || user.email.split('@')[0]);
|
|
} catch {
|
|
toast.error('Failed to load user profile');
|
|
} finally {
|
|
setLoadingProfile(false);
|
|
}
|
|
};
|
|
|
|
const [locations, setLocations] = useState<CrmLocation[]>([]);
|
|
const [deviceTypes, setDeviceTypes] = useState<DeviceTypeRegistry[]>([]);
|
|
const [loadingLocations, setLoadingLocations] = useState(false);
|
|
const [loadingDeviceTypes, setLoadingDeviceTypes] = useState(false);
|
|
|
|
// New location form state
|
|
const [locCode, setLocCode] = useState('');
|
|
const [locName, setLocName] = useState('');
|
|
const [locAddress, setLocAddress] = useState('');
|
|
const [locCity, setLocCity] = useState('');
|
|
const [locPhone, setLocPhone] = useState('');
|
|
const [locIsPrimary, setLocIsPrimary] = useState(false);
|
|
const [creatingLoc, setCreatingLoc] = useState(false);
|
|
|
|
// New device form state
|
|
const [devCategory, setDevCategory] = useState('SMARTPHONE');
|
|
const [devBrand, setDevBrand] = useState('Apple');
|
|
const [devName, setDevName] = useState('');
|
|
const [devCode, setDevCode] = useState('');
|
|
const [creatingDev, setCreatingDev] = useState(false);
|
|
|
|
const fetchLocations = async () => {
|
|
setLoadingLocations(true);
|
|
try {
|
|
const data = await systemConfigService.getLocations();
|
|
setLocations(data);
|
|
} catch {
|
|
toast.error('Failed to load store locations');
|
|
} finally {
|
|
setLoadingLocations(false);
|
|
}
|
|
};
|
|
|
|
const fetchDeviceTypes = async () => {
|
|
setLoadingDeviceTypes(true);
|
|
try {
|
|
const data = await systemConfigService.getDeviceTypes();
|
|
setDeviceTypes(data);
|
|
} catch {
|
|
toast.error('Failed to load device type registry');
|
|
} finally {
|
|
setLoadingDeviceTypes(false);
|
|
}
|
|
};
|
|
|
|
const handleCreateLocation = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!locCode || !locName) {
|
|
toast.error('Location code and name are required');
|
|
return;
|
|
}
|
|
setCreatingLoc(true);
|
|
try {
|
|
await systemConfigService.createLocation({
|
|
code: locCode.trim().toUpperCase(),
|
|
name: locName.trim(),
|
|
address: locAddress,
|
|
city: locCity,
|
|
phone: locPhone,
|
|
is_primary: locIsPrimary,
|
|
});
|
|
toast.success('Store location created');
|
|
setLocCode('');
|
|
setLocName('');
|
|
setLocAddress('');
|
|
setLocCity('');
|
|
setLocPhone('');
|
|
setLocIsPrimary(false);
|
|
fetchLocations();
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to create location');
|
|
} finally {
|
|
setCreatingLoc(false);
|
|
}
|
|
};
|
|
|
|
const handleCreateDeviceType = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!devName || !devCode) {
|
|
toast.error('Device name and code are required');
|
|
return;
|
|
}
|
|
setCreatingDev(true);
|
|
try {
|
|
await systemConfigService.createDeviceType({
|
|
category: devCategory,
|
|
brand: devBrand,
|
|
device_name: devName.trim(),
|
|
code: devCode.trim().toUpperCase(),
|
|
});
|
|
toast.success('Device type registered');
|
|
setDevName('');
|
|
setDevCode('');
|
|
fetchDeviceTypes();
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to register device type');
|
|
} finally {
|
|
setCreatingDev(false);
|
|
}
|
|
};
|
|
|
|
const fetchCreditSettings = async () => {
|
|
setLoadingCredits(true);
|
|
try {
|
|
const data = await creditService.getSettings();
|
|
setCreditSettings(data);
|
|
} catch (err: any) {
|
|
console.error(err);
|
|
toast.error('Failed to load store credit settings');
|
|
} finally {
|
|
setLoadingCredits(false);
|
|
}
|
|
};
|
|
|
|
const handleSaveCredits = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!creditSettings) return;
|
|
setSavingCredits(true);
|
|
try {
|
|
const updated = await creditService.updateSettings(creditSettings);
|
|
setCreditSettings(updated);
|
|
toast.success('Store credit rules & return windows updated successfully');
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to update credit settings');
|
|
} finally {
|
|
setSavingCredits(false);
|
|
}
|
|
};
|
|
|
|
const handleTriggerSweep = async () => {
|
|
setTriggeringSweep(true);
|
|
try {
|
|
const res = await creditService.triggerMaturitySweep();
|
|
toast.success(`Sweep complete: ${res.result?.matured_count || 0} matured, ${res.result?.voided_count || 0} voided.`);
|
|
fetchCreditSettings();
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to trigger maturity sweep');
|
|
} finally {
|
|
setTriggeringSweep(false);
|
|
}
|
|
};
|
|
|
|
const fetchDeliverySettings = async () => {
|
|
setLoadingDelivery(true);
|
|
try {
|
|
const data = await deliverySettingsService.getSettings();
|
|
setDeliverySettings(data);
|
|
} catch (err: any) {
|
|
console.error(err);
|
|
toast.error('Failed to load delivery settings');
|
|
} finally {
|
|
setLoadingDelivery(false);
|
|
}
|
|
};
|
|
|
|
const handleSaveDelivery = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!deliverySettings) return;
|
|
setSavingDelivery(true);
|
|
try {
|
|
const updated = await deliverySettingsService.updateSettings(deliverySettings);
|
|
setDeliverySettings(updated);
|
|
toast.success('Delivery charges & dynamic shipping rules updated successfully');
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to update delivery settings');
|
|
} finally {
|
|
setSavingDelivery(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const token = getAccessToken();
|
|
if (token) {
|
|
const payload = parseJwt(token);
|
|
if (payload) {
|
|
setUserRole(((payload.role as string) || '').toLowerCase());
|
|
}
|
|
}
|
|
fetchBrandingSettings();
|
|
fetchUserProfile();
|
|
fetchLocations();
|
|
fetchDeviceTypes();
|
|
fetchCreditSettings();
|
|
fetchDeliverySettings();
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (blade === 'branding' && !isSuperAdmin) setBlade('overview');
|
|
}, [blade, isSuperAdmin]);
|
|
|
|
const handleSaveProfile = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!profile) return;
|
|
setSavingProfile(true);
|
|
try {
|
|
await userService.update(profile.user_id, {
|
|
first_name: firstName,
|
|
last_name: lastName,
|
|
phone: phone,
|
|
display_name: userName,
|
|
});
|
|
toast.success('Profile saved');
|
|
fetchUserProfile();
|
|
} catch (err: unknown) {
|
|
const message = err instanceof Error ? err.message : 'Failed to update profile';
|
|
toast.error(message);
|
|
} finally {
|
|
setSavingProfile(false);
|
|
}
|
|
};
|
|
|
|
const handleDiscardProfile = () => {
|
|
if (!profile) return;
|
|
setFirstName(profile.first_name || '');
|
|
setLastName(profile.last_name || '');
|
|
setPhone(profile.phone || '');
|
|
setEmail(profile.email || '');
|
|
setUserName(profile.display_name || profile.email.split('@')[0]);
|
|
};
|
|
|
|
const handleUpdatePassword = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!newPassword || !confirmPassword) {
|
|
toast.error('Enter a new password');
|
|
return;
|
|
}
|
|
if (newPassword !== confirmPassword) {
|
|
toast.error('New password and confirmation do not match');
|
|
return;
|
|
}
|
|
setSavingPassword(true);
|
|
try {
|
|
if (!profile) return;
|
|
await userService.update(profile.user_id, {
|
|
password: newPassword,
|
|
});
|
|
toast.success('Password updated');
|
|
setCurrentPassword('');
|
|
setNewPassword('');
|
|
setConfirmPassword('');
|
|
} catch (err: unknown) {
|
|
const message = err instanceof Error ? err.message : 'Password update failed';
|
|
toast.error(message);
|
|
} finally {
|
|
setSavingPassword(false);
|
|
}
|
|
};
|
|
|
|
const handleLogoFileUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const file = e.target.files?.[0];
|
|
if (!file) return;
|
|
|
|
setUploadingLogo(true);
|
|
try {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
formData.append('entity_type', 'invoice_branding');
|
|
formData.append('entity_id', 'logo');
|
|
|
|
const token = getAccessToken();
|
|
const backendUrl = '';
|
|
const res = await fetch(`${backendUrl}/api/v1/files/upload`, {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
body: formData,
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const reader = new FileReader();
|
|
reader.onload = (evt) => {
|
|
if (evt.target?.result) {
|
|
setInvoiceLogoUrl(evt.target.result as string);
|
|
toast.success('Invoice logo uploaded');
|
|
}
|
|
};
|
|
reader.readAsDataURL(file);
|
|
return;
|
|
}
|
|
|
|
const data = await res.json();
|
|
const relativePath =
|
|
data.webp_path || data.storage_path || data.thumbnail_path || data.raw_path || data.url || data.file_path || '';
|
|
const uploadedUrl = relativePath.startsWith('http')
|
|
? relativePath
|
|
: `${backendUrl}${relativePath.startsWith('/') ? '' : '/'}${relativePath}`;
|
|
setInvoiceLogoUrl(uploadedUrl);
|
|
toast.success('Invoice logo uploaded');
|
|
} catch {
|
|
const reader = new FileReader();
|
|
reader.onload = (evt) => {
|
|
if (evt.target?.result) {
|
|
setInvoiceLogoUrl(evt.target.result as string);
|
|
toast.success('Invoice logo loaded');
|
|
}
|
|
};
|
|
reader.readAsDataURL(file);
|
|
} finally {
|
|
setUploadingLogo(false);
|
|
}
|
|
};
|
|
|
|
const handleSaveInvoiceBranding = async () => {
|
|
const brandingData = {
|
|
logoUrl: invoiceLogoUrl,
|
|
companyName: invoiceCompanyName,
|
|
gstin: invoiceGstin,
|
|
storeAddress: invoiceStoreAddress,
|
|
gstRate: Number(invoiceGstRate) || 18.0,
|
|
};
|
|
localStorage.setItem('ifixkart_invoice_branding', JSON.stringify(brandingData));
|
|
|
|
try {
|
|
const token = getAccessToken();
|
|
const backendUrl = '';
|
|
await fetch(`${backendUrl}/api/v1/settings/save/invoice_branding`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
|
body: JSON.stringify({
|
|
setting_value: brandingData,
|
|
description: 'Invoice logo and store branding configurations',
|
|
is_public: true,
|
|
}),
|
|
});
|
|
} catch {
|
|
/* save still persisted locally */
|
|
}
|
|
|
|
toast.success('Invoice logo and branding saved');
|
|
};
|
|
|
|
const copyUserId = async () => {
|
|
if (!profile?.user_id) return;
|
|
try {
|
|
await navigator.clipboard.writeText(profile.user_id);
|
|
setCopiedId(true);
|
|
toast.success('User ID copied to clipboard');
|
|
setTimeout(() => setCopiedId(false), 1500);
|
|
} catch {
|
|
toast.error('Could not copy the user ID');
|
|
}
|
|
};
|
|
|
|
const reloadAll = () => {
|
|
fetchUserProfile();
|
|
fetchBrandingSettings();
|
|
fetchLocations();
|
|
fetchDeviceTypes();
|
|
fetchCreditSettings();
|
|
fetchDeliverySettings();
|
|
};
|
|
|
|
const navItems = useMemo(
|
|
() =>
|
|
[
|
|
{ id: 'overview' as const, label: 'Overview', hint: 'Resource essentials', icon: LayoutDashboard },
|
|
{ id: 'credits' as const, label: 'Credits & Return Windows', hint: 'Sales & service reward %, return window days', icon: Coins },
|
|
{ id: 'delivery' as const, label: 'Delivery & Shipping', hint: 'Delivery charges & dynamic free shipping rules', icon: Truck },
|
|
{ id: 'locations' as const, label: 'Store Locations', hint: 'Multi-store & warehouses', icon: MapPin },
|
|
{ id: 'devices' as const, label: 'Device Registry', hint: 'Dynamic multi-device master', icon: Laptop },
|
|
{ id: 'profile' as const, label: 'Identity', hint: 'Name, phone, username', icon: User },
|
|
{ id: 'security' as const, label: 'Access', hint: 'Password credentials', icon: KeyRound },
|
|
...(isSuperAdmin
|
|
? [{ id: 'branding' as const, label: 'Invoice branding', hint: 'Logo, GSTIN, store', icon: FileText }]
|
|
: []),
|
|
].filter((item) => {
|
|
const q = navSearch.trim().toLowerCase();
|
|
if (!q) return true;
|
|
return item.label.toLowerCase().includes(q) || item.hint.toLowerCase().includes(q);
|
|
}),
|
|
[isSuperAdmin, navSearch]
|
|
);
|
|
|
|
const dataCardShell =
|
|
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] overflow-visible min-w-0';
|
|
const labelClass = 'block text-[11px] font-semibold text-muted-foreground uppercase mb-1.5';
|
|
const inputClass =
|
|
'w-full h-9 px-3 crm-radius-control border border-border bg-card text-[13px] text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-primary transition-colors';
|
|
const primaryButton =
|
|
'inline-flex items-center justify-center gap-2 h-9 min-h-9 px-4 crm-radius-control bg-primary hover:bg-primary/90 text-white text-[13px] font-semibold cursor-pointer disabled:opacity-50 whitespace-nowrap';
|
|
const secondaryButton =
|
|
'inline-flex items-center justify-center gap-2 h-9 min-h-9 px-4 crm-radius-control border border-border bg-card text-foreground text-[13px] font-medium hover:bg-muted cursor-pointer disabled:opacity-50 whitespace-nowrap';
|
|
|
|
const fullName = [firstName, lastName].filter(Boolean).join(' ') || profile?.email || 'Administrator';
|
|
const roleLabel = userRole ? userRole.replace(/_/g, ' ') : 'Admin';
|
|
|
|
const bladeMeta: Record<SettingsBlade, { title: string; subtitle: string }> = {
|
|
overview: {
|
|
title: 'Overview',
|
|
subtitle: 'Account resource essentials and current identity status.',
|
|
},
|
|
credits: {
|
|
title: 'Store Credits & Return Windows',
|
|
subtitle: 'Configure automated cashback percentages and dynamic return window maturity periods for Sales and Repair Services.',
|
|
},
|
|
delivery: {
|
|
title: 'Delivery & Shipping Configuration',
|
|
subtitle: 'Set dynamic order delivery fees, toggle promotional free shipping, and configure minimum qualification thresholds.',
|
|
},
|
|
locations: {
|
|
title: 'Store Locations Manager',
|
|
subtitle: 'Configure retail stores, branches, and fulfillment warehouses.',
|
|
},
|
|
devices: {
|
|
title: 'Dynamic Multi-Device Master Registry',
|
|
subtitle: 'Register devices across smartphones, tablets, laptops, and custom gear.',
|
|
},
|
|
profile: {
|
|
title: 'Identity',
|
|
subtitle: 'Update the display credentials used across the admin console.',
|
|
},
|
|
security: {
|
|
title: 'Access credentials',
|
|
subtitle: 'Rotate the password used to sign in to this administrator account.',
|
|
},
|
|
branding: {
|
|
title: 'Invoice branding',
|
|
subtitle: 'Logo, legal name, GSTIN and store contact printed on invoices.',
|
|
},
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-5 min-w-0">
|
|
<div className="flex flex-wrap items-start justify-between gap-3 min-w-0">
|
|
<div className="min-w-0">
|
|
<div className="flex items-center gap-2">
|
|
<h1 className="text-lg font-semibold text-foreground">System Settings</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 capitalize">
|
|
{roleLabel}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={reloadAll}
|
|
className="w-8 h-8 crm-radius-control border border-border bg-card text-muted-foreground hover:text-foreground hover:bg-muted cursor-pointer transition-colors flex items-center justify-center"
|
|
title="Reload"
|
|
>
|
|
<RefreshCw className={`w-3.5 h-3.5 ${loadingProfile ? 'animate-spin' : ''}`} />
|
|
</button>
|
|
</div>
|
|
|
|
<div className={`${dataCardShell} p-4`}>
|
|
<div className="flex flex-col md:flex-row md:items-center justify-between gap-3 min-w-0">
|
|
<div className="flex items-center gap-3 min-w-0">
|
|
<div className="w-11 h-11 crm-radius-icon bg-primary/10 text-primary flex items-center justify-center shrink-0">
|
|
<Settings className="w-5 h-5" />
|
|
</div>
|
|
<div className="min-w-0">
|
|
<p className="text-[15px] font-semibold text-foreground truncate">{fullName}</p>
|
|
<p className="text-[13px] text-muted-foreground truncate">
|
|
{email || profile?.email || 'Administrator account'} · iFixKart console
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-wrap items-center gap-1.5">
|
|
<span
|
|
className={`inline-flex items-center px-2 py-0.5 crm-radius-badge text-[11px] font-semibold ${
|
|
profile?.is_active !== false ? 'bg-success text-white' : 'bg-muted text-muted-foreground'
|
|
}`}
|
|
>
|
|
{profile?.is_active !== false ? 'Active' : 'Inactive'}
|
|
</span>
|
|
<span className="inline-flex items-center px-2 py-0.5 crm-radius-badge bg-muted text-foreground text-[11px] font-semibold capitalize">
|
|
{roleLabel}
|
|
</span>
|
|
{profile?.email_verified && (
|
|
<span className="inline-flex items-center px-2 py-0.5 crm-radius-badge bg-success/10 text-success text-[11px] font-semibold">
|
|
Email verified
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 xl:grid-cols-[240px_minmax(0,1fr)] gap-4 min-w-0 items-start">
|
|
<aside className={`${dataCardShell} p-3 xl:sticky xl:top-5`}>
|
|
<div className="relative mb-2">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-muted-foreground pointer-events-none" />
|
|
<input
|
|
type="text"
|
|
value={navSearch}
|
|
onChange={(e) => setNavSearch(e.target.value)}
|
|
placeholder="Filter settings"
|
|
className="w-full h-9 pl-8 pr-3 crm-radius-control border border-border bg-card text-[13px] text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-primary"
|
|
/>
|
|
</div>
|
|
<nav className="flex flex-col gap-1">
|
|
{navItems.length === 0 ? (
|
|
<p className="px-3 py-6 text-center text-[12px] text-muted-foreground">No matching settings.</p>
|
|
) : (
|
|
navItems.map((item) => {
|
|
const Icon = item.icon;
|
|
const selected = blade === item.id;
|
|
return (
|
|
<button
|
|
key={item.id}
|
|
type="button"
|
|
onClick={() => setBlade(item.id)}
|
|
className={`relative w-full flex items-center gap-2.5 h-auto min-h-10 px-3 py-2 text-left crm-radius-toggle cursor-pointer transition-colors ${
|
|
selected
|
|
? 'bg-card border border-[#b8c0d4] text-foreground'
|
|
: 'border border-transparent text-foreground hover:bg-muted'
|
|
}`}
|
|
>
|
|
{selected && <span className="absolute left-0 top-1.5 bottom-1.5 w-[3px] bg-primary" />}
|
|
<span
|
|
className={`w-7 h-7 crm-radius-icon flex items-center justify-center shrink-0 ${
|
|
selected ? 'bg-primary/10 text-primary' : 'bg-muted text-muted-foreground'
|
|
}`}
|
|
>
|
|
<Icon className="w-3.5 h-3.5" />
|
|
</span>
|
|
<span className="min-w-0 overflow-hidden">
|
|
<span className="block text-[13px] font-semibold leading-5 truncate">{item.label}</span>
|
|
<span className="block text-[12px] text-muted-foreground mt-0.5 truncate">{item.hint}</span>
|
|
</span>
|
|
</button>
|
|
);
|
|
})
|
|
)}
|
|
</nav>
|
|
</aside>
|
|
|
|
<section className={`${dataCardShell} overflow-hidden`}>
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 px-5 py-4 border-b border-border min-w-0">
|
|
<div className="min-w-0">
|
|
<h2 className="text-[16px] font-semibold text-foreground">{bladeMeta[blade].title}</h2>
|
|
<p className="text-[13px] text-muted-foreground mt-1">{bladeMeta[blade].subtitle}</p>
|
|
</div>
|
|
{blade === 'profile' && (
|
|
<div className="flex items-center gap-2 shrink-0">
|
|
<button type="button" onClick={handleDiscardProfile} className={secondaryButton}>
|
|
Discard
|
|
</button>
|
|
<button type="submit" form="settings-profile-form" disabled={savingProfile} className={primaryButton}>
|
|
{savingProfile ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Save className="w-3.5 h-3.5" />}
|
|
Save
|
|
</button>
|
|
</div>
|
|
)}
|
|
{blade === 'security' && (
|
|
<button type="submit" form="settings-security-form" disabled={savingPassword} className={primaryButton}>
|
|
{savingPassword ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Lock className="w-3.5 h-3.5" />}
|
|
Update password
|
|
</button>
|
|
)}
|
|
{blade === 'branding' && isSuperAdmin && (
|
|
<button type="button" onClick={handleSaveInvoiceBranding} className={primaryButton}>
|
|
<Save className="w-3.5 h-3.5" />
|
|
Save branding
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
<div className="p-5 min-w-0 overflow-hidden">
|
|
{loadingProfile && blade !== 'branding' ? (
|
|
<div className="py-16 flex justify-center">
|
|
<Loader2 className="w-6 h-6 animate-spin text-primary" />
|
|
</div>
|
|
) : blade === 'overview' ? (
|
|
<div className="grid grid-cols-1 lg:grid-cols-2 gap-x-8 gap-y-6 min-w-0">
|
|
<dl className="min-w-0 overflow-hidden">
|
|
<p className="text-[14px] font-semibold text-foreground mb-1">Essentials</p>
|
|
<PropertyRow label="Resource">{fullName}</PropertyRow>
|
|
<PropertyRow label="User name">{userName || '—'}</PropertyRow>
|
|
<PropertyRow label="Email">{email || '—'}</PropertyRow>
|
|
<PropertyRow label="Phone">{phone || '—'}</PropertyRow>
|
|
<PropertyRow label="Role">
|
|
<span className="block truncate capitalize">{roleLabel}</span>
|
|
</PropertyRow>
|
|
</dl>
|
|
<dl className="min-w-0 overflow-hidden">
|
|
<p className="text-[14px] font-semibold text-foreground mb-1">Directory</p>
|
|
<PropertyRow label="User ID">
|
|
<div className="flex items-center gap-2 min-w-0 w-full">
|
|
<span className="block min-w-0 flex-1 overflow-hidden text-ellipsis whitespace-nowrap font-mono text-[13px]" title={profile?.user_id}>
|
|
{profile?.user_id || '—'}
|
|
</span>
|
|
{profile?.user_id && (
|
|
<button
|
|
type="button"
|
|
title="Copy user ID"
|
|
onClick={copyUserId}
|
|
className="w-7 h-7 crm-radius-control border border-border bg-card text-muted-foreground hover:text-foreground hover:bg-muted cursor-pointer flex items-center justify-center shrink-0"
|
|
>
|
|
{copiedId ? <Check className="w-3.5 h-3.5 text-success" /> : <Copy className="w-3.5 h-3.5" />}
|
|
</button>
|
|
)}
|
|
</div>
|
|
</PropertyRow>
|
|
<PropertyRow label="Employee code">{profile?.employee_code || '—'}</PropertyRow>
|
|
<PropertyRow label="Last login">
|
|
{profile?.last_login ? format(new Date(profile.last_login), 'dd MMM yyyy, hh:mm a') : '—'}
|
|
</PropertyRow>
|
|
<PropertyRow label="Created">
|
|
{profile?.created_at ? format(new Date(profile.created_at), 'dd MMM yyyy') : '—'}
|
|
</PropertyRow>
|
|
<PropertyRow label="Status">
|
|
<span
|
|
className={`inline-flex items-center px-2 py-0.5 crm-radius-badge text-[12px] font-semibold ${
|
|
profile?.is_active !== false ? 'bg-success text-white' : 'bg-muted text-muted-foreground'
|
|
}`}
|
|
>
|
|
{profile?.is_active !== false ? 'Active' : 'Inactive'}
|
|
</span>
|
|
</PropertyRow>
|
|
</dl>
|
|
<div className="lg:col-span-2 flex flex-wrap gap-2 pt-1">
|
|
<button type="button" onClick={() => setBlade('profile')} className={secondaryButton}>
|
|
<User className="w-3.5 h-3.5" />
|
|
Edit identity
|
|
</button>
|
|
<button type="button" onClick={() => setBlade('security')} className={secondaryButton}>
|
|
<Shield className="w-3.5 h-3.5" />
|
|
Manage access
|
|
</button>
|
|
</div>
|
|
</div>
|
|
) : blade === 'credits' ? (
|
|
<div className="space-y-6 max-w-4xl">
|
|
{loadingCredits ? (
|
|
<div className="py-12 flex justify-center items-center gap-2 text-muted-foreground text-[13px]">
|
|
<Loader2 className="w-5 h-5 animate-spin text-primary" /> Loading credit configurations...
|
|
</div>
|
|
) : (
|
|
<form onSubmit={handleSaveCredits} className="space-y-6">
|
|
{/* Master Switch Banner */}
|
|
<div className="p-4 border border-border crm-radius-section bg-card flex flex-col sm:flex-row sm:items-center justify-between gap-4">
|
|
<div>
|
|
<h4 className="text-[14px] font-semibold text-foreground flex items-center gap-2">
|
|
<Coins className="w-4 h-4 text-primary" /> Store Credit & Cashback Rewards
|
|
</h4>
|
|
<p className="text-[12px] text-muted-foreground mt-0.5">
|
|
Automatically calculate and award credit percentages on customer purchases and repair services.
|
|
</p>
|
|
</div>
|
|
<label className="relative inline-flex items-center cursor-pointer shrink-0">
|
|
<input
|
|
type="checkbox"
|
|
checked={creditSettings?.credits_enabled ?? true}
|
|
onChange={(e) =>
|
|
setCreditSettings((prev) => (prev ? { ...prev, credits_enabled: e.target.checked } : null))
|
|
}
|
|
className="sr-only peer"
|
|
/>
|
|
<div className="w-11 h-6 bg-muted peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-primary"></div>
|
|
<span className="ml-2.5 text-[13px] font-medium text-foreground">
|
|
{creditSettings?.credits_enabled ? 'Active' : 'Disabled'}
|
|
</span>
|
|
</label>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
{/* Sales Order Config Card */}
|
|
<div className="p-4 border border-border crm-radius-section bg-card space-y-4">
|
|
<div className="border-b border-border pb-3">
|
|
<h4 className="text-[13px] font-semibold text-foreground flex items-center gap-2">
|
|
<Percent className="w-4 h-4 text-emerald-600" /> E-Commerce Sales Orders
|
|
</h4>
|
|
<p className="text-[11px] text-muted-foreground mt-0.5">
|
|
Credit percentage and return window for store purchases.
|
|
</p>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>Cashback Percentage (%)</label>
|
|
<div className="relative">
|
|
<input
|
|
type="number"
|
|
step="0.1"
|
|
min="0"
|
|
max="100"
|
|
value={creditSettings?.credits_sales_percentage ?? 5.0}
|
|
onChange={(e) =>
|
|
setCreditSettings((prev) =>
|
|
prev ? { ...prev, credits_sales_percentage: parseFloat(e.target.value) || 0 } : null
|
|
)
|
|
}
|
|
className={inputClass}
|
|
required
|
|
/>
|
|
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-[12px] text-muted-foreground font-semibold">
|
|
%
|
|
</span>
|
|
</div>
|
|
<span className="text-[11px] text-muted-foreground mt-1 block">
|
|
e.g. 5.0 = 5% of total order final amount.
|
|
</span>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>Return Window (Days)</label>
|
|
<div className="relative">
|
|
<input
|
|
type="number"
|
|
min="0"
|
|
max="365"
|
|
value={creditSettings?.credits_sales_return_window_days ?? 7}
|
|
onChange={(e) =>
|
|
setCreditSettings((prev) =>
|
|
prev
|
|
? { ...prev, credits_sales_return_window_days: parseInt(e.target.value, 10) || 0 }
|
|
: null
|
|
)
|
|
}
|
|
className={inputClass}
|
|
required
|
|
/>
|
|
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-[12px] text-muted-foreground font-semibold">
|
|
days
|
|
</span>
|
|
</div>
|
|
<span className="text-[11px] text-muted-foreground mt-1 block">
|
|
Credits stay PENDING until this window closes. Cancelled/returned orders void credits.
|
|
</span>
|
|
</div>
|
|
|
|
{/* Preview Box */}
|
|
<div className="p-3 bg-muted/40 rounded border border-border/60 text-[12px] text-foreground">
|
|
<span className="font-semibold text-primary block mb-0.5">Example Calculation:</span>
|
|
₹2,000 Delivered Order ➔{' '}
|
|
<span className="font-bold text-emerald-600">
|
|
₹{(2000 * ((creditSettings?.credits_sales_percentage || 5) / 100)).toFixed(2)}
|
|
</span>{' '}
|
|
Store Credit unlocks after {creditSettings?.credits_sales_return_window_days || 7} days.
|
|
</div>
|
|
</div>
|
|
|
|
{/* Repair Service Jobs Config Card */}
|
|
<div className="p-4 border border-border crm-radius-section bg-card space-y-4">
|
|
<div className="border-b border-border pb-3">
|
|
<h4 className="text-[13px] font-semibold text-foreground flex items-center gap-2">
|
|
<Sparkles className="w-4 h-4 text-blue-600" /> Repair Service Jobs
|
|
</h4>
|
|
<p className="text-[11px] text-muted-foreground mt-0.5">
|
|
Credit percentage and claim window for completed repair jobs.
|
|
</p>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>Service Cashback Percentage (%)</label>
|
|
<div className="relative">
|
|
<input
|
|
type="number"
|
|
step="0.1"
|
|
min="0"
|
|
max="100"
|
|
value={creditSettings?.credits_service_percentage ?? 5.0}
|
|
onChange={(e) =>
|
|
setCreditSettings((prev) =>
|
|
prev
|
|
? { ...prev, credits_service_percentage: parseFloat(e.target.value) || 0 }
|
|
: null
|
|
)
|
|
}
|
|
className={inputClass}
|
|
required
|
|
/>
|
|
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-[12px] text-muted-foreground font-semibold">
|
|
%
|
|
</span>
|
|
</div>
|
|
<span className="text-[11px] text-muted-foreground mt-1 block">
|
|
e.g. 5.0 = 5% of captured repair invoice amount.
|
|
</span>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>Dispute / Claim Window (Days)</label>
|
|
<div className="relative">
|
|
<input
|
|
type="number"
|
|
min="0"
|
|
max="365"
|
|
value={creditSettings?.credits_service_claim_window_days ?? 3}
|
|
onChange={(e) =>
|
|
setCreditSettings((prev) =>
|
|
prev
|
|
? { ...prev, credits_service_claim_window_days: parseInt(e.target.value, 10) || 0 }
|
|
: null
|
|
)
|
|
}
|
|
className={inputClass}
|
|
required
|
|
/>
|
|
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-[12px] text-muted-foreground font-semibold">
|
|
days
|
|
</span>
|
|
</div>
|
|
<span className="text-[11px] text-muted-foreground mt-1 block">
|
|
Credits mature after the repair customer satisfaction/claim window closes.
|
|
</span>
|
|
</div>
|
|
|
|
{/* Preview Box */}
|
|
<div className="p-3 bg-muted/40 rounded border border-border/60 text-[12px] text-foreground">
|
|
<span className="font-semibold text-primary block mb-0.5">Example Calculation:</span>
|
|
₹1,500 Completed Repair ➔{' '}
|
|
<span className="font-bold text-blue-600">
|
|
₹{(1500 * ((creditSettings?.credits_service_percentage || 5) / 100)).toFixed(2)}
|
|
</span>{' '}
|
|
Store Credit unlocks after {creditSettings?.credits_service_claim_window_days || 3} days.
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Policy Thresholds Card */}
|
|
<div className="p-4 border border-border crm-radius-section bg-card space-y-4">
|
|
<h4 className="text-[13px] font-semibold text-foreground flex items-center gap-2">
|
|
<Clock className="w-4 h-4 text-amber-600" /> Eligibility & Expiration Limits
|
|
</h4>
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className={labelClass}>Minimum Purchase Amount (₹)</label>
|
|
<input
|
|
type="number"
|
|
min="0"
|
|
step="1"
|
|
value={creditSettings?.credits_minimum_order_value ?? 0.0}
|
|
onChange={(e) =>
|
|
setCreditSettings((prev) =>
|
|
prev ? { ...prev, credits_minimum_order_value: parseFloat(e.target.value) || 0 } : null
|
|
)
|
|
}
|
|
className={inputClass}
|
|
/>
|
|
<span className="text-[11px] text-muted-foreground mt-1 block">
|
|
Orders or services below this value will not generate credit. Set 0 to reward all purchases.
|
|
</span>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>Credit Validity Period (Days)</label>
|
|
<input
|
|
type="number"
|
|
min="0"
|
|
value={creditSettings?.credits_expiry_days ?? 365}
|
|
onChange={(e) =>
|
|
setCreditSettings((prev) =>
|
|
prev ? { ...prev, credits_expiry_days: parseInt(e.target.value, 10) || 0 } : null
|
|
)
|
|
}
|
|
className={inputClass}
|
|
/>
|
|
<span className="text-[11px] text-muted-foreground mt-1 block">
|
|
Days before spendable credits expire (e.g., 365 for 1 year, 0 for lifetime).
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Actions Row */}
|
|
<div className="flex flex-col sm:flex-row items-center justify-between gap-3 pt-2">
|
|
<button
|
|
type="button"
|
|
onClick={handleTriggerSweep}
|
|
disabled={triggeringSweep}
|
|
className={secondaryButton}
|
|
>
|
|
{triggeringSweep ? (
|
|
<Loader2 className="w-3.5 h-3.5 animate-spin" />
|
|
) : (
|
|
<RefreshCw className="w-3.5 h-3.5" />
|
|
)}
|
|
Run Maturity Sweep Now
|
|
</button>
|
|
|
|
<button type="submit" disabled={savingCredits} className={primaryButton}>
|
|
{savingCredits ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Save className="w-3.5 h-3.5" />}
|
|
Save Credit Rules & Return Windows
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)}
|
|
</div>
|
|
) : blade === 'delivery' ? (
|
|
<div className="space-y-6 max-w-4xl">
|
|
{loadingDelivery ? (
|
|
<div className="py-12 flex justify-center items-center gap-2 text-muted-foreground text-[13px]">
|
|
<Loader2 className="w-5 h-5 animate-spin text-primary" /> Loading delivery configurations...
|
|
</div>
|
|
) : (
|
|
<form onSubmit={handleSaveDelivery} className="space-y-6">
|
|
{/* Master Delivery Rule Card */}
|
|
<div className="p-4 border border-border crm-radius-section bg-card space-y-4">
|
|
<div className="border-b border-border pb-3">
|
|
<h4 className="text-[14px] font-semibold text-foreground flex items-center gap-2">
|
|
<Truck className="w-4 h-4 text-primary" /> Standard Delivery Fee
|
|
</h4>
|
|
<p className="text-[12px] text-muted-foreground mt-0.5">
|
|
Base delivery charge applied to e-commerce customer checkout orders across India.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="max-w-md">
|
|
<label className={labelClass}>Standard Delivery Charge (₹)</label>
|
|
<div className="relative">
|
|
<span className="absolute left-3 top-1/2 -translate-y-1/2 text-[13px] text-muted-foreground font-semibold">
|
|
₹
|
|
</span>
|
|
<input
|
|
type="number"
|
|
step="1"
|
|
min="0"
|
|
value={deliverySettings?.delivery_charge ?? 99.0}
|
|
onChange={(e) =>
|
|
setDeliverySettings((prev) =>
|
|
prev ? { ...prev, delivery_charge: parseFloat(e.target.value) || 0 } : null
|
|
)
|
|
}
|
|
className={`${inputClass} pl-7`}
|
|
required
|
|
/>
|
|
</div>
|
|
<span className="text-[11px] text-muted-foreground mt-1 block">
|
|
Default fee added to the order subtotal at checkout.
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Promotional Free Delivery Toggle & Threshold */}
|
|
<div className="p-4 border border-border crm-radius-section bg-card space-y-5">
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 border-b border-border pb-4">
|
|
<div>
|
|
<h4 className="text-[14px] font-semibold text-foreground flex items-center gap-2">
|
|
<Percent className="w-4 h-4 text-emerald-600" /> Promotional Free Delivery Rule
|
|
</h4>
|
|
<p className="text-[12px] text-muted-foreground mt-0.5">
|
|
When disabled, all orders incur the standard delivery charge. When enabled, orders meeting the threshold unlock free delivery.
|
|
</p>
|
|
</div>
|
|
|
|
<label className="relative inline-flex items-center cursor-pointer shrink-0">
|
|
<input
|
|
type="checkbox"
|
|
checked={deliverySettings?.free_delivery_enabled ?? false}
|
|
onChange={(e) =>
|
|
setDeliverySettings((prev) =>
|
|
prev ? { ...prev, free_delivery_enabled: e.target.checked } : null
|
|
)
|
|
}
|
|
className="sr-only peer"
|
|
/>
|
|
<div className="w-11 h-6 bg-muted peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-primary"></div>
|
|
<span className="ml-2.5 text-[13px] font-medium text-foreground">
|
|
{deliverySettings?.free_delivery_enabled ? 'Active' : 'Disabled'}
|
|
</span>
|
|
</label>
|
|
</div>
|
|
|
|
{deliverySettings?.free_delivery_enabled ? (
|
|
<div className="max-w-md space-y-2">
|
|
<label className={labelClass}>Free Delivery Minimum Order Amount (₹)</label>
|
|
<div className="relative">
|
|
<span className="absolute left-3 top-1/2 -translate-y-1/2 text-[13px] text-muted-foreground font-semibold">
|
|
₹
|
|
</span>
|
|
<input
|
|
type="number"
|
|
step="1"
|
|
min="0"
|
|
value={deliverySettings?.free_delivery_min_order_amount ?? 999.0}
|
|
onChange={(e) =>
|
|
setDeliverySettings((prev) =>
|
|
prev ? { ...prev, free_delivery_min_order_amount: parseFloat(e.target.value) || 0 } : null
|
|
)
|
|
}
|
|
className={`${inputClass} pl-7`}
|
|
required
|
|
/>
|
|
</div>
|
|
<span className="text-[11px] text-muted-foreground mt-1 block">
|
|
Customers whose cart subtotal is at or above this amount receive free delivery.
|
|
</span>
|
|
</div>
|
|
) : (
|
|
<div className="p-3 bg-muted/40 rounded border border-border/60 text-[12px] text-muted-foreground">
|
|
Free delivery is currently <strong className="text-foreground">DISABLED</strong>. All customer purchases will be charged the standard ₹{deliverySettings?.delivery_charge || 99} delivery fee regardless of cart size.
|
|
</div>
|
|
)}
|
|
|
|
{/* Live Calculation Preview */}
|
|
<div className="p-3 bg-muted/30 rounded border border-border text-[12px] text-foreground">
|
|
<span className="font-semibold text-primary block mb-0.5">Active Operational Rule:</span>
|
|
{deliverySettings?.free_delivery_enabled ? (
|
|
<>
|
|
Orders of <span className="font-bold text-emerald-600">₹{deliverySettings?.free_delivery_min_order_amount || 999}</span> or higher qualify for <span className="font-bold text-emerald-600">FREE delivery</span>. Orders below this amount incur a flat <span className="font-bold text-foreground">₹{deliverySettings?.delivery_charge || 99}</span> delivery fee.
|
|
</>
|
|
) : (
|
|
<>
|
|
Flat <span className="font-bold text-foreground">₹{deliverySettings?.delivery_charge || 99}</span> delivery fee applies to all orders. Promotional free delivery is not active.
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Actions Row */}
|
|
<div className="flex justify-end pt-2">
|
|
<button type="submit" disabled={savingDelivery} className={primaryButton}>
|
|
{savingDelivery ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Save className="w-3.5 h-3.5" />}
|
|
Save Delivery Rules
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)}
|
|
</div>
|
|
) : blade === 'locations' ? (
|
|
<div className="space-y-6">
|
|
<form onSubmit={handleCreateLocation} className="p-4 border border-border crm-radius-section bg-muted/10 space-y-4">
|
|
<h3 className="text-[14px] font-semibold text-foreground flex items-center gap-2">
|
|
<Plus className="w-4 h-4 text-primary" /> Add New Store Location
|
|
</h3>
|
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-3">
|
|
<div>
|
|
<label className={labelClass}>Location Code *</label>
|
|
<input type="text" placeholder="LOC-MAIN" value={locCode} onChange={(e) => setLocCode(e.target.value)} className={inputClass} required />
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Location Name *</label>
|
|
<input type="text" placeholder="Main Store Chennai" value={locName} onChange={(e) => setLocName(e.target.value)} className={inputClass} required />
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Phone Number</label>
|
|
<input type="text" placeholder="+91 9876543210" value={locPhone} onChange={(e) => setLocPhone(e.target.value)} className={inputClass} />
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>City</label>
|
|
<input type="text" placeholder="Chennai" value={locCity} onChange={(e) => setLocCity(e.target.value)} className={inputClass} />
|
|
</div>
|
|
<div className="md:col-span-2">
|
|
<label className={labelClass}>Address</label>
|
|
<input type="text" placeholder="123 Mount Road, Anna Salai" value={locAddress} onChange={(e) => setLocAddress(e.target.value)} className={inputClass} />
|
|
</div>
|
|
</div>
|
|
<div className="flex items-center justify-between pt-2">
|
|
<label className="flex items-center gap-2 text-[13px] text-foreground cursor-pointer">
|
|
<input type="checkbox" checked={locIsPrimary} onChange={(e) => setLocIsPrimary(e.target.checked)} className="rounded border-border" />
|
|
Set as Primary Store Location
|
|
</label>
|
|
<button type="submit" disabled={creatingLoc} className={primaryButton}>
|
|
{creatingLoc ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Plus className="w-3.5 h-3.5" />}
|
|
Create Location
|
|
</button>
|
|
</div>
|
|
</form>
|
|
|
|
<div className="space-y-3">
|
|
<h3 className="text-[14px] font-semibold text-foreground">Active Locations Registry ({locations.length})</h3>
|
|
{loadingLocations ? (
|
|
<div className="py-8 flex justify-center"><Loader2 className="w-5 h-5 animate-spin text-primary" /></div>
|
|
) : locations.length === 0 ? (
|
|
<p className="text-[13px] text-muted-foreground italic">No store locations registered yet.</p>
|
|
) : (
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
|
|
{locations.map((loc) => (
|
|
<div key={loc.location_id} className="p-3.5 border border-border crm-radius-section bg-card flex items-start justify-between">
|
|
<div>
|
|
<div className="flex items-center gap-2">
|
|
<span className="text-[14px] font-semibold text-foreground">{loc.name}</span>
|
|
<span className="px-1.5 py-0.5 text-[10px] font-mono bg-muted text-muted-foreground crm-radius-badge">{loc.code}</span>
|
|
{loc.is_primary && (
|
|
<span className="px-1.5 py-0.5 text-[10px] font-semibold bg-primary/10 text-primary crm-radius-badge">Primary</span>
|
|
)}
|
|
</div>
|
|
<p className="text-[12px] text-muted-foreground mt-1">{loc.address || 'No address specified'} {loc.city ? `· ${loc.city}` : ''}</p>
|
|
{loc.phone && <p className="text-[11px] font-mono text-muted-foreground mt-0.5">📞 {loc.phone}</p>}
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
) : blade === 'devices' ? (
|
|
<div className="space-y-6">
|
|
<form onSubmit={handleCreateDeviceType} className="p-4 border border-border crm-radius-section bg-muted/10 space-y-4">
|
|
<h3 className="text-[14px] font-semibold text-foreground flex items-center gap-2">
|
|
<Plus className="w-4 h-4 text-primary" /> Register Dynamic Device Type
|
|
</h3>
|
|
<div className="grid grid-cols-1 md:grid-cols-4 gap-3">
|
|
<div>
|
|
<label className={labelClass}>Category *</label>
|
|
<select value={devCategory} onChange={(e) => setDevCategory(e.target.value)} className={inputClass}>
|
|
<option value="SMARTPHONE">Smartphone</option>
|
|
<option value="TABLET">Tablet</option>
|
|
<option value="LAPTOP">Laptop</option>
|
|
<option value="SMARTWATCH">Smartwatch</option>
|
|
<option value="CONSOLE">Gaming Console</option>
|
|
<option value="CUSTOM">Custom Device</option>
|
|
</select>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Brand *</label>
|
|
<input type="text" placeholder="Apple, Samsung..." value={devBrand} onChange={(e) => setDevBrand(e.target.value)} className={inputClass} required />
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Device Name *</label>
|
|
<input type="text" placeholder="iPhone 14 Pro Max" value={devName} onChange={(e) => setDevName(e.target.value)} className={inputClass} required />
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Device Code *</label>
|
|
<input type="text" placeholder="DEV-IPH14PM" value={devCode} onChange={(e) => setDevCode(e.target.value)} className={inputClass} required />
|
|
</div>
|
|
</div>
|
|
<div className="flex justify-end pt-2">
|
|
<button type="submit" disabled={creatingDev} className={primaryButton}>
|
|
{creatingDev ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Plus className="w-3.5 h-3.5" />}
|
|
Register Device
|
|
</button>
|
|
</div>
|
|
</form>
|
|
|
|
<div className="space-y-3">
|
|
<h3 className="text-[14px] font-semibold text-foreground">Registered Multi-Device Master ({deviceTypes.length})</h3>
|
|
{loadingDeviceTypes ? (
|
|
<div className="py-8 flex justify-center"><Loader2 className="w-5 h-5 animate-spin text-primary" /></div>
|
|
) : deviceTypes.length === 0 ? (
|
|
<p className="text-[13px] text-muted-foreground italic">No dynamic devices registered yet.</p>
|
|
) : (
|
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-3">
|
|
{deviceTypes.map((dev) => (
|
|
<div key={dev.device_type_id} className="p-3 border border-border crm-radius-section bg-card">
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-[13px] font-semibold text-foreground truncate">{dev.device_name}</span>
|
|
<span className="px-1.5 py-0.5 text-[10px] font-mono bg-muted text-muted-foreground crm-radius-badge">{dev.code}</span>
|
|
</div>
|
|
<p className="text-[11px] text-muted-foreground mt-1 capitalize">
|
|
{dev.brand || 'All Brands'} · {(dev.category || 'General').toLowerCase()}
|
|
</p>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
) : blade === 'profile' ? (
|
|
<form id="settings-profile-form" onSubmit={handleSaveProfile} className="space-y-4 max-w-3xl">
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className={labelClass}>First name</label>
|
|
<input type="text" value={firstName} onChange={(e) => setFirstName(e.target.value)} className={inputClass} />
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Last name</label>
|
|
<input type="text" value={lastName} onChange={(e) => setLastName(e.target.value)} className={inputClass} />
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Username</label>
|
|
<input type="text" value={userName} onChange={(e) => setUserName(e.target.value)} className={inputClass} />
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Phone number</label>
|
|
<input type="text" value={phone} onChange={(e) => setPhone(e.target.value)} className={inputClass} />
|
|
</div>
|
|
<div className="md:col-span-2">
|
|
<label className={labelClass}>Email address</label>
|
|
<input
|
|
type="email"
|
|
value={email}
|
|
disabled
|
|
className={`${inputClass} bg-muted text-muted-foreground cursor-not-allowed`}
|
|
/>
|
|
<p className="text-[13px] text-muted-foreground mt-1.5">Email is bound to this directory account and cannot be changed here.</p>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
) : blade === 'security' ? (
|
|
<form id="settings-security-form" onSubmit={handleUpdatePassword} className="space-y-4 max-w-3xl">
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
<div className="md:col-span-2">
|
|
<label className={labelClass}>Current password</label>
|
|
<input
|
|
type="password"
|
|
placeholder="••••••••"
|
|
value={currentPassword}
|
|
onChange={(e) => setCurrentPassword(e.target.value)}
|
|
className={inputClass}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>New password</label>
|
|
<input
|
|
type="password"
|
|
placeholder="••••••••"
|
|
value={newPassword}
|
|
onChange={(e) => setNewPassword(e.target.value)}
|
|
className={inputClass}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Confirm new password</label>
|
|
<input
|
|
type="password"
|
|
placeholder="••••••••"
|
|
value={confirmPassword}
|
|
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
className={inputClass}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<p className="text-[13px] text-muted-foreground">
|
|
Updating the password signs this administrator in with the new credential on the next login.
|
|
</p>
|
|
</form>
|
|
) : (
|
|
<div className="grid grid-cols-1 xl:grid-cols-[minmax(0,1fr)_260px] gap-5 min-w-0">
|
|
<div className="space-y-4 min-w-0">
|
|
<div>
|
|
<label className={labelClass}>Company / invoice brand logo</label>
|
|
<div className="flex items-start gap-3 min-w-0">
|
|
{invoiceLogoUrl ? (
|
|
<div className="w-16 h-16 crm-radius-icon border border-border bg-card p-1.5 flex items-center justify-center overflow-hidden shrink-0">
|
|
<img src={invoiceLogoUrl} alt="Invoice logo preview" className="max-w-full max-h-full object-contain" />
|
|
</div>
|
|
) : (
|
|
<div className="w-16 h-16 crm-radius-icon border border-dashed border-border bg-muted/20 flex flex-col items-center justify-center shrink-0 text-muted-foreground">
|
|
<ImageIcon className="w-5 h-5" />
|
|
</div>
|
|
)}
|
|
<div className="flex-1 min-w-0 space-y-2">
|
|
<input
|
|
type="text"
|
|
value={invoiceLogoUrl}
|
|
onChange={(e) => setInvoiceLogoUrl(e.target.value)}
|
|
placeholder="https://ifixkart.com/logo.png"
|
|
className={inputClass}
|
|
/>
|
|
<input
|
|
type="file"
|
|
accept="image/*"
|
|
id="invoiceLogoFileInput"
|
|
className="hidden"
|
|
onChange={handleLogoFileUpload}
|
|
/>
|
|
<button
|
|
type="button"
|
|
disabled={uploadingLogo}
|
|
onClick={() => document.getElementById('invoiceLogoFileInput')?.click()}
|
|
className={secondaryButton}
|
|
>
|
|
{uploadingLogo ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Upload className="w-3.5 h-3.5" />}
|
|
{uploadingLogo ? 'Uploading...' : 'Upload logo'}
|
|
</button>
|
|
<p className="text-[13px] text-muted-foreground">PNG, JPG, WebP or SVG. Transparent logos print cleanest.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Company legal name</label>
|
|
<input
|
|
type="text"
|
|
value={invoiceCompanyName}
|
|
onChange={(e) => setInvoiceCompanyName(e.target.value)}
|
|
placeholder="iFixKart Solutions Platform"
|
|
className={inputClass}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>GSTIN / tax registration no.</label>
|
|
<input
|
|
type="text"
|
|
value={invoiceGstin}
|
|
onChange={(e) => setInvoiceGstin(e.target.value)}
|
|
placeholder="33AAAAA0000A1Z5"
|
|
className={inputClass}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Default GST / Tax Rate (%)</label>
|
|
<div className="flex items-center gap-2">
|
|
<input
|
|
type="number"
|
|
step="0.1"
|
|
min="0"
|
|
max="100"
|
|
value={invoiceGstRate}
|
|
onChange={(e) => setInvoiceGstRate(e.target.value)}
|
|
placeholder="18.0"
|
|
className={inputClass}
|
|
/>
|
|
<span className="text-[13px] font-semibold text-muted-foreground">%</span>
|
|
</div>
|
|
<p className="text-[12px] text-muted-foreground mt-1">
|
|
Configures default GST split on generated tax invoices (e.g. 18% = CGST {(Number(invoiceGstRate) / 2 || 9)}% + SGST {(Number(invoiceGstRate) / 2 || 9)}%).
|
|
</p>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Store address & contact</label>
|
|
<input
|
|
type="text"
|
|
value={invoiceStoreAddress}
|
|
onChange={(e) => setInvoiceStoreAddress(e.target.value)}
|
|
placeholder="Offline Main Store Counter, Chennai | +91 9876543210"
|
|
className={inputClass}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<aside className="crm-radius-section border border-border bg-muted/20 p-4 min-w-0 overflow-hidden h-fit">
|
|
<p className="text-[13px] font-semibold text-foreground mb-3">Invoice preview</p>
|
|
<div className="crm-radius-section border border-border bg-card p-4 min-w-0 overflow-hidden">
|
|
<div className="flex items-center gap-2 pb-3 border-b border-border min-w-0">
|
|
{invoiceLogoUrl ? (
|
|
<img src={invoiceLogoUrl} alt="" className="h-8 w-8 object-contain shrink-0" />
|
|
) : (
|
|
<Building className="w-5 h-5 text-muted-foreground shrink-0" />
|
|
)}
|
|
<div className="min-w-0 overflow-hidden">
|
|
<p className="text-[13px] font-semibold text-foreground truncate">{invoiceCompanyName || 'Company name'}</p>
|
|
<p className="text-[12px] text-muted-foreground truncate">Tax invoice</p>
|
|
</div>
|
|
</div>
|
|
<dl className="mt-3 space-y-2 text-[13px] min-w-0">
|
|
<div className="flex flex-wrap items-start justify-between gap-3 min-w-0">
|
|
<dt className="text-muted-foreground shrink-0">GSTIN</dt>
|
|
<dd className="font-mono font-semibold text-foreground min-w-0 truncate">{invoiceGstin || '—'}</dd>
|
|
</div>
|
|
<div className="min-w-0">
|
|
<dt className="text-muted-foreground">Store</dt>
|
|
<dd className="font-medium text-foreground mt-0.5 leading-5 break-words">{invoiceStoreAddress || '—'}</dd>
|
|
</div>
|
|
</dl>
|
|
</div>
|
|
</aside>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</section>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|