452 lines
19 KiB
TypeScript
452 lines
19 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import {
|
|
Navigation,
|
|
Wallet,
|
|
Package,
|
|
Clock,
|
|
RefreshCw,
|
|
MapPin,
|
|
Loader2,
|
|
Printer,
|
|
ExternalLink,
|
|
ShieldAlert,
|
|
ArrowRight,
|
|
Sparkles,
|
|
} from 'lucide-react';
|
|
import { apiFetch } from '@/services/api/client';
|
|
import { API_ENDPOINTS } from '@/services/api/config';
|
|
import { toast } from 'sonner';
|
|
import { format } from 'date-fns';
|
|
import { LogisticsHeaderNav } from '@/components/logistics/LogisticsHeaderNav';
|
|
|
|
interface DelhiveryDashboard {
|
|
status: string;
|
|
is_enabled: boolean;
|
|
mode: string;
|
|
client_cms: string;
|
|
pickup_location: string;
|
|
pickup_pin: string;
|
|
wallet: {
|
|
balance_inr: number;
|
|
currency: string;
|
|
is_trial_credit: boolean;
|
|
status: string;
|
|
};
|
|
shipment_counts: {
|
|
ready_to_ship: number;
|
|
in_transit: number;
|
|
out_for_delivery: number;
|
|
delivered: number;
|
|
rto_returned: number;
|
|
};
|
|
couriers: Array<{
|
|
id: number;
|
|
name: string;
|
|
mode: string;
|
|
min_weight_kg: number;
|
|
estimated_days: number;
|
|
rate_inr: number;
|
|
rating: number;
|
|
cod_supported: boolean;
|
|
}>;
|
|
recent_dispatches: Array<{
|
|
order_id: string;
|
|
order_no: string;
|
|
awb_code: string;
|
|
courier: string;
|
|
destination_city: string;
|
|
destination_pin: string;
|
|
status: string;
|
|
fulfillment_status: string;
|
|
package_weight: number;
|
|
dispatch_date: string;
|
|
label_url: string;
|
|
tracking_url: string;
|
|
}>;
|
|
}
|
|
|
|
function inr(amount: number) {
|
|
return `₹${amount.toLocaleString('en-IN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
|
|
}
|
|
|
|
function dateLabel(value?: string) {
|
|
if (!value) return '—';
|
|
const date = new Date(value);
|
|
if (Number.isNaN(date.getTime())) return '—';
|
|
return format(date, 'dd-MMM-yyyy, hh:mm a');
|
|
}
|
|
|
|
export default function DelhiveryLogisticsPage() {
|
|
const [data, setData] = useState<DelhiveryDashboard | null>(null);
|
|
const [loading, setLoading] = useState<boolean>(true);
|
|
|
|
// Rate calculator state
|
|
const [pickupPin, setPickupPin] = useState<string>('560001');
|
|
const [deliveryPin, setDeliveryPin] = useState<string>('');
|
|
const [weightKg, setWeightKg] = useState<number>(0.5);
|
|
const [rateLoading, setRateLoading] = useState<boolean>(false);
|
|
const [calculatedRates, setCalculatedRates] = useState<DelhiveryDashboard['couriers'] | null>(null);
|
|
|
|
const fetchData = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const res = await apiFetch<DelhiveryDashboard>(API_ENDPOINTS.DELHIVERY_DASHBOARD);
|
|
if (res) {
|
|
setData(res);
|
|
setCalculatedRates(res.couriers || []);
|
|
}
|
|
} catch (err: unknown) {
|
|
console.error('Failed to load Delhivery dashboard:', err);
|
|
toast.error('Failed to load Delhivery One logistics information');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchData();
|
|
}, []);
|
|
|
|
const handleCalculateRate = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!deliveryPin || deliveryPin.trim().length !== 6) {
|
|
toast.error('Please enter a valid 6-digit delivery pincode');
|
|
return;
|
|
}
|
|
try {
|
|
setRateLoading(true);
|
|
const res = await apiFetch<{
|
|
couriers: DelhiveryDashboard['couriers'];
|
|
}>(`${API_ENDPOINTS.DELHIVERY_RATES}?delivery_pincode=${deliveryPin}&pickup_pincode=${pickupPin}&weight_kg=${weightKg}`);
|
|
if (res?.couriers) {
|
|
setCalculatedRates(res.couriers);
|
|
toast.success(`Rates calculated for pincode ${deliveryPin}`);
|
|
}
|
|
} catch (err: unknown) {
|
|
toast.error('Failed to calculate shipping rates');
|
|
} finally {
|
|
setRateLoading(false);
|
|
}
|
|
};
|
|
|
|
const dataCardShell =
|
|
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0';
|
|
const fieldClass =
|
|
'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';
|
|
const labelClass = 'block text-[11px] font-semibold text-muted-foreground uppercase mb-1.5';
|
|
const primaryButton =
|
|
'inline-flex items-center justify-center gap-2 h-9 px-4 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-2 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';
|
|
|
|
const couriers = calculatedRates || data?.couriers || [];
|
|
const dispatches = data?.recent_dispatches || [];
|
|
const counts = data?.shipment_counts || {
|
|
ready_to_ship: 0,
|
|
in_transit: 0,
|
|
out_for_delivery: 0,
|
|
delivered: 0,
|
|
rto_returned: 0,
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-5 min-w-0">
|
|
<LogisticsHeaderNav />
|
|
|
|
{/* Page Header */}
|
|
<div className="flex flex-wrap items-center justify-between gap-3 min-w-0">
|
|
<div className="flex items-center gap-2.5 min-w-0">
|
|
<div className="w-9 h-9 rounded-lg bg-red-500/10 text-red-600 flex items-center justify-center shrink-0 font-black">
|
|
D1
|
|
</div>
|
|
<div>
|
|
<div className="flex items-center gap-2">
|
|
<h1 className="text-lg font-bold text-foreground">Delhivery One Logistics</h1>
|
|
<span className="inline-flex items-center px-2 py-0.5 crm-radius-badge bg-amber-500/15 text-amber-600 border border-amber-500/30 text-[11px] font-bold">
|
|
Trial Mode
|
|
</span>
|
|
<span className="inline-flex items-center px-2 py-0.5 crm-radius-badge bg-emerald-500/15 text-emerald-600 border border-emerald-500/30 text-[11px] font-semibold">
|
|
Auto-Pickup Simulated
|
|
</span>
|
|
</div>
|
|
<p className="text-[12px] text-muted-foreground">
|
|
Automated Express Fulfillment • CMS Client:{' '}
|
|
<code className="text-foreground">{data?.client_cms?.slice(-12) || 'e97e8488-2ebd'}</code>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<button type="button" onClick={fetchData} disabled={loading} className={secondaryButton}>
|
|
<RefreshCw className={`w-3.5 h-3.5 ${loading ? 'animate-spin' : ''}`} />
|
|
Refresh
|
|
</button>
|
|
</div>
|
|
|
|
{/* Trial Mode Alert Banner */}
|
|
<div className="p-3.5 rounded-lg border border-amber-500/30 bg-amber-500/10 flex items-start gap-3">
|
|
<ShieldAlert className="w-5 h-5 text-amber-600 shrink-0 mt-0.5" />
|
|
<div className="text-[12.5px] text-foreground leading-relaxed">
|
|
<strong className="text-amber-700 dark:text-amber-400">Delhivery One is currently running in Trial Mode.</strong>{' '}
|
|
When orders are marked "Ready for delivery" with Delhivery One selected, real AWBs and printable thermal shipping slips are generated immediately. Live billing charges and physical carrier dispatches are suppressed until live API tokens are activated.
|
|
</div>
|
|
</div>
|
|
|
|
{/* Metric Cards */}
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
|
|
{/* Trial Wallet */}
|
|
<div className={`${dataCardShell} p-4`}>
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div className="min-w-0">
|
|
<p className="text-[13px] text-muted-foreground font-medium">Trial Wallet Balance</p>
|
|
<p className="text-[22px] font-bold text-foreground mt-1.5 tracking-tight truncate">
|
|
{loading ? '—' : inr(data?.wallet.balance_inr ?? 5000)}
|
|
</p>
|
|
<span className="text-[11px] font-medium text-amber-600 mt-1 inline-block">
|
|
Trial Credits Active
|
|
</span>
|
|
</div>
|
|
<span className="w-9 h-9 rounded-full bg-amber-500/10 text-amber-600 flex items-center justify-center shrink-0">
|
|
<Wallet className="w-4 h-4" />
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Ready to Ship */}
|
|
<div className={`${dataCardShell} p-4`}>
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div className="min-w-0">
|
|
<p className="text-[13px] text-muted-foreground font-medium">Ready to Dispatch</p>
|
|
<p className="text-[22px] font-bold text-foreground mt-1.5 tracking-tight truncate">
|
|
{loading ? '—' : counts.ready_to_ship}
|
|
</p>
|
|
<span className="text-[11px] text-muted-foreground mt-1 inline-block">
|
|
Manifested & Label Ready
|
|
</span>
|
|
</div>
|
|
<span className="w-9 h-9 rounded-full bg-blue-500/10 text-blue-600 flex items-center justify-center shrink-0">
|
|
<Package className="w-4 h-4" />
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* In Transit */}
|
|
<div className={`${dataCardShell} p-4`}>
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div className="min-w-0">
|
|
<p className="text-[13px] text-muted-foreground font-medium">In Transit / Out for Delivery</p>
|
|
<p className="text-[22px] font-bold text-foreground mt-1.5 tracking-tight truncate">
|
|
{loading ? '—' : counts.in_transit + counts.out_for_delivery}
|
|
</p>
|
|
<span className="text-[11px] text-muted-foreground mt-1 inline-block">
|
|
On road to consignee
|
|
</span>
|
|
</div>
|
|
<span className="w-9 h-9 rounded-full bg-purple-500/10 text-purple-600 flex items-center justify-center shrink-0">
|
|
<Navigation className="w-4 h-4" />
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Total Delivered */}
|
|
<div className={`${dataCardShell} p-4`}>
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div className="min-w-0">
|
|
<p className="text-[13px] text-muted-foreground font-medium">Delivered Parcels</p>
|
|
<p className="text-[22px] font-bold text-foreground mt-1.5 tracking-tight truncate text-emerald-600">
|
|
{loading ? '—' : counts.delivered}
|
|
</p>
|
|
<span className="text-[11px] text-muted-foreground mt-1 inline-block">
|
|
Successful handovers
|
|
</span>
|
|
</div>
|
|
<span className="w-9 h-9 rounded-full bg-emerald-500/10 text-emerald-600 flex items-center justify-center shrink-0">
|
|
<Clock className="w-4 h-4" />
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Main Grid: Rate Calculator & Available Services */}
|
|
<div className="grid grid-cols-1 lg:grid-cols-3 gap-5">
|
|
{/* Rate Calculator */}
|
|
<div className={`${dataCardShell} p-5 lg:col-span-1`}>
|
|
<div className="flex items-center gap-2 mb-4">
|
|
<MapPin className="w-4 h-4 text-primary" />
|
|
<h2 className="text-[15px] font-bold text-foreground">Delhivery Rate & TAT Calculator</h2>
|
|
</div>
|
|
<form onSubmit={handleCalculateRate} className="space-y-3.5">
|
|
<div>
|
|
<label className={labelClass}>Origin Pincode (Hub)</label>
|
|
<input
|
|
type="text"
|
|
value={pickupPin}
|
|
onChange={(e) => setPickupPin(e.target.value)}
|
|
maxLength={6}
|
|
className={fieldClass}
|
|
required
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Destination Pincode</label>
|
|
<input
|
|
type="text"
|
|
placeholder="e.g. 560034"
|
|
value={deliveryPin}
|
|
onChange={(e) => setDeliveryPin(e.target.value)}
|
|
maxLength={6}
|
|
className={fieldClass}
|
|
required
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>Dead Weight (KG)</label>
|
|
<input
|
|
type="number"
|
|
step="0.05"
|
|
min="0.05"
|
|
max="50"
|
|
value={weightKg}
|
|
onChange={(e) => setWeightKg(parseFloat(e.target.value) || 0.5)}
|
|
className={fieldClass}
|
|
required
|
|
/>
|
|
</div>
|
|
<button
|
|
type="submit"
|
|
disabled={rateLoading}
|
|
className={`${primaryButton} w-full mt-2`}
|
|
>
|
|
{rateLoading ? <Loader2 className="w-4 h-4 animate-spin" /> : <Sparkles className="w-4 h-4" />}
|
|
Calculate Freight Rates
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
{/* Courier Services Table */}
|
|
<div className={`${dataCardShell} p-5 lg:col-span-2 overflow-hidden flex flex-col`}>
|
|
<div className="flex items-center justify-between gap-2 mb-4">
|
|
<div>
|
|
<h2 className="text-[15px] font-bold text-foreground">Available Delhivery Service Lines</h2>
|
|
<p className="text-[12px] text-muted-foreground">Estimated transit time and shipping charges for {weightKg} kg</p>
|
|
</div>
|
|
</div>
|
|
<div className="overflow-x-auto -mx-5 -mb-5">
|
|
<table className="w-full text-left text-[12.5px]">
|
|
<thead className="bg-muted/50 border-b border-border text-[11px] font-semibold text-muted-foreground uppercase">
|
|
<tr>
|
|
<th className="px-5 py-3">Service Tier</th>
|
|
<th className="px-3 py-3">Mode</th>
|
|
<th className="px-3 py-3">Est. Transit</th>
|
|
<th className="px-3 py-3">Rate (INR)</th>
|
|
<th className="px-3 py-3">COD</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-border">
|
|
{couriers.map((c) => (
|
|
<tr key={c.id} className="hover:bg-muted/20">
|
|
<td className="px-5 py-3.5 font-semibold text-foreground flex items-center gap-2">
|
|
<div className="w-2 h-2 rounded-full bg-red-500 shrink-0" />
|
|
{c.name}
|
|
</td>
|
|
<td className="px-3 py-3.5 text-muted-foreground">{c.mode}</td>
|
|
<td className="px-3 py-3.5 font-medium text-foreground">
|
|
{c.estimated_days} {c.estimated_days === 1 ? 'Day' : 'Days'}
|
|
</td>
|
|
<td className="px-3 py-3.5 font-bold text-foreground">{inr(c.rate_inr)}</td>
|
|
<td className="px-3 py-3.5">
|
|
{c.cod_supported ? (
|
|
<span className="text-emerald-600 font-semibold text-[11px]">Supported</span>
|
|
) : (
|
|
<span className="text-muted-foreground text-[11px]">Prepaid only</span>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Recent Delhivery Dispatches */}
|
|
<div className={`${dataCardShell} p-5`}>
|
|
<div className="flex items-center justify-between gap-3 mb-4">
|
|
<div>
|
|
<h2 className="text-[15px] font-bold text-foreground">Recent Delhivery Shipments</h2>
|
|
<p className="text-[12px] text-muted-foreground">Orders dispatched via Delhivery One automated fulfillment</p>
|
|
</div>
|
|
</div>
|
|
|
|
{dispatches.length === 0 ? (
|
|
<div className="text-center py-12 text-muted-foreground text-[13px] border border-dashed border-border rounded-lg">
|
|
<Package className="w-8 h-8 mx-auto mb-2 text-muted-foreground/50" />
|
|
<p className="font-medium text-foreground">No Delhivery shipments dispatched yet</p>
|
|
<p className="text-[12px] mt-1">
|
|
When an order is updated to "Ready for delivery" with Delhivery One selected, it will appear here with live AWB tracking and thermal slips.
|
|
</p>
|
|
</div>
|
|
) : (
|
|
<div className="overflow-x-auto -mx-5 -mb-5">
|
|
<table className="w-full text-left text-[12.5px]">
|
|
<thead className="bg-muted/50 border-b border-border text-[11px] font-semibold text-muted-foreground uppercase">
|
|
<tr>
|
|
<th className="px-5 py-3">Order No</th>
|
|
<th className="px-3 py-3">AWB Code</th>
|
|
<th className="px-3 py-3">Courier Mode</th>
|
|
<th className="px-3 py-3">Destination</th>
|
|
<th className="px-3 py-3">Status</th>
|
|
<th className="px-3 py-3">Date</th>
|
|
<th className="px-5 py-3 text-right">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-border">
|
|
{dispatches.map((d) => (
|
|
<tr key={d.order_id} className="hover:bg-muted/20">
|
|
<td className="px-5 py-3.5 font-bold text-foreground">{d.order_no}</td>
|
|
<td className="px-3 py-3.5 font-mono text-[12px] text-foreground font-semibold">
|
|
{d.awb_code}
|
|
</td>
|
|
<td className="px-3 py-3.5 text-muted-foreground">{d.courier}</td>
|
|
<td className="px-3 py-3.5 text-foreground">
|
|
{d.destination_city} ({d.destination_pin})
|
|
</td>
|
|
<td className="px-3 py-3.5">
|
|
<span className="inline-flex items-center px-2 py-0.5 crm-radius-badge bg-blue-500/15 text-blue-600 font-semibold text-[11px]">
|
|
{d.status}
|
|
</span>
|
|
</td>
|
|
<td className="px-3 py-3.5 text-muted-foreground text-[11.5px]">
|
|
{dateLabel(d.dispatch_date)}
|
|
</td>
|
|
<td className="px-5 py-3.5 text-right space-x-2">
|
|
<a
|
|
href={d.label_url}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="inline-flex items-center gap-1 text-[12px] font-semibold text-primary hover:underline"
|
|
>
|
|
<Printer className="w-3.5 h-3.5" />
|
|
Label
|
|
</a>
|
|
<a
|
|
href={d.tracking_url}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="inline-flex items-center gap-1 text-[12px] font-semibold text-muted-foreground hover:text-foreground hover:underline ml-2"
|
|
>
|
|
<ExternalLink className="w-3.5 h-3.5" />
|
|
Track
|
|
</a>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|