New pages: - app/(admin)/warranties/ — full warranty management with claims - app/(admin)/refunds/ — refund management - app/(admin)/notification-templates/ — email/SMS template editor - app/(admin)/user-pos-analytics/ — per-user POS analytics New components: - components/quotations/ — quotation components - components/ui/PartNumbersModal.tsx — part numbers selection New services: - creditService.ts, deliverySettingsService.ts - notificationTemplateService.ts, roleNotificationSettingsService.ts Updated: - Sidebar navigation, dashboard, orders, POS sessions, products - Bulk quotations, CRM leads/quotations, notifications gateway - settings, service invoices, quotes, technician, users pages - adminService, billingFinanceService, catalogService, client - inventoryPosService, notificationGatewayService, openwaService, userService - Removed pos-sync page (merged into POS sessions)
241 lines
9.9 KiB
TypeScript
241 lines
9.9 KiB
TypeScript
'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<CrmLead[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
// Send Quote Modal State
|
|
const [quoteTarget, setQuoteTarget] = useState<CrmLead | null>(null);
|
|
const [quoteTitle, setQuoteTitle] = useState('');
|
|
const [quoteAmount, setQuoteAmount] = useState<number | string>(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<CrmLead | null>(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 (
|
|
<div className="p-6 space-y-6 max-w-7xl mx-auto">
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 border-b border-border pb-4">
|
|
<div>
|
|
<div className="flex items-center gap-2">
|
|
<span className="px-2.5 py-1 rounded-full text-xs font-bold bg-emerald-500/10 text-emerald-600 border border-emerald-500/20">
|
|
Stage 3 of 5
|
|
</span>
|
|
<h1 className="text-2xl font-bold text-foreground">3. Qualified Leads</h1>
|
|
</div>
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
Leads with verified requirements, confirmed details, and ready for quotation delivery
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="py-20 text-center text-muted-foreground animate-pulse text-sm">
|
|
Loading Qualified Leads...
|
|
</div>
|
|
) : leads.length === 0 ? (
|
|
<div className="py-16 text-center bg-card border border-border rounded-xl">
|
|
<Sparkles className="w-10 h-10 text-emerald-500 mx-auto mb-2 opacity-50" />
|
|
<h3 className="text-sm font-semibold text-foreground">No qualified leads currently in this stage</h3>
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
Leads advance here after staff confirm requirements with the customer.
|
|
</p>
|
|
</div>
|
|
) : (
|
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
|
{leads.map((lead) => (
|
|
<div key={lead.lead_id} className="bg-card border border-border hover:border-emerald-500/50 rounded-xl p-4 shadow-xs space-y-3 flex flex-col justify-between transition-all">
|
|
<div className="space-y-2">
|
|
<div className="flex items-start justify-between gap-2">
|
|
<h3 className="font-semibold text-sm text-foreground">
|
|
{lead.title || lead.first_name || 'Qualified Inquiry'}
|
|
</h3>
|
|
<span className="px-2 py-0.5 rounded-md text-[10px] font-bold bg-emerald-500/10 text-emerald-600">
|
|
QUALIFIED
|
|
</span>
|
|
</div>
|
|
|
|
<div className="space-y-1 text-xs">
|
|
{lead.phone && (
|
|
<div className="flex items-center gap-1.5 text-foreground font-medium">
|
|
<Phone className="w-3.5 h-3.5 text-muted-foreground" />
|
|
{formatPhoneDisplay(lead.phone)}
|
|
</div>
|
|
)}
|
|
{lead.email && (
|
|
<div className="flex items-center gap-1.5 text-muted-foreground truncate">
|
|
<Mail className="w-3.5 h-3.5 shrink-0" />
|
|
<span className="truncate">{lead.email}</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between text-[10px] text-muted-foreground pt-1">
|
|
<span className="bg-muted px-2 py-0.5 rounded-md font-semibold">
|
|
Source: {lead.source}
|
|
</span>
|
|
<span className="flex items-center gap-1">
|
|
<Clock className="w-3 h-3" />
|
|
{new Date(lead.created_at).toLocaleDateString()}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Multi-Option Quotation & Loss Actions */}
|
|
<div className="pt-3 border-t border-border space-y-2">
|
|
<div className="flex items-center gap-2">
|
|
<button
|
|
onClick={() => handleOpenQuotationBuilder(lead)}
|
|
className="flex-1 inline-flex items-center justify-center gap-1 text-xs font-semibold bg-emerald-600 text-white hover:bg-emerald-700 py-1.5 px-3 rounded-lg shadow-xs transition-colors"
|
|
>
|
|
<Send className="w-3.5 h-3.5" />
|
|
<span>Send Quote</span>
|
|
</button>
|
|
|
|
<button
|
|
onClick={() => handleOpenQuotationBuilder(lead)}
|
|
className="inline-flex items-center justify-center gap-1 text-xs font-semibold bg-muted text-foreground hover:bg-muted/80 py-1.5 px-2.5 rounded-lg border border-border transition-colors"
|
|
title="Draft in B2B Quotation Builder Module"
|
|
>
|
|
<FileText className="w-3.5 h-3.5 text-primary" />
|
|
<span>Draft</span>
|
|
</button>
|
|
|
|
<button
|
|
onClick={() => setLostTarget(lead)}
|
|
className="inline-flex items-center justify-center p-1.5 rounded-lg border border-rose-200 text-rose-600 hover:bg-rose-50 transition-colors"
|
|
title="Mark Lead as Lost"
|
|
>
|
|
<XCircle className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
{/* Modal: Mark Lead as Lost */}
|
|
{lostTarget && (
|
|
<div className="fixed inset-0 z-50 bg-black/50 backdrop-blur-xs flex items-center justify-center p-4">
|
|
<div className="bg-card border border-border rounded-xl shadow-xl max-w-md w-full p-6 space-y-4">
|
|
<div className="flex items-center justify-between border-b border-border pb-3">
|
|
<h3 className="text-lg font-bold text-rose-600 flex items-center gap-2">
|
|
<XCircle className="w-5 h-5 text-rose-600" />
|
|
Mark Lead as Lost
|
|
</h3>
|
|
<button onClick={() => setLostTarget(null)} className="text-muted-foreground hover:text-foreground">
|
|
<X className="w-5 h-5" />
|
|
</button>
|
|
</div>
|
|
|
|
<div className="space-y-3 text-xs">
|
|
<p className="text-muted-foreground">
|
|
Select reason for closing lead <strong>{lostTarget.title || lostTarget.phone}</strong>:
|
|
</p>
|
|
|
|
<div>
|
|
<label className="block font-medium mb-1">Reason for Loss</label>
|
|
<select
|
|
value={lossReason}
|
|
onChange={(e) => setLossReason(e.target.value)}
|
|
className="w-full bg-background border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
|
|
>
|
|
<option value="Spam / Not Genuine Inquiry">Spam / Not Genuine Inquiry</option>
|
|
<option value="Budget Mismatch">Budget Mismatch</option>
|
|
<option value="Competitor Chosen">Competitor Chosen</option>
|
|
<option value="Customer Unresponsive">Customer Unresponsive</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div className="pt-2 flex justify-end gap-2">
|
|
<button
|
|
type="button"
|
|
onClick={() => setLostTarget(null)}
|
|
className="px-4 py-2 rounded-lg border border-border font-medium hover:bg-muted"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isMarkingLost}
|
|
onClick={handleExecuteMarkLost}
|
|
className="px-4 py-2 rounded-lg bg-rose-600 text-white font-semibold hover:bg-rose-700"
|
|
>
|
|
{isMarkingLost ? 'Saving...' : 'Confirm Mark Lost'}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|