'use client'; import React, { useState, useEffect } from 'react'; import { Truck, ShieldCheck, Server, Lock, RefreshCw, CheckCircle2, AlertCircle, MapPin, Package, DollarSign, Key, Calendar, } from 'lucide-react'; import { toast } from 'sonner'; import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav'; import { integrationService } from '@/services/api/integrationService'; 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 (
{/* Top Banner */}

Shiprocket Logistics & Courier Partner Integration

Automated courier rate comparison, pickup dispatch, label printing, and real-time pincode serviceability calculator.

{creds.is_locked_by_config ? ( Configured via Config.py ) : ( Database Configuration )}
{/* Credentials Form (Cols 1-6) */}

Shiprocket Logistics Settings

setCreds({ ...creds, email: e.target.value })} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 focus:ring-2 focus:ring-purple-500 focus:outline-none" />
setCreds({ ...creds, pickup_location: e.target.value })} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 focus:ring-2 focus:ring-purple-500 focus:outline-none" />
setCreds({ ...creds, pickup_pin: e.target.value })} placeholder="e.g. 600001" className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 focus:ring-2 focus:ring-purple-500 focus:outline-none" />
{!creds.is_locked_by_config && ( )}
{/* Quick Login Authenticator */}

Refresh Authentication Token

setAuthEmail(e.target.value)} className="w-full px-3 py-1.5 text-xs rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white" /> setAuthPassword(e.target.value)} className="w-full px-3 py-1.5 text-xs rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white" />
{/* Real-time Courier Serviceability Checker (Cols 7-12) */}

Pincode Serviceability & Rate Calculator

Check real-time courier partner availability, estimated delivery days, and freight tariffs.

setPickupPin(e.target.value)} placeholder="e.g. 600001" className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-2 focus:ring-purple-500 focus:outline-none" />
setDeliveryPin(e.target.value)} placeholder="e.g. 560001" className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-2 focus:ring-purple-500 focus:outline-none" />
setWeight(parseFloat(e.target.value) || 0.5)} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-2 focus:ring-purple-500 focus:outline-none" />
Couriers: Bluedart, Delhivery, Ekart, DTDC, Shadowfax
{/* Available Couriers Table */} {serviceResults.length > 0 && (

Available Courier Partners ({serviceResults.length})

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