"use client"; import { Fragment, useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import { Menu, Transition } from '@headlessui/react'; import ConfirmDialog from '@/components/layout/ConfirmDialog'; import { useToast } from '@/components/layout/ToastContext'; import Pagination from '@/components/layout/Pagination'; import { useCRMFilter } from '@/contexts/CRMFilterContext'; import { SolutionGuard } from '@/components/auth/SolutionGuard'; import SearchableSelect from '@/components/form/SearchableSelect'; import { ListBulletIcon, TrashIcon, PencilIcon, EllipsisVerticalIcon, MagnifyingGlassIcon, PlusIcon, XMarkIcon, UserGroupIcon, EyeIcon, CalendarIcon, RectangleStackIcon, } from '@heroicons/react/24/outline'; interface List { id: string; tenant_id: string; customer_id: string; customer_name: string; funnel_id?: string; name: string; description: string; color: string; customer_count: number; lead_count: number; created_at: string; updated_at: string; } interface Funnel { id: string; name: string; } interface Customer { id: string; name: string; company: string; } const COLORS = [ { name: 'Azul', value: '#3B82F6' }, { name: 'Verde', value: '#10B981' }, { name: 'Roxo', value: '#8B5CF6' }, { name: 'Rosa', value: '#EC4899' }, { name: 'Laranja', value: '#F97316' }, { name: 'Amarelo', value: '#EAB308' }, { name: 'Vermelho', value: '#EF4444' }, { name: 'Cinza', value: '#6B7280' }, ]; function CampaignsContent() { const router = useRouter(); const toast = useToast(); const { selectedCustomerId } = useCRMFilter(); console.log('📢 CampaignsPage render, selectedCustomerId:', selectedCustomerId); const [lists, setLists] = useState([]); const [customers, setCustomers] = useState([]); const [funnels, setFunnels] = useState([]); const [loading, setLoading] = useState(true); const [isModalOpen, setIsModalOpen] = useState(false); const [editingList, setEditingList] = useState(null); const [confirmOpen, setConfirmOpen] = useState(false); const [listToDelete, setListToDelete] = useState(null); const [searchTerm, setSearchTerm] = useState(''); const [currentPage, setCurrentPage] = useState(1); const itemsPerPage = 10; const [formData, setFormData] = useState({ name: '', description: '', color: COLORS[0].value, customer_id: '', funnel_id: '', }); useEffect(() => { console.log('🔄 CampaignsPage useEffect triggered by selectedCustomerId:', selectedCustomerId); fetchLists(); fetchCustomers(); fetchFunnels(); }, [selectedCustomerId]); const fetchFunnels = async () => { try { const response = await fetch('/api/crm/funnels', { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); if (response.ok) { const data = await response.json(); setFunnels(data.funnels || []); } } catch (error) { console.error('Error fetching funnels:', error); } }; const fetchCustomers = async () => { try { const response = await fetch('/api/crm/customers', { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}`, }, }); if (response.ok) { const data = await response.json(); setCustomers(data.customers || []); } } catch (error) { console.error('Error fetching customers:', error); } }; const fetchLists = async () => { try { setLoading(true); const url = selectedCustomerId ? `/api/crm/lists?customer_id=${selectedCustomerId}` : '/api/crm/lists'; console.log(`📊 Fetching campaigns from: ${url}`); const response = await fetch(url, { cache: 'no-store', headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}`, }, }); if (response.ok) { const data = await response.json(); console.log('📊 Campaigns data received:', data); setLists(data.lists || []); } } catch (error) { console.error('Error fetching campaigns:', error); } finally { setLoading(false); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const url = editingList ? `/api/crm/lists/${editingList.id}` : '/api/crm/lists'; const method = editingList ? 'PUT' : 'POST'; try { const response = await fetch(url, { method, headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}`, 'Content-Type': 'application/json', }, body: JSON.stringify(formData), }); if (response.ok) { toast.success( editingList ? 'Campanha atualizada' : 'Campanha criada', editingList ? 'A campanha foi atualizada com sucesso.' : 'A nova campanha foi criada com sucesso.' ); fetchLists(); handleCloseModal(); } else { const error = await response.json(); toast.error('Erro', error.message || 'Não foi possível salvar a campanha.'); } } catch (error) { console.error('Error saving campaign:', error); toast.error('Erro', 'Ocorreu um erro ao salvar a campanha.'); } }; const handleNewCampaign = () => { setEditingList(null); setFormData({ name: '', description: '', color: COLORS[0].value, customer_id: selectedCustomerId || '', funnel_id: '', }); setIsModalOpen(true); }; const handleEdit = (list: List) => { setEditingList(list); setFormData({ name: list.name, description: list.description, color: list.color, customer_id: list.customer_id || '', funnel_id: list.funnel_id || '', }); setIsModalOpen(true); }; const handleDeleteClick = (id: string) => { setListToDelete(id); setConfirmOpen(true); }; const handleConfirmDelete = async () => { if (!listToDelete) return; try { const response = await fetch(`/api/crm/lists/${listToDelete}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}`, }, }); if (response.ok) { setLists(lists.filter(l => l.id !== listToDelete)); toast.success('Campanha excluída', 'A campanha foi excluída com sucesso.'); } else { toast.error('Erro ao excluir', 'Não foi possível excluir a campanha.'); } } catch (error) { console.error('Error deleting campaign:', error); toast.error('Erro ao excluir', 'Ocorreu um erro ao excluir a campanha.'); } finally { setConfirmOpen(false); setListToDelete(null); } }; const handleCloseModal = () => { setIsModalOpen(false); setEditingList(null); setFormData({ name: '', description: '', color: COLORS[0].value, customer_id: '', funnel_id: '', }); }; const filteredLists = lists.filter((list) => { const searchLower = searchTerm.toLowerCase(); return ( (list.name?.toLowerCase() || '').includes(searchLower) || (list.description?.toLowerCase() || '').includes(searchLower) ); }); const totalPages = Math.ceil(filteredLists.length / itemsPerPage); const paginatedLists = filteredLists.slice( (currentPage - 1) * itemsPerPage, currentPage * itemsPerPage ); return (
{/* Header */}

Campanhas

Organize seus leads e rastreie a origem de cada um

{/* Search */}
setSearchTerm(e.target.value)} />
{/* Table */} {loading ? (
) : filteredLists.length === 0 ? (

Nenhuma campanha encontrada

{searchTerm ? 'Nenhuma campanha corresponde à sua busca.' : 'Comece criando sua primeira campanha.'}

) : (
{paginatedLists.map((list) => ( router.push(`/crm/campanhas/${list.id}`)} className="group hover:bg-zinc-50 dark:hover:bg-zinc-800/50 transition-colors cursor-pointer" > ))}
Campanha Cliente Vinculado Leads Criada em Ações
{list.name}
{list.description && (
{list.description}
)}
{list.customer_name ? ( {list.customer_name} ) : ( Geral )}
{list.lead_count || 0}
{new Date(list.created_at).toLocaleDateString('pt-BR')}
e.stopPropagation()}>
{({ active }) => ( )}
{({ active }) => ( )}
)} {/* Modal */} {isModalOpen && (

{editingList ? 'Editar Campanha' : 'Nova Campanha'}

{editingList ? 'Atualize as informações da campanha.' : 'Crie uma nova campanha para organizar seus leads.'}

({ id: c.id, name: c.name, subtitle: c.company || undefined }))} value={formData.customer_id} onChange={(value) => setFormData({ ...formData, customer_id: value || '' })} placeholder="Nenhum cliente (Geral)" emptyText="Nenhum cliente encontrado" helperText="Vincule esta campanha a um cliente específico para melhor organização." />
setFormData({ ...formData, name: e.target.value })} placeholder="Ex: Black Friday 2025" required className="w-full px-3 py-2.5 border border-zinc-200 dark:border-zinc-700 rounded-lg bg-white dark:bg-zinc-900 text-zinc-900 dark:text-white placeholder-zinc-400 focus:outline-none focus:ring-2 focus:ring-[var(--brand-color)] focus:border-transparent transition-all" />