'use client'; import React, { useState, useEffect } from 'react'; import { Truck, RefreshCw, CheckCircle2, MapPin, Key } from 'lucide-react'; import { toast } from 'sonner'; import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav'; import { integrationService } from '@/services/api/integrationService'; const fieldClass = 'w-full h-9 px-3 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary disabled:opacity-60'; const areaClass = 'w-full px-3 py-2 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary disabled:opacity-60'; 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 disabled:opacity-50'; 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'; export default function ShippingIntegrationsPage() { const [creds, setCreds] = useState({ email: '', pickup_location: 'Primary', pickup_pin: '', api_url: 'https://apiv2.shiprocket.in/v1/external', source: 'unconfigured', is_locked_by_config: false, }); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); // Auth test const [authEmail, setAuthEmail] = useState(''); const [authPassword, setAuthPassword] = useState(''); const [authing, setAuthing] = useState(false); // Serviceability check const [pickupPin, setPickupPin] = useState(''); const [deliveryPin, setDeliveryPin] = useState(''); const [weight, setWeight] = useState(0.5); const [isCod, setIsCod] = useState(false); const [checkingService, setCheckingService] = useState(false); const [serviceResults, setServiceResults] = useState([]); const fetchShippingConfig = async () => { setLoading(true); try { const data = await integrationService.getShiprocketCredentials(); setCreds(data); if (data.pickup_pin) setPickupPin(data.pickup_pin); if (data.email) setAuthEmail(data.email); } catch (err: any) { toast.error('Failed to load Shiprocket credentials'); } finally { setLoading(false); } }; useEffect(() => { fetchShippingConfig(); }, []); const handleSave = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); try { await integrationService.saveShiprocketCredentials(creds); toast.success('Shiprocket credentials saved successfully'); } catch (err: any) { toast.error(err.message || 'Failed to save Shiprocket credentials'); } finally { setSaving(false); } }; const handleAuthTest = async (e: React.FormEvent) => { e.preventDefault(); if (!authEmail.trim() || !authPassword.trim()) { toast.error('Email and password are required for Shiprocket login'); return; } setAuthing(true); try { const res = await integrationService.authShiprocket({ email: authEmail.trim(), password: authPassword.trim(), }); if (res.status === 'SUCCESS') { toast.success('Shiprocket Authentication Successful! Session token generated.'); } else { toast.error(`Auth failed: ${res.detail || JSON.stringify(res)}`); } } catch (err: any) { toast.error(err.message || 'Shiprocket authentication failed'); } finally { setAuthing(false); } }; const handleCheckServiceability = async (e: React.FormEvent) => { e.preventDefault(); if (!pickupPin.trim() || !deliveryPin.trim()) { toast.error('Both Pickup and Delivery Pincodes are required'); return; } setCheckingService(true); try { const res = await integrationService.checkShiprocketServiceability({ pickup_pin: pickupPin.trim(), delivery_pin: deliveryPin.trim(), weight, cod: isCod, }); if (res.status === 'SUCCESS') { const couriers = res.data?.data?.available_courier_companies || []; setServiceResults(couriers); toast.success(`Serviceability confirmed! ${couriers.length} couriers available.`); } else { toast.error(`Serviceability check failed: ${res.detail || 'Pincode not serviceable'}`); } } catch (err: any) { toast.error(err.message || 'Courier serviceability check failed'); } finally { setCheckingService(false); } }; return (

Shiprocket

{creds.is_locked_by_config ? 'Config' : 'Database'}
{/* Delhivery One Configuration Card */}
D1

Delhivery One Logistics

Trial Mode Active Auto-Pickup Simulated

Configured with client credentials & MCP gateway • Safe simulated fulfillment

Open Delhivery Hub →
Client ID ucp-service-cli
Client Realm ucp-Q72U870K1D05
Client CMS ID cms::client::e97e8488-2ebd...
Auth Endpoint https://ucp-auth.delhivery.com/holyknight
Origin Pickup PIN 560001 (Bengaluru Central Hub)
MCP Auto-Mint d1-mcp-mint@latest (Active)
{/* Credentials Form (Cols 1-6) */}

Shiprocket Logistics Settings

setCreds({ ...creds, email: e.target.value })} className={fieldClass} />
setCreds({ ...creds, pickup_location: e.target.value })} className={fieldClass} />
setCreds({ ...creds, pickup_pin: e.target.value })} placeholder="Pincode" className={fieldClass} />
{!creds.is_locked_by_config && ( )}
{/* Quick Login Authenticator */}

Refresh Authentication Token

setAuthEmail(e.target.value)} className={fieldClass} /> setAuthPassword(e.target.value)} className={fieldClass} />
{/* Real-time Courier Serviceability Checker (Cols 7-12) */}

Pincode Serviceability & Rate Calculator

setPickupPin(e.target.value)} placeholder="Pincode" className={fieldClass} />
setDeliveryPin(e.target.value)} placeholder="Pincode" className={fieldClass} />
setWeight(parseFloat(e.target.value) || 0.5)} className={fieldClass} />
{/* Available Couriers Table */} {serviceResults.length > 0 && (

Couriers ({serviceResults.length})

{serviceResults.map((c: any, idx: number) => ( ))}
Courier Name Estimated Delivery Rating Freight Charge COD Eligible
{c.courier_name || '—'} {c.estimated_delivery_days ? `${c.estimated_delivery_days} Days` : '—'} {c.rating ?? '—'} {c.rate || c.freight_charge ? `₹${c.rate || c.freight_charge}` : '—'} {c.cod === 1 || c.is_cod ? 'Yes' : 'Prepaid Only'}
)}
); }