ehsan-dashboards/dashboards/crm/components/PipelineTab.tsx

376 lines
17 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// components/PipelineTab.tsx
'use client';
import React, { useState } from 'react';
import {
Columns3,
Table as TableIcon,
Search,
Filter,
ArrowRight,
Flame,
Phone,
MessageSquare,
Building,
MoreVertical,
CheckCircle,
AlertCircle,
ChevronLeft,
} from 'lucide-react';
import { PIPELINE_STAGES, isOverdue } from '@/lib/automation';
interface PipelineTabProps {
leads: any[];
onSelectLead: (lead: any) => void;
onAdvanceLead?: (leadId: string) => Promise<boolean>;
onRefresh: () => void;
}
export function PipelineTab({
leads,
onSelectLead,
onAdvanceLead,
onRefresh,
}: PipelineTabProps) {
const [viewMode, setViewMode] = useState<'kanban' | 'table'>('kanban');
const [searchTerm, setSearchTerm] = useState('');
const [selectedCity, setSelectedCity] = useState('all');
const [selectedTemperature, setSelectedTemperature] = useState('all');
const [selectedStage, setSelectedStage] = useState('all');
// Filter leads
const filteredLeads = leads.filter((lead) => {
const matchesSearch =
lead.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
lead.ownerName.toLowerCase().includes(searchTerm.toLowerCase()) ||
lead.ownerPhone.includes(searchTerm) ||
(lead.city && lead.city.includes(searchTerm));
const matchesCity = selectedCity === 'all' || lead.city === selectedCity;
const matchesTemp = selectedTemperature === 'all' || lead.temperature === selectedTemperature;
const matchesStage = selectedStage === 'all' || lead.stage === selectedStage;
return matchesSearch && matchesCity && matchesTemp && matchesStage;
});
const cities = Array.from(new Set(leads.map((l) => l.city).filter(Boolean)));
const handleAdvance = async (e: React.MouseEvent, leadId: string) => {
e.stopPropagation();
if (onAdvanceLead) {
await onAdvanceLead(leadId);
} else {
await fetch(`/api/leads/${leadId}/advance`, { method: 'POST' });
}
onRefresh();
};
return (
<div className="space-y-4">
{/* Top Controls: Search, Filters & View Toggle */}
<div className="bg-white p-4 rounded-2xl border border-slate-200 shadow-sm flex flex-col md:flex-row items-center justify-between gap-3">
<div className="flex flex-wrap items-center gap-2.5 w-full md:w-auto">
{/* Search Bar */}
<div className="relative w-full sm:w-64">
<Search className="w-4 h-4 text-slate-400 absolute right-3 top-2.5" />
<input
type="text"
placeholder="جستجوی نام مجموعه، مدیر، شماره..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="w-full pl-3 pr-9 py-1.5 text-xs border border-slate-300 rounded-xl focus:ring-2 focus:ring-sky-500"
/>
</div>
{/* City Filter */}
<select
value={selectedCity}
onChange={(e) => setSelectedCity(e.target.value)}
className="border border-slate-300 rounded-xl px-2.5 py-1.5 text-xs text-slate-700 focus:ring-2 focus:ring-sky-500"
>
<option value="all">همه شهرها</option>
{cities.map((city) => (
<option key={city} value={city}>
{city}
</option>
))}
</select>
{/* Temperature Filter */}
<select
value={selectedTemperature}
onChange={(e) => setSelectedTemperature(e.target.value)}
className="border border-slate-300 rounded-xl px-2.5 py-1.5 text-xs text-slate-700 focus:ring-2 focus:ring-sky-500"
>
<option value="all">همه دماها</option>
<option value="گرم">فقط لیدهای گرم</option>
<option value="خنثی">فقط لیدهای خنثی</option>
<option value="سرد">فقط لیدهای سرد</option>
</select>
{/* Stage Filter for Table View */}
{viewMode === 'table' && (
<select
value={selectedStage}
onChange={(e) => setSelectedStage(e.target.value)}
className="border border-slate-300 rounded-xl px-2.5 py-1.5 text-xs text-slate-700 focus:ring-2 focus:ring-sky-500"
>
<option value="all">همه مراحل پایپ‌لاین</option>
{PIPELINE_STAGES.map((s: any) => {
const stageName = typeof s === 'string' ? s : s.stage;
return (
<option key={stageName} value={stageName}>
{stageName}
</option>
);
})}
</select>
)}
</div>
{/* View Switcher */}
<div className="flex items-center gap-1 bg-slate-100 p-1 rounded-xl self-end md:self-auto">
<button
onClick={() => setViewMode('kanban')}
className={`flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-bold transition-all ${
viewMode === 'kanban'
? 'bg-white text-slate-900 shadow-sm'
: 'text-slate-600 hover:text-slate-900'
}`}
>
<Columns3 className="w-3.5 h-3.5" />
<span>کانبان</span>
</button>
<button
onClick={() => setViewMode('table')}
className={`flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-bold transition-all ${
viewMode === 'table'
? 'bg-white text-slate-900 shadow-sm'
: 'text-slate-600 hover:text-slate-900'
}`}
>
<TableIcon className="w-3.5 h-3.5" />
<span>جدول</span>
</button>
</div>
</div>
{/* KANBAN VIEW */}
{viewMode === 'kanban' && (
<div className="flex gap-4 overflow-x-auto pb-6 pt-1">
{PIPELINE_STAGES.map((s: any) => {
const stage = typeof s === 'string' ? s : s.stage;
const stageLeads = filteredLeads.filter((l) => l.stage === stage);
let headerBg = 'bg-slate-100 text-slate-800';
let borderColor = 'border-slate-200';
if (stage === 'ارتباط اولیه') headerBg = 'bg-sky-100 text-sky-900';
if (stage.includes('پرپوزال')) headerBg = 'bg-blue-100 text-blue-900';
if (stage.includes('گرم')) headerBg = 'bg-rose-100 text-rose-900';
if (stage.includes('سرد')) headerBg = 'bg-slate-200 text-slate-800';
if (stage === 'لوپ پیامکی') headerBg = 'bg-indigo-100 text-indigo-900';
if (stage === 'بسته شد / موفق') headerBg = 'bg-emerald-100 text-emerald-900';
if (stage === 'بایگانی / ناموفق') headerBg = 'bg-gray-200 text-gray-700';
return (
<div
key={stage}
className="w-72 shrink-0 bg-slate-50/70 rounded-2xl border border-slate-200 p-3 flex flex-col max-h-[calc(100vh-220px)]"
>
{/* Column Header */}
<div className={`p-2.5 rounded-xl ${headerBg} flex items-center justify-between mb-3 shadow-sm`}>
<h4 className="font-bold text-xs">{stage}</h4>
<span className="text-xs font-extrabold bg-white/70 px-2 py-0.5 rounded-full">
{stageLeads.length}
</span>
</div>
{/* Cards Container */}
<div className="space-y-2.5 overflow-y-auto flex-1 pr-0.5 pl-0.5">
{stageLeads.length === 0 ? (
<div className="text-center py-8 text-[11px] text-slate-400">
موردی در این مرحله نیست
</div>
) : (
stageLeads.map((lead) => {
const overdue = lead.nextFollowupAt && isOverdue(new Date(lead.nextFollowupAt));
return (
<div
key={lead.id}
onClick={() => onSelectLead(lead.id)}
className="bg-white p-3.5 rounded-xl border border-slate-200 hover:border-sky-300 shadow-sm hover:shadow transition-all cursor-pointer space-y-2.5"
>
<div className="flex items-start justify-between">
<div>
<h5 className="font-bold text-slate-900 text-xs">{lead.name}</h5>
<p className="text-[11px] text-slate-500 mt-0.5">
{lead.city} {lead.district ? `• ${lead.district}` : ''}
</p>
</div>
<span
className={`text-[10px] font-bold px-1.5 py-0.5 rounded ${
lead.temperature === 'گرم'
? 'bg-rose-100 text-rose-700'
: lead.temperature === 'خنثی'
? 'bg-amber-100 text-amber-700'
: 'bg-slate-100 text-slate-600'
}`}
>
{lead.temperature}
</span>
</div>
{/* Rooms Badges */}
{lead.rooms && lead.rooms.length > 0 && (
<div className="flex flex-wrap gap-1">
{lead.rooms.map((r: any) => (
<span
key={r.id}
className="text-[10px] bg-slate-100 text-slate-600 px-1.5 py-0.2 rounded"
>
{r.title}
</span>
))}
</div>
)}
{/* Next Action & Contact */}
<div className="text-[11px] bg-slate-50 p-2 rounded-lg text-slate-600 space-y-1">
<div className="flex items-center justify-between">
<span className="font-semibold">اقدام: {lead.nextChannel}</span>
{overdue && (
<span className="text-[9px] font-bold bg-rose-500 text-white px-1 py-0.2 rounded">
تاخیر
</span>
)}
</div>
<p className="text-[10px] text-slate-500 line-clamp-1">{lead.nextAction}</p>
</div>
{/* Bottom Card Actions */}
<div className="flex items-center justify-between pt-1">
<span className="text-[10px] text-slate-400">
مدیر: {lead.ownerName}
</span>
{stage !== 'بسته شد / موفق' && stage !== 'بایگانی / ناموفق' && (
<button
onClick={(e) => handleAdvance(e, lead.id)}
title="مرحله بعد"
className="flex items-center gap-1 text-[10px] font-bold bg-sky-50 hover:bg-sky-100 text-sky-700 px-2 py-1 rounded-md border border-sky-200 transition-colors"
>
<span>مرحله بعد</span>
<ChevronLeft className="w-3 h-3" />
</button>
)}
</div>
</div>
);
})
)}
</div>
</div>
);
})}
</div>
)}
{/* TABLE VIEW */}
{viewMode === 'table' && (
<div className="bg-white rounded-2xl border border-slate-200 shadow-sm overflow-hidden">
<div className="overflow-x-auto">
<table className="w-full text-right text-xs">
<thead className="bg-slate-50 text-slate-700 border-b border-slate-200 font-bold">
<tr>
<th className="py-3 px-4">نام مجموعه</th>
<th className="py-3 px-4">شهر و منطقه</th>
<th className="py-3 px-4">مدیر و شماره</th>
<th className="py-3 px-4">مرحله فعلی</th>
<th className="py-3 px-4">دما</th>
<th className="py-3 px-4">اقدام و کانال بعدی</th>
<th className="py-3 px-4">تاریخ پیگیری</th>
<th className="py-3 px-4 text-center">عملیات</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100">
{filteredLeads.length === 0 ? (
<tr>
<td colSpan={8} className="py-8 text-center text-slate-400">
هیچ موردی با فیلترهای انتخابی یافت نشد.
</td>
</tr>
) : (
filteredLeads.map((lead) => {
const overdue = lead.nextFollowupAt && isOverdue(new Date(lead.nextFollowupAt));
return (
<tr
key={lead.id}
onClick={() => onSelectLead(lead.id)}
className="hover:bg-slate-50/80 cursor-pointer transition-colors"
>
<td className="py-3 px-4 font-bold text-slate-900">
{lead.name}
{lead.rooms && (
<span className="text-[10px] text-slate-400 block font-normal">
{lead.rooms.map((r: any) => r.title).join('، ')}
</span>
)}
</td>
<td className="py-3 px-4 text-slate-600">
{lead.city} {lead.district ? `(${lead.district})` : ''}
</td>
<td className="py-3 px-4 text-slate-600">
<span className="font-semibold text-slate-800">{lead.ownerName}</span>
<span className="block text-[11px] text-slate-400">{lead.ownerPhone}</span>
</td>
<td className="py-3 px-4">
<span className="bg-slate-100 text-slate-800 px-2 py-0.5 rounded-full font-medium">
{lead.stage}
</span>
</td>
<td className="py-3 px-4">
<span
className={`px-2 py-0.5 rounded-full font-bold text-[10px] ${
lead.temperature === 'گرم'
? 'bg-rose-100 text-rose-700'
: lead.temperature === 'خنثی'
? 'bg-amber-100 text-amber-700'
: 'bg-slate-100 text-slate-600'
}`}
>
{lead.temperature} ({lead.temperatureScore})
</span>
</td>
<td className="py-3 px-4 text-slate-700">
<span className="font-semibold">{lead.nextChannel}: </span>
<span className="text-slate-500">{lead.nextAction}</span>
</td>
<td className="py-3 px-4 text-slate-600">
{lead.nextFollowupAt ? (
<span className={overdue ? 'text-rose-600 font-bold' : ''}>
{new Date(lead.nextFollowupAt).toLocaleDateString('fa-IR')}
{overdue && ' (تاخیر)'}
</span>
) : (
'—'
)}
</td>
<td className="py-3 px-4 text-center">
<button
onClick={(e) => handleAdvance(e, lead.id)}
className="bg-sky-50 hover:bg-sky-100 text-sky-700 px-2.5 py-1 rounded-lg text-xs font-bold border border-sky-200 transition-colors"
>
مرحله بعد
</button>
</td>
</tr>
);
})
)}
</tbody>
</table>
</div>
</div>
)}
</div>
);
}