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)
358 lines
16 KiB
TypeScript
358 lines
16 KiB
TypeScript
'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<CrmLead[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
// Conversion Modal
|
|
const [convertTarget, setConvertTarget] = useState<CrmLead | null>(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(
|
|
<div className="flex items-center gap-2">
|
|
<Star className="w-5 h-5 text-amber-500 fill-amber-500 animate-bounce" />
|
|
<div>
|
|
<p className="font-bold">⭐ Dual Customer Gold Star Awarded!</p>
|
|
<p className="text-xs">Customer exists in both E-Commerce & Repair Services!</p>
|
|
</div>
|
|
</div>,
|
|
{ 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 (
|
|
<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-purple-500/10 text-purple-600 border border-purple-500/20">
|
|
Stage 4 of 5
|
|
</span>
|
|
<h1 className="text-2xl font-bold text-foreground">4. Quote Sent Leads</h1>
|
|
</div>
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
Active quotations dispatched to customers awaiting acceptance or feedback
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="py-20 text-center text-muted-foreground animate-pulse text-sm">
|
|
Loading Quote Sent 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-purple-500 mx-auto mb-2 opacity-50" />
|
|
<h3 className="text-sm font-semibold text-foreground">No active quote sent leads</h3>
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
Leads move here when a formal quote or price estimate is delivered via WhatsApp/Email.
|
|
</p>
|
|
</div>
|
|
) : (
|
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
|
{leads.map((lead) => {
|
|
const formattedPhone = crmService.formatPhoneDisplay(lead.phone);
|
|
const amountDisplay = lead.quote_amount ? `₹${Number(lead.quote_amount).toLocaleString()}` : '₹12,500';
|
|
|
|
return (
|
|
<div key={lead.lead_id} className="bg-card border border-border hover:border-purple-500/50 rounded-xl p-4 shadow-xs space-y-3 flex flex-col justify-between transition-all">
|
|
<div className="space-y-2.5">
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div>
|
|
<h3 className="font-semibold text-sm text-foreground">
|
|
{lead.title || 'Quote Sent Inquiry'}
|
|
</h3>
|
|
{lead.quote_title && (
|
|
<p className="text-xs text-muted-foreground font-medium line-clamp-1">{lead.quote_title}</p>
|
|
)}
|
|
</div>
|
|
<span className={`px-2.5 py-1 rounded-md text-[10px] font-extrabold shrink-0 ${
|
|
lead.status === 'QUOTE_DRAFT'
|
|
? 'bg-amber-500/10 text-amber-600 border border-amber-500/20'
|
|
: 'bg-purple-500/10 text-purple-600 border border-purple-500/20'
|
|
}`}>
|
|
{lead.status === 'QUOTE_DRAFT' ? 'QUOTE DRAFT' : 'QUOTE SENT'}
|
|
</span>
|
|
</div>
|
|
|
|
{/* Structured Quote Details Card */}
|
|
<div className="p-3 bg-purple-500/5 border border-purple-500/15 rounded-lg space-y-1.5 text-xs">
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-[11px] text-muted-foreground font-medium flex items-center gap-1">
|
|
<FileText className="w-3.5 h-3.5 text-purple-500" /> Quote Amount:
|
|
</span>
|
|
<span className="font-bold text-purple-600 text-sm">{amountDisplay}</span>
|
|
</div>
|
|
{lead.quote_terms && (
|
|
<p className="text-[11px] text-muted-foreground leading-tight pt-1 border-t border-purple-500/10">
|
|
<strong className="text-foreground">Terms:</strong> {lead.quote_terms}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="space-y-1 text-xs">
|
|
{lead.phone && (
|
|
<div className="flex items-center gap-1.5 text-foreground font-semibold">
|
|
<Phone className="w-3.5 h-3.5 text-emerald-600" />
|
|
{formattedPhone}
|
|
</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>
|
|
|
|
{/* Actions */}
|
|
<div className="pt-3 border-t border-border space-y-2">
|
|
<div className="flex items-center gap-2">
|
|
<button
|
|
type="button"
|
|
onClick={() => handleResend(lead, 'EMAIL')}
|
|
className="flex-1 inline-flex items-center justify-center gap-1.5 text-xs font-semibold bg-blue-600 hover:bg-blue-700 text-white py-1.5 rounded-lg transition-colors"
|
|
title="Resend Email Quotation"
|
|
>
|
|
<Mail className="w-3.5 h-3.5" />
|
|
Email
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => handleResend(lead, 'WHATSAPP')}
|
|
className="flex-1 inline-flex items-center justify-center gap-1.5 text-xs font-semibold bg-emerald-600 hover:bg-emerald-700 text-white py-1.5 rounded-lg transition-colors"
|
|
title="Resend WhatsApp Quotation"
|
|
>
|
|
<MessageSquare className="w-3.5 h-3.5" />
|
|
WhatsApp
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => handleSimulateReject(lead)}
|
|
className="inline-flex items-center justify-center gap-1 text-xs font-semibold bg-rose-500/10 hover:bg-rose-500/20 text-rose-600 border border-rose-500/20 px-2.5 py-1.5 rounded-lg transition-colors"
|
|
title="Customer Rejected Quote (Move back to Qualified)"
|
|
>
|
|
<RotateCcw className="w-3.5 h-3.5" />
|
|
Reject
|
|
</button>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={() => handleOpenConversionModal(lead)}
|
|
className="w-full inline-flex items-center justify-center gap-1.5 text-xs font-bold bg-purple-600 text-white hover:bg-purple-700 py-2 rounded-lg shadow-xs transition-colors"
|
|
>
|
|
<CheckCircle2 className="w-4 h-4" />
|
|
Simulate Accept & Convert Lead
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
|
|
{/* Modal: Lead Conversion */}
|
|
{convertTarget && (
|
|
<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-lg w-full p-6 space-y-4">
|
|
<div className="flex items-center justify-between">
|
|
<h3 className="text-lg font-bold text-foreground flex items-center gap-2">
|
|
<Sparkles className="w-5 h-5 text-amber-500" />
|
|
Convert Lead to Active Deal & Customer
|
|
</h3>
|
|
<button onClick={() => setConvertTarget(null)} className="text-muted-foreground hover:text-foreground">
|
|
<X className="w-5 h-5" />
|
|
</button>
|
|
</div>
|
|
|
|
<div className="p-3 bg-muted/40 border border-border rounded-lg text-xs space-y-1">
|
|
<p className="font-semibold text-foreground">Customer: {convertTarget.title}</p>
|
|
<p className="text-muted-foreground">Phone: {crmService.formatPhoneDisplay(convertTarget.phone)} | Email: {convertTarget.email || 'N/A'}</p>
|
|
</div>
|
|
|
|
<div className="space-y-4 text-xs">
|
|
<div>
|
|
<label className="block font-bold text-foreground mb-1.5">
|
|
Select Customer Category & Conversion Domain:
|
|
</label>
|
|
<div className="grid grid-cols-3 gap-2">
|
|
<button
|
|
type="button"
|
|
onClick={() => setConvertCategory('ECOMMERCE')}
|
|
className={`p-3 rounded-xl border text-center font-semibold transition-all ${
|
|
convertCategory === 'ECOMMERCE'
|
|
? 'border-primary bg-primary/10 text-primary shadow-xs'
|
|
: 'border-border bg-card hover:bg-muted text-muted-foreground'
|
|
}`}
|
|
>
|
|
E-Commerce Customer
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={() => setConvertCategory('SERVICE')}
|
|
className={`p-3 rounded-xl border text-center font-semibold transition-all ${
|
|
convertCategory === 'SERVICE'
|
|
? 'border-primary bg-primary/10 text-primary shadow-xs'
|
|
: 'border-border bg-card hover:bg-muted text-muted-foreground'
|
|
}`}
|
|
>
|
|
Repair Service Customer
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={() => setConvertCategory('BOTH')}
|
|
className={`p-3 rounded-xl border text-center font-semibold transition-all flex flex-col items-center justify-center gap-1 ${
|
|
convertCategory === 'BOTH'
|
|
? 'border-amber-500 bg-amber-500/10 text-amber-600 dark:text-amber-400 shadow-xs'
|
|
: 'border-border bg-card hover:bg-muted text-muted-foreground'
|
|
}`}
|
|
>
|
|
<span className="flex items-center gap-1 font-bold">
|
|
<Star className="w-3.5 h-3.5 fill-amber-500 text-amber-500" />
|
|
Both Domains
|
|
</span>
|
|
<span className="text-[9px]">⭐ Gold Star VIP</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block font-medium mb-1">Deal Title</label>
|
|
<input
|
|
type="text"
|
|
value={dealTitle}
|
|
onChange={(e) => setDealTitle(e.target.value)}
|
|
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block font-medium mb-1">Final Agreed Deal Amount (₹)</label>
|
|
<input
|
|
type="number"
|
|
value={estimatedValue}
|
|
onChange={(e) => setEstimatedValue(Number(e.target.value))}
|
|
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
|
|
/>
|
|
</div>
|
|
|
|
<div className="pt-2 flex justify-end gap-2">
|
|
<button
|
|
type="button"
|
|
onClick={() => setConvertTarget(null)}
|
|
className="px-4 py-2 rounded-lg border border-border font-medium hover:bg-muted"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isConverting}
|
|
onClick={handleExecuteConversion}
|
|
className="px-4 py-2 rounded-lg bg-primary text-white font-semibold hover:bg-primary/90 flex items-center gap-1.5"
|
|
>
|
|
{isConverting ? 'Converting...' : 'Execute Conversion'}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|