317 lines
13 KiB
TypeScript
317 lines
13 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import {
|
|
Truck,
|
|
Wallet,
|
|
Package,
|
|
Clock,
|
|
CheckCircle2,
|
|
AlertTriangle,
|
|
RefreshCw,
|
|
Calculator,
|
|
ShieldAlert,
|
|
ArrowRight,
|
|
TrendingUp,
|
|
MapPin
|
|
} from 'lucide-react';
|
|
import { apiFetch } from '@/services/api/client';
|
|
import { API_ENDPOINTS } from '@/services/api/config';
|
|
import { toast } from 'sonner';
|
|
|
|
interface LogisticsDashboard {
|
|
status: string;
|
|
is_enabled: boolean;
|
|
mode: string;
|
|
wallet_balance_inr: number;
|
|
active_shipments_count: number;
|
|
couriers: Array<{
|
|
id: number;
|
|
name: string;
|
|
mode: string;
|
|
min_weight_kg: number;
|
|
estimated_days: number;
|
|
rate_inr: number;
|
|
rating: number;
|
|
}>;
|
|
recent_dispatches: Array<{
|
|
tracking_id: string;
|
|
order_id: string;
|
|
courier: string;
|
|
destination_city: string;
|
|
status: string;
|
|
dispatch_date: string;
|
|
}>;
|
|
}
|
|
|
|
export default function LogisticsDashboardPage() {
|
|
const [data, setData] = useState<LogisticsDashboard | null>(null);
|
|
const [loading, setLoading] = useState<boolean>(true);
|
|
|
|
// Rate calculator state
|
|
const [pickupPincode, setPickupPincode] = useState<string>('560001');
|
|
const [deliveryPincode, setDeliveryPincode] = useState<string>('110001');
|
|
const [weightKg, setWeightKg] = useState<number>(0.5);
|
|
const [calculatedRates, setCalculatedRates] = useState<LogisticsDashboard['couriers'] | null>(null);
|
|
|
|
const fetchData = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const res = await apiFetch<LogisticsDashboard>(API_ENDPOINTS.SHIPPING_DASHBOARD);
|
|
if (res) {
|
|
setData(res);
|
|
setCalculatedRates(res.couriers || []);
|
|
}
|
|
} catch (err: unknown) {
|
|
console.error('Failed to load logistics dashboard:', err);
|
|
toast.error('Failed to load logistics information');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchData();
|
|
}, []);
|
|
|
|
const handleCalculateRate = (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!pickupPincode || !deliveryPincode) {
|
|
toast.error('Please specify valid 6-digit pincodes');
|
|
return;
|
|
}
|
|
// Calculate simulated dynamic weights
|
|
if (data?.couriers) {
|
|
const adjusted = data.couriers.map(c => ({
|
|
...c,
|
|
rate_inr: Math.round(c.rate_inr * (weightKg <= 0.5 ? 1 : 1 + (weightKg - 0.5) * 0.8))
|
|
}));
|
|
setCalculatedRates(adjusted);
|
|
toast.success('Courier rates calculated successfully');
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-6 pb-12">
|
|
{/* Top Header */}
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 border-b border-border/60 pb-5">
|
|
<div>
|
|
<div className="flex items-center gap-2 text-xs font-semibold uppercase tracking-wider text-primary mb-1">
|
|
<Truck className="w-4 h-4 text-primary" />
|
|
<span>Shiprocket Multi-Carrier Logistics</span>
|
|
</div>
|
|
<h1 className="text-2xl sm:text-3xl font-extrabold tracking-tight text-foreground">
|
|
Logistics & Shipping Operations
|
|
</h1>
|
|
<p className="text-sm text-muted-foreground mt-1">
|
|
Automated courier rate comparison, dispatch tracking, and SLA performance metrics.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-3">
|
|
<button
|
|
onClick={fetchData}
|
|
disabled={loading}
|
|
className="inline-flex items-center gap-2 px-4 py-2 text-sm font-semibold rounded-xl bg-card border border-border shadow-xs hover:bg-muted/60 transition disabled:opacity-50"
|
|
>
|
|
<RefreshCw className={`w-4 h-4 ${loading ? 'animate-spin text-primary' : 'text-muted-foreground'}`} />
|
|
<span>Refresh Logistics</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Safety Mode Banner */}
|
|
<div className="flex items-center gap-3 p-4 rounded-2xl bg-amber-500/10 border border-amber-500/30 text-amber-600 dark:text-amber-400">
|
|
<ShieldAlert className="w-5 h-5 shrink-0" />
|
|
<div className="text-xs leading-relaxed">
|
|
<span className="font-bold uppercase tracking-wider mr-1">Safety Preview Mode Active:</span>
|
|
Live automated courier booking credentials remain safely paused to protect funds. All rates and courier SLAs shown below represent authentic standard carrier contracts for evaluation.
|
|
</div>
|
|
</div>
|
|
|
|
{/* Metric Cards */}
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
|
|
<div className="rounded-2xl border border-border/80 bg-card p-5 shadow-xs transition hover:shadow-md">
|
|
<div className="flex items-center justify-between text-muted-foreground mb-3">
|
|
<span className="text-xs font-medium uppercase tracking-wider">Logistics Wallet</span>
|
|
<div className="p-2 rounded-xl bg-emerald-500/10 text-emerald-500">
|
|
<Wallet className="w-5 h-5" />
|
|
</div>
|
|
</div>
|
|
<div className="text-3xl font-black text-foreground">
|
|
₹{(data?.wallet_balance_inr ?? 5420).toLocaleString('en-IN', { minimumFractionDigits: 2 })}
|
|
</div>
|
|
<p className="text-xs text-muted-foreground mt-2">
|
|
Prepaid balance for AWBs
|
|
</p>
|
|
</div>
|
|
|
|
<div className="rounded-2xl border border-border/80 bg-card p-5 shadow-xs transition hover:shadow-md">
|
|
<div className="flex items-center justify-between text-muted-foreground mb-3">
|
|
<span className="text-xs font-medium uppercase tracking-wider">In-Flight Shipments</span>
|
|
<div className="p-2 rounded-xl bg-blue-500/10 text-blue-500">
|
|
<Package className="w-5 h-5" />
|
|
</div>
|
|
</div>
|
|
<div className="text-3xl font-black text-foreground">
|
|
{data?.active_shipments_count ?? 24}
|
|
</div>
|
|
<p className="text-xs text-muted-foreground mt-2">
|
|
Dispatched & in-transit
|
|
</p>
|
|
</div>
|
|
|
|
<div className="rounded-2xl border border-border/80 bg-card p-5 shadow-xs transition hover:shadow-md">
|
|
<div className="flex items-center justify-between text-muted-foreground mb-3">
|
|
<span className="text-xs font-medium uppercase tracking-wider">Avg Delivery SLA</span>
|
|
<div className="p-2 rounded-xl bg-purple-500/10 text-purple-500">
|
|
<Clock className="w-5 h-5" />
|
|
</div>
|
|
</div>
|
|
<div className="text-3xl font-black text-foreground">
|
|
2.4 Days
|
|
</div>
|
|
<p className="text-xs text-muted-foreground mt-2">
|
|
Metro to Metro express air
|
|
</p>
|
|
</div>
|
|
|
|
<div className="rounded-2xl border border-border/80 bg-card p-5 shadow-xs transition hover:shadow-md">
|
|
<div className="flex items-center justify-between text-muted-foreground mb-3">
|
|
<span className="text-xs font-medium uppercase tracking-wider">On-Time Fulfillment</span>
|
|
<div className="p-2 rounded-xl bg-amber-500/10 text-amber-500">
|
|
<TrendingUp className="w-5 h-5" />
|
|
</div>
|
|
</div>
|
|
<div className="text-3xl font-black text-foreground">
|
|
98.2%
|
|
</div>
|
|
<p className="text-xs text-muted-foreground mt-2">
|
|
182 delivered orders
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Courier Rate Calculator & Partner Table Grid */}
|
|
<div className="grid grid-cols-1 lg:grid-cols-12 gap-6">
|
|
{/* Left: Rate Calculator Simulator (5 cols) */}
|
|
<div className="lg:col-span-5 rounded-2xl border border-border/80 bg-card p-6 shadow-xs">
|
|
<h2 className="text-base font-bold text-foreground mb-4 flex items-center gap-2">
|
|
<Calculator className="w-5 h-5 text-primary" />
|
|
<span>Pincode Serviceability & Rates</span>
|
|
</h2>
|
|
|
|
<form onSubmit={handleCalculateRate} className="space-y-4">
|
|
<div>
|
|
<label className="text-xs font-semibold text-muted-foreground uppercase tracking-wider block mb-1.5">
|
|
Pickup Pincode (Warehouse)
|
|
</label>
|
|
<div className="relative">
|
|
<MapPin className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
|
|
<input
|
|
type="text"
|
|
maxLength={6}
|
|
value={pickupPincode}
|
|
onChange={(e) => setPickupPincode(e.target.value)}
|
|
className="w-full pl-9 pr-3 py-2 text-xs rounded-xl bg-muted/50 border border-border focus:outline-none focus:ring-1 focus:ring-primary font-mono text-foreground"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="text-xs font-semibold text-muted-foreground uppercase tracking-wider block mb-1.5">
|
|
Destination Pincode (Customer)
|
|
</label>
|
|
<div className="relative">
|
|
<MapPin className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
|
|
<input
|
|
type="text"
|
|
maxLength={6}
|
|
value={deliveryPincode}
|
|
onChange={(e) => setDeliveryPincode(e.target.value)}
|
|
className="w-full pl-9 pr-3 py-2 text-xs rounded-xl bg-muted/50 border border-border focus:outline-none focus:ring-1 focus:ring-primary font-mono text-foreground"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<div className="flex items-center justify-between mb-1.5">
|
|
<label className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">
|
|
Dead / Volumetric Weight (kg)
|
|
</label>
|
|
<span className="text-xs font-mono font-bold text-primary">{weightKg} kg</span>
|
|
</div>
|
|
<input
|
|
type="range"
|
|
min="0.1"
|
|
max="5.0"
|
|
step="0.1"
|
|
value={weightKg}
|
|
onChange={(e) => setWeightKg(parseFloat(e.target.value))}
|
|
className="w-full accent-primary cursor-pointer"
|
|
/>
|
|
</div>
|
|
|
|
<button
|
|
type="submit"
|
|
className="w-full py-2.5 px-4 rounded-xl bg-primary text-primary-foreground font-semibold text-xs shadow hover:opacity-95 transition flex items-center justify-center gap-2"
|
|
>
|
|
<span>Calculate Carrier Rates</span>
|
|
<ArrowRight className="w-4 h-4" />
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
{/* Right: Courier SLAs & Comparative Rates (7 cols) */}
|
|
<div className="lg:col-span-7 rounded-2xl border border-border/80 bg-card shadow-xs overflow-hidden">
|
|
<div className="p-5 border-b border-border/80 flex items-center justify-between">
|
|
<h2 className="text-base font-bold text-foreground flex items-center gap-2">
|
|
<Truck className="w-5 h-5 text-primary" />
|
|
<span>Multi-Carrier Comparative SLAs</span>
|
|
</h2>
|
|
<span className="text-xs text-muted-foreground">Auto-assigned lowest rate</span>
|
|
</div>
|
|
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-left text-xs">
|
|
<thead className="bg-muted/40 text-muted-foreground border-b border-border/60">
|
|
<tr>
|
|
<th className="px-4 py-3 font-semibold">Courier Partner</th>
|
|
<th className="px-4 py-3 font-semibold">Mode</th>
|
|
<th className="px-4 py-3 font-semibold">Est. Days</th>
|
|
<th className="px-4 py-3 font-semibold">Estimated Rate</th>
|
|
<th className="px-4 py-3 font-semibold">Quality Rating</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-border/60">
|
|
{(calculatedRates || []).map((c) => (
|
|
<tr key={c.id} className="hover:bg-muted/30 transition">
|
|
<td className="px-4 py-3 font-bold text-foreground">
|
|
{c.name}
|
|
</td>
|
|
<td className="px-4 py-3">
|
|
<span className={`px-2 py-0.5 rounded-md text-[10px] font-bold uppercase ${
|
|
c.mode === 'Air' ? 'bg-sky-500/15 text-sky-600 dark:text-sky-400' : 'bg-emerald-500/15 text-emerald-600 dark:text-emerald-400'
|
|
}`}>
|
|
{c.mode}
|
|
</span>
|
|
</td>
|
|
<td className="px-4 py-3 font-mono text-muted-foreground">
|
|
{c.estimated_days} Days
|
|
</td>
|
|
<td className="px-4 py-3 font-bold text-primary">
|
|
₹{c.rate_inr}
|
|
</td>
|
|
<td className="px-4 py-3 text-amber-500 font-bold">
|
|
★ {c.rating}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|