'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import { crmService, CrmLead, formatPhoneDisplay, buildQuotationWhatsAppUrl } from '@/services/api/crmService'; import { openwaService } from '@/services/api/openwaService'; import { Phone, Mail, ArrowRight, Clock, Sparkles, FileText, XCircle, Send, X } from 'lucide-react'; import { toast } from 'sonner'; export default function QualifiedLeadsPage() { const router = useRouter(); const [leads, setLeads] = useState([]); const [loading, setLoading] = useState(true); // Send Quote Modal State const [quoteTarget, setQuoteTarget] = useState(null); const [quoteTitle, setQuoteTitle] = useState(''); const [quoteAmount, setQuoteAmount] = useState(12500); const [quoteTerms, setQuoteTerms] = useState('6 Months iFixKart Warranty + Free Screen Protection'); const [isSendingQuote, setIsSendingQuote] = useState(false); // Mark Lost Modal State const [lostTarget, setLostTarget] = useState(null); const [lossReason, setLossReason] = useState('Spam / Not Genuine Inquiry'); const [isMarkingLost, setIsMarkingLost] = useState(false); useEffect(() => { loadLeads(); }, []); const loadLeads = async () => { try { setLoading(true); const data = await crmService.getLeads('QUALIFIED'); setLeads(data || []); } catch (err) { setLeads([]); } finally { setLoading(false); } }; const handleOpenQuotationBuilder = (lead: CrmLead) => { try { const query = new URLSearchParams({ lead_id: lead.lead_id, customer_name: lead.title || lead.first_name || '', name: lead.title || lead.first_name || '', phone: lead.phone || '', email: lead.email || '' }).toString(); router.push(`/crm-quotations?${query}`); } catch (err) { toast.error('Failed to navigate to Quotation Module'); } }; const handleExecuteMarkLost = async () => { if (!lostTarget) return; try { setIsMarkingLost(true); await crmService.updateLeadStatus( lostTarget.lead_id, 'LOST', `Lead marked as LOST. Reason: ${lossReason}` ); toast.info(`Lead marked as LOST (${lossReason})`); setLostTarget(null); loadLeads(); } catch (err) { toast.error('Failed to mark lead as lost'); } finally { setIsMarkingLost(false); } }; return (
Stage 3 of 5

3. Qualified Leads

Leads with verified requirements, confirmed details, and ready for quotation delivery

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

No qualified leads currently in this stage

Leads advance here after staff confirm requirements with the customer.

) : (
{leads.map((lead) => (

{lead.title || lead.first_name || 'Qualified Inquiry'}

QUALIFIED
{lead.phone && (
{formatPhoneDisplay(lead.phone)}
)} {lead.email && (
{lead.email}
)}
Source: {lead.source} {new Date(lead.created_at).toLocaleDateString()}
{/* Multi-Option Quotation & Loss Actions */}
))}
)} {/* Modal: Mark Lead as Lost */} {lostTarget && (

Mark Lead as Lost

Select reason for closing lead {lostTarget.title || lostTarget.phone}:

)}
); }