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

776 lines
No EOL
34 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/LeadModal.tsx
'use client';
import React, { useState } from 'react';
import {
X,
Phone,
Building2,
Calendar,
Flame,
Clock,
Sparkles,
Send,
MessageSquare,
CheckCircle2,
Plus,
Trash2,
ArrowRight,
ExternalLink,
Save,
Tag,
AlertTriangle,
History
} from 'lucide-react';
import { PIPELINE_STAGES, OBJECTION_PLAYBOOK } from '@/lib/automation';
interface Room {
id?: string;
name: string;
capacity?: number | null;
pricePerPerson?: number | null;
}
interface Activity {
id: string;
type: string;
channel?: string | null;
notes?: string | null;
performedBy?: string | null;
createdAt: string;
}
interface Lead {
id: string;
name: string;
managerName?: string | null;
phone: string;
city: string;
district?: string | null;
instagram?: string | null;
website?: string | null;
address?: string | null;
stage: string;
temperature: string;
temperatureScore: number;
nextFollowupDate?: string | null;
nextFollowupChannel?: string | null;
nextActionNote?: string | null;
loopIndex: number;
salesChannels?: string | null;
deadSlots?: string | null;
biggestPain?: string | null;
coreDesire?: string | null;
hookForEkipa?: string | null;
smsTag?: string | null;
objectionStatus?: string | null;
potentialDealValue?: number | null;
rooms?: Room[];
activities?: Activity[];
}
interface LeadModalProps {
lead: Lead | null;
isOpen: boolean;
onClose: () => void;
onRefresh: () => void;
}
export function LeadModal({ lead, isOpen, onClose, onRefresh }: LeadModalProps) {
if (!isOpen || !lead) return null;
const [activeTab, setActiveTab] = useState<'info' | 'ai' | 'activities'>('info');
const [formData, setFormData] = useState<Partial<Lead>>({ ...lead });
const [saving, setSaving] = useState(false);
const [advancing, setAdvancing] = useState(false);
// AI extraction state
const [callNotesInput, setCallNotesInput] = useState('');
const [aiExtracting, setAiExtracting] = useState(false);
const [aiSuccessMessage, setAiSuccessMessage] = useState('');
// New room state
const [newRoomName, setNewRoomName] = useState('');
const [newRoomCap, setNewRoomCap] = useState(6);
const [newRoomPrice, setNewRoomPrice] = useState(250000);
// New activity state
const [activityType, setActivityType] = useState('تماس تلفنی');
const [activityNote, setActivityNote] = useState('');
const [activitySubmitting, setActivitySubmitting] = useState(false);
const handleSaveLead = async () => {
setSaving(true);
try {
const payload: any = {
...formData,
ownerName: (formData as any).ownerName || (formData as any).managerName,
ownerPhone: (formData as any).ownerPhone || (formData as any).phone,
nextAction: (formData as any).nextAction || (formData as any).nextActionNote,
nextChannel: (formData as any).nextChannel || (formData as any).nextFollowupChannel,
painPoint: (formData as any).painPoint || (formData as any).biggestPain,
aspiration: (formData as any).aspiration || (formData as any).coreDesire,
ekipaHook: (formData as any).ekipaHook || (formData as any).hookForEkipa,
};
const res = await fetch(`/api/leads/${lead.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (res.ok) {
onRefresh();
alert('اطلاعات با موفقیت ذخیره شد.');
}
} catch (err) {
console.error(err);
} finally {
setSaving(false);
}
};
const handleAdvanceStage = async () => {
setAdvancing(true);
try {
const res = await fetch(`/api/leads/${lead.id}/advance`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
notes: 'ارتقای مرحله از پروفایل لید'
})
});
if (res.ok) {
onRefresh();
onClose();
}
} catch (err) {
console.error(err);
} finally {
setAdvancing(false);
}
};
const handleAiExtract = async () => {
if (!callNotesInput.trim()) {
alert('لطفاً ابتدا یادداشت مکالمه را وارد کنید.');
return;
}
setAiExtracting(true);
setAiSuccessMessage('');
try {
const res = await fetch('/api/ai/extract', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
leadId: lead.id,
text: callNotesInput
})
});
const data = await res.json();
if (data.success && data.extracted) {
const ext = data.extracted;
setFormData((prev) => ({
...prev,
salesChannels: ext.salesChannels ? ext.salesChannels.join(', ') : prev.salesChannels,
deadSlots: ext.deadSlots || prev.deadSlots,
biggestPain: ext.biggestPain || prev.biggestPain,
coreDesire: ext.coreDesire || prev.coreDesire,
hookForEkipa: ext.hookForEkipa || prev.hookForEkipa,
objectionStatus: ext.objectionStatus || prev.objectionStatus,
smsTag: ext.suggestedSmsTag || prev.smsTag
}));
setAiSuccessMessage('اطلاعات با هوش مصنوعی استخراج و فرم بروزرسانی شد.');
}
} catch (err) {
console.error(err);
} finally {
setAiExtracting(false);
}
};
const handleAddActivity = async (e: React.FormEvent) => {
e.preventDefault();
if (!activityNote.trim()) return;
setActivitySubmitting(true);
try {
const res = await fetch(`/api/leads/${lead.id}/activities`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
type: activityType,
notes: activityNote
})
});
if (res.ok) {
setActivityNote('');
onRefresh();
}
} catch (err) {
console.error(err);
} finally {
setActivitySubmitting(false);
}
};
const handleAddRoom = () => {
if (!newRoomName.trim()) return;
const currentRooms = formData.rooms || [];
setFormData({
...formData,
rooms: [
...currentRooms,
{
name: newRoomName.trim(),
capacity: Number(newRoomCap) || 6,
pricePerPerson: Number(newRoomPrice) || 250000
}
]
});
setNewRoomName('');
};
const handleRemoveRoom = (index: number) => {
const currentRooms = formData.rooms || [];
setFormData({
...formData,
rooms: currentRooms.filter((_, idx) => idx !== index)
});
};
return (
<div className="fixed inset-0 z-50 bg-slate-900/60 backdrop-blur-sm flex items-center justify-center p-3 md:p-6 overflow-y-auto">
<div className="bg-white rounded-3xl shadow-2xl border border-slate-200 w-full max-w-4xl max-h-[90vh] flex flex-col overflow-hidden animate-in fade-in zoom-in-95 duration-150">
{/* Modal Top Bar */}
<div className="p-6 border-b border-slate-200 flex flex-col md:flex-row md:items-center justify-between gap-4 bg-slate-50">
<div className="flex items-start gap-4">
<div className="w-14 h-14 rounded-2xl bg-teal-600 text-white font-bold text-xl flex items-center justify-center shrink-0 shadow-md">
{lead.name.substring(0, 2)}
</div>
<div>
<div className="flex items-center gap-2">
<h2 className="text-xl font-bold text-slate-900">{lead.name}</h2>
<span
className={`px-3 py-0.5 rounded-full text-xs font-bold border ${
lead.temperature === 'گرم'
? 'bg-rose-50 text-rose-700 border-rose-200'
: lead.temperature === 'خنثی'
? 'bg-amber-50 text-amber-700 border-amber-200'
: 'bg-blue-50 text-blue-700 border-blue-200'
}`}
>
<Flame className="w-3.5 h-3.5 inline ml-1" />
{lead.temperature} ({lead.temperatureScore} امتیاز)
</span>
</div>
<div className="flex items-center gap-3 text-xs text-slate-500 mt-1">
<span>{lead.city} {lead.district ? `(${lead.district})` : ''}</span>
<span>•</span>
<span className="font-semibold text-slate-700">{lead.stage}</span>
<span>•</span>
<span dir="ltr">{lead.phone}</span>
</div>
</div>
</div>
<div className="flex items-center gap-2">
<button
onClick={handleAdvanceStage}
disabled={advancing}
className="flex items-center gap-1.5 px-4 py-2.5 rounded-xl bg-teal-600 hover:bg-teal-700 text-white text-xs font-bold shadow-md transition-all active:scale-95"
>
<ArrowRight className="w-4 h-4" />
<span>انجام شد، برو مرحله‌ی بعد</span>
</button>
<button
onClick={onClose}
className="p-2.5 rounded-xl text-slate-400 hover:text-slate-700 hover:bg-slate-200/60 transition-colors"
>
<X className="w-5 h-5" />
</button>
</div>
</div>
{/* Tab Headers */}
<div className="flex border-b border-slate-200 px-6 gap-6 bg-white text-sm font-semibold">
<button
onClick={() => setActiveTab('info')}
className={`py-3.5 border-b-2 transition-colors flex items-center gap-2 ${
activeTab === 'info'
? 'border-teal-600 text-teal-700'
: 'border-transparent text-slate-500 hover:text-slate-800'
}`}
>
<Building2 className="w-4 h-4" />
مشخصات، پایپ‌لاین و اتاق‌ها
</button>
<button
onClick={() => setActiveTab('ai')}
className={`py-3.5 border-b-2 transition-colors flex items-center gap-2 ${
activeTab === 'ai'
? 'border-teal-600 text-teal-700'
: 'border-transparent text-slate-500 hover:text-slate-800'
}`}
>
<Sparkles className="w-4 h-4 text-purple-600" />
تحلیل هوشمند و پر کردن با AI
</button>
<button
onClick={() => setActiveTab('activities')}
className={`py-3.5 border-b-2 transition-colors flex items-center gap-2 ${
activeTab === 'activities'
? 'border-teal-600 text-teal-700'
: 'border-transparent text-slate-500 hover:text-slate-800'
}`}
>
<History className="w-4 h-4" />
تاریخچه تعاملات ({lead.activities?.length || 0})
</button>
</div>
{/* Modal Body */}
<div className="p-6 overflow-y-auto space-y-6 flex-1 bg-slate-50/30">
{/* TAB 1: INFO & ROOMS */}
{activeTab === 'info' && (
<div className="space-y-6">
{/* General Fields */}
<div className="bg-white p-5 rounded-2xl border border-slate-200 shadow-sm space-y-4">
<h3 className="font-bold text-sm text-slate-900 border-b border-slate-100 pb-2">
اطلاعات تماس و مدیریت
</h3>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">نام مدیر</label>
<input
type="text"
value={formData.managerName || ''}
onChange={(e) => setFormData({ ...formData, managerName: e.target.value })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">شماره تماس</label>
<input
type="text"
dir="ltr"
value={formData.phone || ''}
onChange={(e) => setFormData({ ...formData, phone: e.target.value })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">شهر</label>
<input
type="text"
value={formData.city || ''}
onChange={(e) => setFormData({ ...formData, city: e.target.value })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">منطقه / محله</label>
<input
type="text"
value={formData.district || ''}
onChange={(e) => setFormData({ ...formData, district: e.target.value })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">آیدی اینستاگرام</label>
<input
type="text"
dir="ltr"
value={formData.instagram || ''}
onChange={(e) => setFormData({ ...formData, instagram: e.target.value })}
placeholder="@escaperoom"
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">وب‌سایت / پلتفرم رزرو</label>
<input
type="text"
dir="ltr"
value={formData.website || ''}
onChange={(e) => setFormData({ ...formData, website: e.target.value })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
</div>
</div>
{/* Pipeline Status Fields */}
<div className="bg-white p-5 rounded-2xl border border-slate-200 shadow-sm space-y-4">
<h3 className="font-bold text-sm text-slate-900 border-b border-slate-100 pb-2">
وضعیت پایپ‌لاین و پیگیری
</h3>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">مرحله فعلی</label>
<select
value={formData.stage || ''}
onChange={(e) => setFormData({ ...formData, stage: e.target.value })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500 font-semibold"
>
{PIPELINE_STAGES.map((s, idx) => (
<option key={idx} value={s.stage}>
{s.stage}
</option>
))}
</select>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">کانال پیگیری بعدی</label>
<select
value={formData.nextFollowupChannel || 'تلفنی'}
onChange={(e) => setFormData({ ...formData, nextFollowupChannel: e.target.value })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
>
<option value="تلفنی">تلفنی</option>
<option value="پیامک">پیامک</option>
<option value="حضوری">حضوری</option>
<option value="انجام شد">انجام شد</option>
</select>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">دور لوپ پیگیری</label>
<input
type="number"
value={formData.loopIndex || 0}
onChange={(e) => setFormData({ ...formData, loopIndex: Number(e.target.value) })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div className="md:col-span-3">
<label className="text-xs font-semibold text-slate-600 block mb-1">اقدام بعدی یا خلاصه یادداشت</label>
<input
type="text"
value={formData.nextActionNote || ''}
onChange={(e) => setFormData({ ...formData, nextActionNote: e.target.value })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
</div>
</div>
{/* Rooms Section */}
<div className="bg-white p-5 rounded-2xl border border-slate-200 shadow-sm space-y-4">
<h3 className="font-bold text-sm text-slate-900 border-b border-slate-100 pb-2">
اتاق‌های فرار مجموعه ({formData.rooms?.length || 0} اتاق)
</h3>
{/* Rooms List */}
<div className="space-y-2">
{(formData.rooms || []).map((room, idx) => (
<div
key={idx}
className="flex items-center justify-between p-3 rounded-xl bg-slate-50 border border-slate-200 text-xs"
>
<div className="flex items-center gap-3">
<span className="w-6 h-6 rounded-lg bg-teal-100 text-teal-800 font-bold flex items-center justify-center">
{idx + 1}
</span>
<span className="font-bold text-slate-800 text-sm">{room.name}</span>
<span className="text-slate-500">ظرفیت: {room.capacity || 6} نفر</span>
<span className="text-slate-500">
قیمت هر نفر: {(room.pricePerPerson || 250000).toLocaleString('fa-IR')} تومان
</span>
</div>
<button
onClick={() => handleRemoveRoom(idx)}
className="text-rose-500 hover:text-rose-700 p-1 rounded-lg"
>
<Trash2 className="w-4 h-4" />
</button>
</div>
))}
</div>
{/* Add Room Row */}
<div className="pt-2 border-t border-slate-100 flex flex-wrap items-center gap-2">
<input
type="text"
placeholder="نام اتاق فرار جدید"
value={newRoomName}
onChange={(e) => setNewRoomName(e.target.value)}
className="text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500 flex-1"
/>
<input
type="number"
placeholder="ظرفیت"
value={newRoomCap}
onChange={(e) => setNewRoomCap(Number(e.target.value))}
className="text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500 w-20"
/>
<input
type="number"
placeholder="قیمت هر نفر"
value={newRoomPrice}
onChange={(e) => setNewRoomPrice(Number(e.target.value))}
className="text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500 w-32"
/>
<button
onClick={handleAddRoom}
className="flex items-center gap-1 px-3 py-2 bg-slate-800 hover:bg-slate-900 text-white rounded-xl text-xs font-semibold"
>
<Plus className="w-4 h-4" />
افزودن اتاق
</button>
</div>
</div>
{/* Save Button */}
<div className="flex justify-end">
<button
onClick={handleSaveLead}
disabled={saving}
className="flex items-center gap-2 px-6 py-2.5 bg-teal-600 hover:bg-teal-700 text-white rounded-xl text-xs font-bold shadow transition-all"
>
<Save className="w-4 h-4" />
<span>{saving ? 'در حال ذخیره...' : 'ذخیره تغییرات'}</span>
</button>
</div>
</div>
)}
{/* TAB 2: AI INSIGHTS */}
{activeTab === 'ai' && (
<div className="space-y-6">
{/* Call Note Input & AI Button */}
<div className="bg-purple-50/70 border border-purple-200 rounded-2xl p-5 space-y-3">
<div className="flex items-center gap-2 text-purple-900 font-bold text-sm">
<Sparkles className="w-5 h-5 text-purple-600" />
<span>استخراج خودکار اطلاعات لید از متن تماس (AI)</span>
</div>
<p className="text-xs text-purple-800 leading-relaxed">
متن خلاصه مکالمه یا یادداشت صوتی تبدیل‌شده به متن را در کادر زیر وارد کنید و دکمه را بزنید؛ سیستم هوشمند فیلدهای سانس‌های خالی، خواست قلبی، چالش‌ها و اعتراض مشتری را خودکار استخراج می‌کند.
</p>
<textarea
rows={3}
value={callNotesInput}
onChange={(e) => setCallNotesInput(e.target.value)}
placeholder="مثال: با آقای باقری صحبت شد. گفتن وسط هفته‌ها سانس‌های ۲ تا ۶ بعدازظهر کاملاً خالیه. از کارمزد پلتفرم‌های دیگه ناراضین و دنبال فروش بیشتر آخر هفته نیستن بلکه وسط هفته براشون مهمه..."
className="w-full text-xs p-3 rounded-xl border border-purple-300 focus:ring-2 focus:ring-purple-500 bg-white"
/>
<div className="flex items-center justify-between">
<button
onClick={handleAiExtract}
disabled={aiExtracting}
className="flex items-center gap-2 px-5 py-2.5 bg-purple-600 hover:bg-purple-700 text-white text-xs font-bold rounded-xl shadow-md transition-all active:scale-95"
>
<Sparkles className="w-4 h-4" />
<span>{aiExtracting ? 'در حال پردازش هوش مصنوعی...' : '✨ پر کردن خودکار با AI'}</span>
</button>
{aiSuccessMessage && (
<span className="text-xs text-emerald-700 font-bold bg-emerald-100 px-3 py-1 rounded-lg">
{aiSuccessMessage}
</span>
)}
</div>
</div>
{/* Extracted Fields Form */}
<div className="bg-white p-5 rounded-2xl border border-slate-200 space-y-4">
<h3 className="font-bold text-sm text-slate-900 border-b border-slate-100 pb-2">
بینش‌های استخراج‌شده و قلاب اکیپا
</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">
سانس‌های مرده و خالی
</label>
<input
type="text"
value={formData.deadSlots || ''}
onChange={(e) => setFormData({ ...formData, deadSlots: e.target.value })}
placeholder="مثال: شنبه تا سه‌شنبه ۱۲ تا ۱۸"
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">
کانال‌های فروش فعلی
</label>
<input
type="text"
value={formData.salesChannels || ''}
onChange={(e) => setFormData({ ...formData, salesChannels: e.target.value })}
placeholder="مثال: تلفنی، اینستاگرام، تایم فور فان"
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">
بزرگ‌ترین درد و چالش
</label>
<input
type="text"
value={formData.biggestPain || ''}
onChange={(e) => setFormData({ ...formData, biggestPain: e.target.value })}
placeholder="مثال: کنسلی‌های لحظه آخری و نبود مشتری وسط هفته"
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">
خواست و هدف قلبی مدیر
</label>
<input
type="text"
value={formData.coreDesire || ''}
onChange={(e) => setFormData({ ...formData, coreDesire: e.target.value })}
placeholder="مثال: پر شدن سانس‌های خالی بدون هزینه اولیه سنگین"
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
</div>
<div className="md:col-span-2">
<label className="text-xs font-semibold text-teal-800 block mb-1">
قلاب اختصاصی اکیپا برای این مجموعه (Hook)
</label>
<input
type="text"
value={formData.hookForEkipa || ''}
onChange={(e) => setFormData({ ...formData, hookForEkipa: e.target.value })}
placeholder="مثال: پر کردن تضمینی سانس‌های بعدازظهر بدون کارمزد ثابت"
className="w-full text-xs px-3 py-2 rounded-xl border border-teal-300 bg-teal-50/50 focus:ring-2 focus:ring-teal-500 font-semibold"
/>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">
نوع اعتراض مطرح‌شده
</label>
<select
value={formData.objectionStatus || ''}
onChange={(e) => setFormData({ ...formData, objectionStatus: e.target.value })}
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
>
<option value="">بدون اعتراض خاص</option>
{Object.keys(OBJECTION_PLAYBOOK).map((key) => (
<option key={key} value={key}>
{key}
</option>
))}
</select>
</div>
<div>
<label className="text-xs font-semibold text-slate-600 block mb-1">
قالب پیامک پیشنهادی
</label>
<input
type="text"
value={formData.smsTag || ''}
onChange={(e) => setFormData({ ...formData, smsTag: e.target.value })}
placeholder="sms_proposal_welcome"
className="w-full text-xs px-3 py-2 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500 font-mono"
/>
</div>
</div>
<div className="pt-2 flex justify-end">
<button
onClick={handleSaveLead}
disabled={saving}
className="flex items-center gap-2 px-6 py-2.5 bg-purple-600 hover:bg-purple-700 text-white rounded-xl text-xs font-bold shadow transition-all"
>
<Save className="w-4 h-4" />
<span>{saving ? 'در حال ذخیره...' : 'ذخیره بینش‌های AI'}</span>
</button>
</div>
</div>
</div>
)}
{/* TAB 3: ACTIVITIES & LOGS */}
{activeTab === 'activities' && (
<div className="space-y-6">
{/* Add Activity Form */}
<form onSubmit={handleAddActivity} className="bg-white p-5 rounded-2xl border border-slate-200 space-y-3">
<h3 className="font-bold text-sm text-slate-900">ثبت تعامل یا تماس جدید</h3>
<div className="flex gap-2">
{['تماس تلفنی', 'ارسال پیامک', 'جلسه حضوری', 'پیگیری واتساپ'].map((t) => (
<button
key={t}
type="button"
onClick={() => setActivityType(t)}
className={`px-3 py-1.5 rounded-lg text-xs font-medium border transition-colors ${
activityType === t
? 'bg-slate-900 text-white border-slate-900'
: 'bg-white text-slate-600 border-slate-200'
}`}
>
{t}
</button>
))}
</div>
<textarea
rows={2}
value={activityNote}
onChange={(e) => setActivityNote(e.target.value)}
placeholder="نتیجه مکالمه یا بازخورد لید را یادداشت کنید..."
className="w-full text-xs p-3 rounded-xl border border-slate-300 focus:ring-2 focus:ring-teal-500"
/>
<div className="flex justify-end">
<button
type="submit"
disabled={activitySubmitting}
className="flex items-center gap-1.5 px-4 py-2 bg-teal-600 hover:bg-teal-700 text-white rounded-xl text-xs font-bold transition-all"
>
<Send className="w-3.5 h-3.5" />
<span>ثبت در تاریخچه</span>
</button>
</div>
</form>
{/* Activity Timeline */}
<div className="space-y-3">
{(lead.activities || []).length === 0 ? (
<p className="text-center text-xs text-slate-400 py-6">هیچ تعاملی ثبت نشده است.</p>
) : (
(lead.activities || []).map((act) => (
<div
key={act.id}
className="bg-white p-4 rounded-xl border border-slate-200 flex items-start gap-3 text-xs"
>
<div className="w-8 h-8 rounded-lg bg-teal-50 text-teal-700 flex items-center justify-center shrink-0">
<MessageSquare className="w-4 h-4" />
</div>
<div className="flex-1">
<div className="flex items-center justify-between">
<span className="font-bold text-slate-800">{act.type}</span>
<span className="text-[11px] text-slate-400 font-mono" dir="ltr">
{new Date(act.createdAt).toLocaleString('fa-IR')}
</span>
</div>
{act.notes && (
<p className="text-slate-600 mt-1 leading-relaxed bg-slate-50 p-2 rounded-lg">
{act.notes}
</p>
)}
{act.performedBy && (
<span className="text-[10px] text-slate-400 block mt-1">
ثبت توسط: {act.performedBy}
</span>
)}
</div>
</div>
))
)}
</div>
</div>
)}
</div>
</div>
</div>
);
}