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

357 lines
No EOL
16 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/SettingsTab.tsx
'use client';
import React, { useState } from 'react';
import {
Settings as SettingsIcon,
Database,
Bot,
MessageSquare,
ShieldAlert,
Save,
CheckCircle2,
HardDrive,
Workflow,
Sparkles,
RefreshCw,
ExternalLink,
BookOpen
} from 'lucide-react';
import { PIPELINE_STAGES, OBJECTION_PLAYBOOK, FOLLOWUP_RULES } from '@/lib/automation';
import { SMS_TEMPLATES } from '@/lib/sms';
interface SettingsTabProps {
leadCount?: number;
stats?: any;
onRefresh: () => void;
}
export function SettingsTab({ leadCount, stats, onRefresh }: SettingsTabProps) {
const [activeSection, setActiveSection] = useState<'automation' | 'sms' | 'objections' | 'database'>('automation');
const [n8nWebhookUrl, setN8nWebhookUrl] = useState('');
const [smsMode, setSmsMode] = useState<'mock' | 'live'>('mock');
const [savedSuccess, setSavedSuccess] = useState(false);
const handleSave = () => {
setSavedSuccess(true);
setTimeout(() => setSavedSuccess(false), 3000);
};
return (
<div className="space-y-6">
{/* Top Header */}
<div className="bg-white rounded-2xl border border-slate-200 p-6 flex flex-col md:flex-row md:items-center justify-between gap-4">
<div>
<div className="flex items-center gap-2 mb-1">
<SettingsIcon className="w-6 h-6 text-slate-700" />
<h2 className="text-xl font-bold text-slate-900">تنظیمات و زیرساخت اتوماسیون</h2>
</div>
<p className="text-sm text-slate-500">
پیکربندی قوانین خودکار پایپ‌لاین، موتور پیامک، کتابچه اعتراضات و دیتابیس محلی پایدار SQLite
</p>
</div>
{savedSuccess && (
<div className="flex items-center gap-2 text-emerald-700 bg-emerald-50 px-4 py-2 rounded-xl border border-emerald-200 text-sm">
<CheckCircle2 className="w-4 h-4" />
<span>تنظیمات با موفقیت ذخیره شد</span>
</div>
)}
</div>
{/* Nav Tabs */}
<div className="flex gap-2 border-b border-slate-200 pb-2">
<button
onClick={() => setActiveSection('automation')}
className={`flex items-center gap-2 px-4 py-2.5 rounded-xl text-sm font-medium transition-colors ${
activeSection === 'automation'
? 'bg-slate-900 text-white'
: 'text-slate-600 hover:bg-slate-100'
}`}
>
<Workflow className="w-4 h-4" />
قوانین مراحل و تاخیرها
</button>
<button
onClick={() => setActiveSection('objections')}
className={`flex items-center gap-2 px-4 py-2.5 rounded-xl text-sm font-medium transition-colors ${
activeSection === 'objections'
? 'bg-slate-900 text-white'
: 'text-slate-600 hover:bg-slate-100'
}`}
>
<BookOpen className="w-4 h-4" />
کتابچه پاسخ به اعتراضات
</button>
<button
onClick={() => setActiveSection('sms')}
className={`flex items-center gap-2 px-4 py-2.5 rounded-xl text-sm font-medium transition-colors ${
activeSection === 'sms'
? 'bg-slate-900 text-white'
: 'text-slate-600 hover:bg-slate-100'
}`}
>
<MessageSquare className="w-4 h-4" />
قالب‌های پیامک و اتصال n8n
</button>
<button
onClick={() => setActiveSection('database')}
className={`flex items-center gap-2 px-4 py-2.5 rounded-xl text-sm font-medium transition-colors ${
activeSection === 'database'
? 'bg-slate-900 text-white'
: 'text-slate-600 hover:bg-slate-100'
}`}
>
<Database className="w-4 h-4" />
پایگاه داده محلی SQLite
</button>
</div>
{/* Section 1: Automation Rules */}
{activeSection === 'automation' && (
<div className="bg-white rounded-2xl border border-slate-200 overflow-hidden">
<div className="p-5 border-b border-slate-100 bg-slate-50 flex items-center justify-between">
<div>
<h3 className="font-bold text-slate-800">جدول قوانین اتوماسیون پایپ‌لاین</h3>
<p className="text-xs text-slate-500 mt-0.5">
سیستم با ثبت هر اقدام، مرحله بعدی، کانال ارتباطی و فاصله زمانی مناسب را بر اساس این ماتریس محاسبه می‌کند.
</p>
</div>
<span className="text-xs font-semibold px-2.5 py-1 bg-teal-100 text-teal-800 rounded-lg">
۱۰ مرحله استاندارد
</span>
</div>
<div className="overflow-x-auto">
<table className="w-full text-right text-xs">
<thead className="bg-slate-100/70 text-slate-600 font-semibold border-b border-slate-200">
<tr>
<th className="p-3.5">مرحله</th>
<th className="p-3.5">اقدام بعدی خودکار</th>
<th className="p-3.5">کانال پیگیری</th>
<th className="p-3.5">فاصله زمانی (روز)</th>
<th className="p-3.5">برچسب پیامک پیش‌فرض</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100">
{FOLLOWUP_RULES.map((r, idx) => (
<tr key={idx} className="hover:bg-slate-50/50">
<td className="p-3.5 font-bold text-slate-900">{r.currentStage}</td>
<td className="p-3.5 text-slate-700">{r.action}</td>
<td className="p-3.5">
<span
className={`px-2 py-0.5 rounded-full text-[11px] font-semibold ${
r.channel === 'پیامک'
? 'bg-blue-100 text-blue-800'
: 'bg-emerald-100 text-emerald-800'
}`}
>
{r.channel}
</span>
</td>
<td className="p-3.5 text-slate-800 font-mono">
{r.delayDays === 0 ? 'همان روز' : `${r.delayDays} روز`}
</td>
<td className="p-3.5">
{r.smsTag ? (
<span className="px-2 py-0.5 rounded bg-slate-100 text-slate-700 font-mono text-[11px]">
{r.smsTag}
</span>
) : (
<span className="text-slate-400">—</span>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
)}
{/* Section 2: Objections Playbook */}
{activeSection === 'objections' && (
<div className="space-y-4">
<div className="bg-amber-50 border border-amber-200 rounded-2xl p-5 text-amber-900 flex items-start gap-3">
<ShieldAlert className="w-5 h-5 text-amber-600 shrink-0 mt-0.5" />
<div>
<h4 className="font-bold text-sm">پلی‌بوک پاسخ به اعتراضات صاحبان اتاق فرار</h4>
<p className="text-xs text-amber-800 mt-1 leading-relaxed">
این راهنما در تماس‌های تلفنی و ثبت لاگ فعالیت برای هر لید به عنوان دستیار هوشمند نمایش داده می‌شود تا تیم فروش سریع‌ترین و بهترین پاسخ اقناعی را ارائه دهد.
</p>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{Object.entries(OBJECTION_PLAYBOOK).map(([key, item], idx) => (
<div key={idx} className="bg-white rounded-2xl border border-slate-200 p-5 shadow-sm space-y-3">
<div className="flex items-center justify-between">
<span className="font-bold text-sm text-slate-900 bg-slate-100 px-3 py-1 rounded-xl">
{key}
</span>
<span className="text-xs text-rose-600 bg-rose-50 px-2 py-0.5 rounded-md font-medium">
اعتراض متداول
</span>
</div>
<div>
<span className="text-[11px] font-bold text-slate-500 block mb-1">زاویه دید مشتری:</span>
<p className="text-xs text-slate-600 bg-slate-50 p-2.5 rounded-xl border border-slate-100 leading-relaxed">
«{(item as any).objection || (item as any).angle}»
</p>
</div>
<div>
<span className="text-[11px] font-bold text-emerald-700 block mb-1 flex items-center gap-1">
<Sparkles className="w-3.5 h-3.5" />
پاسخ پیشنهادی اکیپا:
</span>
<p className="text-xs text-emerald-900 bg-emerald-50/80 p-3 rounded-xl border border-emerald-200 leading-relaxed font-medium">
{(item as any).rebuttal || (item as any).pitch}
</p>
</div>
</div>
))}
</div>
</div>
)}
{/* Section 3: SMS & n8n */}
{activeSection === 'sms' && (
<div className="space-y-6">
<div className="bg-white rounded-2xl border border-slate-200 p-6 space-y-4">
<h3 className="font-bold text-slate-900 flex items-center gap-2">
<MessageSquare className="w-5 h-5 text-indigo-600" />
تنظیمات وب‌هوک n8n و شبیه‌ساز پیامک
</h3>
<p className="text-xs text-slate-500">
در حال حاضر سامانه پیامک در وضعیت خودکفا (Mock) عمل می‌کند و کلیه رویدادهای ارسالی در جدول تاریخچه دیتابیس ثبت می‌شوند. در صورت اتصال وب‌هوک n8n، پیامک‌ها به سرویس کاوه‌نگار یا فراز انتقال می‌یابند.
</p>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 pt-2">
<div>
<label className="text-xs font-semibold text-slate-700 block mb-1.5">
حالت ارسال پیامک
</label>
<div className="flex gap-2">
<button
onClick={() => setSmsMode('mock')}
className={`flex-1 py-2 px-3 rounded-xl text-xs font-semibold border transition-colors ${
smsMode === 'mock'
? 'bg-emerald-600 text-white border-emerald-600'
: 'bg-white text-slate-600 border-slate-300'
}`}
>
شبیه‌ساز داخلی (Mock / بدون وابستگی)
</button>
<button
onClick={() => setSmsMode('live')}
className={`flex-1 py-2 px-3 rounded-xl text-xs font-semibold border transition-colors ${
smsMode === 'live'
? 'bg-indigo-600 text-white border-indigo-600'
: 'bg-white text-slate-600 border-slate-300'
}`}
>
اتصال زنده به n8n Webhook
</button>
</div>
</div>
<div>
<label className="text-xs font-semibold text-slate-700 block mb-1.5">
آدرس وب‌هوک n8n (اختیاری)
</label>
<input
type="text"
placeholder="https://n8n.domain.com/webhook/crm-sms"
value={n8nWebhookUrl}
onChange={(e) => setN8nWebhookUrl(e.target.value)}
dir="ltr"
className="w-full text-xs font-mono px-3 py-2 rounded-xl border border-slate-300 focus:outline-none focus:ring-2 focus:ring-indigo-500"
/>
</div>
</div>
<div className="pt-2 flex justify-end">
<button
onClick={handleSave}
className="flex items-center gap-1.5 px-4 py-2 bg-slate-900 text-white text-xs font-semibold rounded-xl hover:bg-slate-800 transition-colors"
>
<Save className="w-3.5 h-3.5" />
ذخیره تنظیمات وب‌هوک
</button>
</div>
</div>
{/* Templates Preview */}
<div className="bg-white rounded-2xl border border-slate-200 p-6 space-y-4">
<h3 className="font-bold text-slate-900">قالب‌های فعال پیامک اکیپا</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{Object.entries(SMS_TEMPLATES).map(([tag, text]) => (
<div key={tag} className="border border-slate-200 rounded-xl p-4 bg-slate-50/50 space-y-2">
<div className="flex items-center justify-between">
<span className="font-mono text-xs font-bold bg-indigo-50 text-indigo-700 px-2 py-0.5 rounded">
{tag}
</span>
<span className="text-[11px] text-slate-400">قالب خودکار</span>
</div>
<p className="text-xs text-slate-700 leading-relaxed font-mono whitespace-pre-line bg-white p-2.5 rounded-lg border border-slate-100">
{typeof text === 'string' ? text : text.template}
</p>
</div>
))}
</div>
</div>
</div>
)}
{/* Section 4: Database Status */}
{activeSection === 'database' && (
<div className="bg-white rounded-2xl border border-slate-200 p-6 space-y-6">
<div className="flex items-center gap-3">
<div className="w-12 h-12 rounded-2xl bg-indigo-50 flex items-center justify-center text-indigo-600">
<HardDrive className="w-6 h-6" />
</div>
<div>
<h3 className="font-bold text-slate-900 text-base">پایگاه داده محلی SQLite (مستقل و پایدار)</h3>
<p className="text-xs text-slate-500">
تمام اطلاعات، اتاق‌ها، فعالیت‌ها و وظایف آنبوردینگ در دیسک محلی ذخیره می‌شوند.
</p>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div className="bg-slate-50 rounded-xl p-4 border border-slate-200">
<span className="text-xs text-slate-500 block mb-1">مسیر فایل دیتابیس</span>
<span className="font-mono text-xs font-bold text-slate-800 break-all" dir="ltr">
/workspace/personal/crm/prisma/crm.db
</span>
</div>
<div className="bg-slate-50 rounded-xl p-4 border border-slate-200">
<span className="text-xs text-slate-500 block mb-1">تعداد لیدهای فعال</span>
<span className="text-xl font-bold text-slate-800">{leadCount} مجموعه</span>
</div>
<div className="bg-slate-50 rounded-xl p-4 border border-slate-200">
<span className="text-xs text-slate-500 block mb-1">سیستم نگهداری و ORM</span>
<span className="text-sm font-bold text-emerald-700">Prisma Client v5.22 (بدون نیاز به شبکه)</span>
</div>
</div>
<div className="border-t border-slate-100 pt-4 flex items-center justify-between text-xs text-slate-500">
<span>برای پشتیبان‌گیری، کپی کردن فایل <code className="font-mono bg-slate-100 px-1 py-0.5 rounded">crm.db</code> یا گرفتن خروجی اکسل از نوار بالا کافی است.</span>
<button
onClick={onRefresh}
className="flex items-center gap-1 text-slate-700 hover:text-slate-900 font-semibold"
>
<RefreshCw className="w-3.5 h-3.5" />
بروزرسانی وضعیت دیتابیس
</button>
</div>
</div>
)}
</div>
);
}