'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import { crmService, CrmLead } from '@/services/api/crmService'; import { notificationGatewayService } from '@/services/api/notificationGatewayService'; import { Phone, Mail, Sparkles, Star, X, MessageSquare, CheckCircle2, RotateCcw, FileText } from 'lucide-react'; import { toast } from 'sonner'; export default function QuoteSentLeadsPage() { const router = useRouter(); const [leads, setLeads] = useState([]); const [loading, setLoading] = useState(true); // Conversion Modal const [convertTarget, setConvertTarget] = useState(null); const [convertCategory, setConvertCategory] = useState<'ECOMMERCE' | 'SERVICE' | 'BOTH'>('BOTH'); const [dealTitle, setDealTitle] = useState(''); const [estimatedValue, setEstimatedValue] = useState(15000); const [isConverting, setIsConverting] = useState(false); useEffect(() => { loadLeads(); }, []); const loadLeads = async () => { try { setLoading(true); const data = await crmService.getLeads('QUOTE_SENT'); setLeads(data || []); } catch (err) { setLeads([]); } finally { setLoading(false); } }; const handleResend = async (lead: CrmLead, channel: 'WHATSAPP' | 'EMAIL') => { try { const entityId = lead.quote_title?.match(/SQ-202\d-\d{3}/)?.[0] || lead.lead_id; toast.loading(`Resending quotation via ${channel}...`, { id: 'resend' }); await notificationGatewayService.dispatchEntity({ channel: channel, notification_type: 'QUOTATION', entity_id: entityId, idempotency_key: `QUOTATION:${entityId}:${channel}:${Date.now()}` }); toast.success(`Quotation resent successfully via ${channel}!`, { id: 'resend' }); } catch (err) { toast.error(`Failed to resend quotation via ${channel}`, { id: 'resend' }); } }; const handleSimulateReject = async (lead: CrmLead) => { try { await crmService.updateLeadStatus( lead.lead_id, 'QUALIFIED', 'Customer rejected quotation offer. Reverted back to Stage 3 (Qualified).' ); toast.info('Quotation rejected by customer. Lead returned to Stage 3 (Qualified).'); loadLeads(); } catch (err) { toast.error('Failed to update lead status'); } }; const handleOpenConversionModal = (lead: CrmLead) => { setConvertTarget(lead); setDealTitle(lead.quote_title || lead.title || `Converted Deal - ${crmService.formatPhoneDisplay(lead.phone)}`); if (lead.quote_amount) { setEstimatedValue(Number(lead.quote_amount)); } }; const handleExecuteConversion = async () => { if (!convertTarget) return; try { setIsConverting(true); const res = await crmService.convertLead(convertTarget.lead_id, { category: convertCategory, deal_title: dealTitle, estimated_value: Number(estimatedValue), customer_phone: convertTarget.phone, customer_email: convertTarget.email || undefined, customer_name: convertTarget.title, notes: convertTarget.notes }); if (res.is_dual_customer) { toast.success(

⭐ Dual Customer Gold Star Awarded!

Customer exists in both E-Commerce & Repair Services!

, { duration: 5000 } ); } else { toast.success('Quote Accepted! Lead converted to active Deal & Customer profile!'); } setConvertTarget(null); router.push('/crm-leads/converted'); } catch (err) { toast.error('Failed to convert lead'); } finally { setIsConverting(false); } }; return (
Stage 4 of 5

4. Quote Sent Leads

Active quotations dispatched to customers awaiting acceptance or feedback

{loading ? (
Loading Quote Sent Leads...
) : leads.length === 0 ? (

No active quote sent leads

Leads move here when a formal quote or price estimate is delivered via WhatsApp/Email.

) : (
{leads.map((lead) => { const formattedPhone = crmService.formatPhoneDisplay(lead.phone); const amountDisplay = lead.quote_amount ? `₹${Number(lead.quote_amount).toLocaleString()}` : '₹12,500'; return (

{lead.title || 'Quote Sent Inquiry'}

{lead.quote_title && (

{lead.quote_title}

)}
{lead.status === 'QUOTE_DRAFT' ? 'QUOTE DRAFT' : 'QUOTE SENT'}
{/* Structured Quote Details Card */}
Quote Amount: {amountDisplay}
{lead.quote_terms && (

Terms: {lead.quote_terms}

)}
{lead.phone && (
{formattedPhone}
)} {lead.email && (
{lead.email}
)}
{/* Actions */}
); })}
)} {/* Modal: Lead Conversion */} {convertTarget && (

Convert Lead to Active Deal & Customer

Customer: {convertTarget.title}

Phone: {crmService.formatPhoneDisplay(convertTarget.phone)} | Email: {convertTarget.email || 'N/A'}

setDealTitle(e.target.value)} className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden" />
setEstimatedValue(Number(e.target.value))} className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden" />
)}
); }