ifixkart-admin/app/(admin)/crm-leads/new/page.tsx

338 lines
15 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import { crmService, CrmLead } from '@/services/api/crmService';
import { Users, Plus, Phone, Mail, ArrowRight, MessageSquare, Clock, Sparkles, X, LayoutGrid, List } from 'lucide-react';
import { toast } from 'sonner';
export default function NewInquiriesPage() {
const router = useRouter();
const [leads, setLeads] = useState<CrmLead[]>([]);
const [loading, setLoading] = useState(true);
const [viewMode, setViewMode] = useState<'grid' | 'list'>('grid');
const [showAddModal, setShowAddModal] = useState(false);
const [newLead, setNewLead] = useState({ title: '', phone: '', email: '', source: 'STOREFRONT', notes: '' });
useEffect(() => {
loadLeads();
}, []);
const loadLeads = async () => {
try {
setLoading(true);
const data = await crmService.getLeads('NEW');
setLeads(data || []);
} catch (err) {
setLeads([]);
} finally {
setLoading(false);
}
};
const handleAdvanceToContacted = async (lead: CrmLead) => {
try {
await crmService.updateLeadStatus(lead.lead_id, 'CONTACTED');
toast.success(`Moved lead "${lead.title || lead.phone}" to Contacted stage!`);
router.push('/crm-leads/contacted');
} catch (err) {
toast.error('Failed to update lead status');
}
};
const handleCreateLead = async (e: React.FormEvent) => {
e.preventDefault();
try {
await crmService.createLead(newLead);
toast.success('Inbound Lead created successfully!');
setShowAddModal(false);
setNewLead({ title: '', phone: '', email: '', source: 'STOREFRONT', notes: '' });
loadLeads();
} catch (err: any) {
toast.error(err.message || 'Failed to create lead');
}
};
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-blue-500/10 text-blue-600 border border-blue-500/20">
Stage 1 of 5
</span>
<h1 className="text-2xl font-bold text-foreground">1. New Inquiries</h1>
</div>
<p className="text-xs text-muted-foreground mt-1">
Incoming customer leads from Storefront Forms, WhatsApp, and Storefront Customer Logins
</p>
</div>
<div className="flex items-center gap-3">
<div className="flex items-center gap-1 bg-muted p-1 rounded-lg border border-border">
<button
onClick={() => setViewMode('grid')}
className={`p-1.5 rounded-md text-xs font-semibold flex items-center gap-1 transition-colors ${
viewMode === 'grid'
? 'bg-background text-foreground shadow-xs'
: 'text-muted-foreground hover:text-foreground'
}`}
title="Grid View"
>
<LayoutGrid className="w-4 h-4" />
<span className="hidden sm:inline">Grid</span>
</button>
<button
onClick={() => setViewMode('list')}
className={`p-1.5 rounded-md text-xs font-semibold flex items-center gap-1 transition-colors ${
viewMode === 'list'
? 'bg-background text-foreground shadow-xs'
: 'text-muted-foreground hover:text-foreground'
}`}
title="List View"
>
<List className="w-4 h-4" />
<span className="hidden sm:inline">List</span>
</button>
</div>
<button
onClick={() => setShowAddModal(true)}
className="flex items-center gap-1.5 bg-primary text-white hover:bg-primary/90 px-3.5 py-2 rounded-lg text-xs font-semibold shadow-xs transition-colors"
>
<Plus className="w-4 h-4" />
Add Lead
</button>
</div>
</div>
{loading ? (
<div className="py-20 text-center text-muted-foreground animate-pulse text-sm">
Loading New Inquiries...
</div>
) : leads.length === 0 ? (
<div className="py-16 text-center bg-card border border-border rounded-xl">
<Sparkles className="w-10 h-10 text-blue-500 mx-auto mb-2 opacity-50" />
<h3 className="text-sm font-semibold text-foreground">No new inquiries right now</h3>
<p className="text-xs text-muted-foreground mt-1">
New storefront inquiries & customer logins will automatically land here.
</p>
</div>
) : viewMode === 'grid' ? (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{leads.map((lead) => {
const cleanPhone = lead.phone ? lead.phone.replace(/[^0-9]/g, '') : '';
return (
<div key={lead.lead_id} className="bg-card border border-border hover:border-blue-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 || 'Inbound Inquiry'}
</h3>
<span className="px-2 py-0.5 rounded-md text-[10px] font-bold bg-blue-500/10 text-blue-600">
NEW
</span>
</div>
<div className="space-y-1 text-xs">
{lead.phone && (
<div className="flex items-center justify-between text-foreground font-medium">
<span className="flex items-center gap-1.5">
<Phone className="w-3.5 h-3.5 text-muted-foreground" />
{lead.phone}
</span>
{cleanPhone && (
<a
href={`https://wa.me/${cleanPhone}`}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-[10px] font-semibold bg-emerald-500/10 text-emerald-600 px-1.5 py-0.5 rounded-md hover:bg-emerald-500 hover:text-white transition-colors"
>
<MessageSquare className="w-3 h-3" />
WhatsApp
</a>
)}
</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>
<div className="pt-3 border-t border-border flex items-center justify-between">
<span className="text-[11px] text-muted-foreground">Needs initial outreach</span>
<button
onClick={() => handleAdvanceToContacted(lead)}
className="inline-flex items-center gap-1 text-xs font-semibold bg-blue-600 text-white hover:bg-blue-700 px-3 py-1.5 rounded-lg shadow-xs transition-colors"
>
<span>Move to Contacted</span>
<ArrowRight className="w-3.5 h-3.5" />
</button>
</div>
</div>
);
})}
</div>
) : (
<div className="overflow-x-auto border border-border rounded-xl bg-card shadow-xs">
<table className="w-full text-left border-collapse text-xs">
<thead>
<tr className="border-b border-border bg-muted/50 text-muted-foreground font-semibold">
<th className="p-3.5">Inquiry Title / Customer</th>
<th className="p-3.5">Phone Number</th>
<th className="p-3.5">Email Address</th>
<th className="p-3.5">Source</th>
<th className="p-3.5">Created Date</th>
<th className="p-3.5 text-right">Action</th>
</tr>
</thead>
<tbody className="divide-y divide-border">
{leads.map((lead) => {
const cleanPhone = lead.phone ? lead.phone.replace(/[^0-9]/g, '') : '';
return (
<tr key={lead.lead_id} className="hover:bg-muted/30 transition-colors">
<td className="p-3.5 font-semibold text-foreground">
{lead.title || lead.first_name || 'Inbound Inquiry'}
</td>
<td className="p-3.5">
<div className="flex items-center gap-2">
<span className="font-medium text-foreground">{lead.phone || '—'}</span>
{cleanPhone && (
<a
href={`https://wa.me/${cleanPhone}`}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-[10px] font-semibold bg-emerald-500/10 text-emerald-600 px-2 py-0.5 rounded-md hover:bg-emerald-500 hover:text-white transition-colors"
>
<MessageSquare className="w-3 h-3" />
WhatsApp
</a>
)}
</div>
</td>
<td className="p-3.5 text-muted-foreground">{lead.email || '—'}</td>
<td className="p-3.5">
<span className="bg-muted px-2 py-0.5 rounded-md font-semibold text-[10px] text-muted-foreground">
{lead.source}
</span>
</td>
<td className="p-3.5 text-muted-foreground">
{new Date(lead.created_at).toLocaleDateString()}
</td>
<td className="p-3.5 text-right">
<button
onClick={() => handleAdvanceToContacted(lead)}
className="inline-flex items-center gap-1 text-xs font-semibold bg-blue-600 text-white hover:bg-blue-700 px-3 py-1.5 rounded-lg shadow-xs transition-colors"
>
<span>Move to Contacted</span>
<ArrowRight className="w-3.5 h-3.5" />
</button>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
{/* Modal: Add Inbound Lead */}
{showAddModal && (
<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">
<h3 className="text-lg font-bold text-foreground flex items-center gap-2">
<Plus className="w-5 h-5 text-primary" />
Add Lead
</h3>
<button onClick={() => setShowAddModal(false)} className="text-muted-foreground hover:text-foreground">
<X className="w-5 h-5" />
</button>
</div>
<form onSubmit={handleCreateLead} className="space-y-3 text-xs">
<div>
<label className="block font-medium mb-1">Inquiry Title / Customer Name</label>
<input
type="text"
required
placeholder="e.g. Bulk iPhone Purchase Inquiry"
value={newLead.title}
onChange={(e) => setNewLead({ ...newLead, title: 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">Phone Number</label>
<input
type="text"
required
placeholder="+91 9876543210"
value={newLead.phone}
onChange={(e) => setNewLead({ ...newLead, phone: 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">Email Address (Optional)</label>
<input
type="email"
placeholder="customer@company.com"
value={newLead.email}
onChange={(e) => setNewLead({ ...newLead, email: 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">Source</label>
<select
value={newLead.source}
onChange={(e) => setNewLead({ ...newLead, source: e.target.value })}
className="w-full bg-muted border border-border rounded-lg p-2.5 text-foreground focus:outline-hidden"
>
<option value="STOREFRONT">Storefront Form</option>
<option value="WHATSAPP">WhatsApp Message</option>
<option value="WALK_IN">Store Walk-In</option>
<option value="PHONE">Phone Call</option>
</select>
</div>
<div className="pt-3 flex justify-end gap-2">
<button
type="button"
onClick={() => setShowAddModal(false)}
className="px-4 py-2 rounded-lg border border-border font-medium hover:bg-muted"
>
Cancel
</button>
<button
type="submit"
className="px-4 py-2 rounded-lg bg-primary text-white font-semibold hover:bg-primary/90"
>
Create Lead
</button>
</div>
</form>
</div>
</div>
)}
</div>
);
}