'use client'; import { useState, useEffect, useRef } from 'react'; import { LayoutDashboard, Users, CreditCard, Package, Settings, Shield, TrendingUp, Bell, LogOut, User, CheckCircle, AlertCircle } from 'lucide-react'; import { useRouter } from 'next/navigation'; import AdminDashboard from '@/components/AdminDashboard'; import Tenants from '@/components/Tenants'; import Plans from '@/components/Plans'; import Payments from '@/components/Payments'; import AdminSettings from '@/components/AdminSettings'; import ToastContainer from '@/components/Toast'; const navItems = [ { section: 'Visão Geral' }, { id: 'dashboard', label: 'Dashboard', icon: LayoutDashboard }, { section: 'Gestão' }, { id: 'tenants', label: 'Assinantes', icon: Users }, { id: 'plans', label: 'Planos', icon: Package }, { id: 'payments', label: 'Pagamentos', icon: CreditCard }, { section: 'Sistema' }, { id: 'settings', label: 'Configurações', icon: Settings }, ]; const pages: Record = { dashboard: { title: 'Dashboard', subtitle: 'Visão geral das assinaturas' }, tenants: { title: 'Assinantes', subtitle: 'Gerenciar estabelecimentos' }, plans: { title: 'Planos', subtitle: 'Gerenciar planos de assinatura' }, payments: { title: 'Pagamentos', subtitle: 'Histórico de cobranças' }, settings: { title: 'Configurações', subtitle: 'Configurações do sistema' }, }; export default function AdminPage() { const router = useRouter(); const [currentPage, setCurrentPage] = useState('dashboard'); const [isLoading, setIsLoading] = useState(true); const [showNotifications, setShowNotifications] = useState(false); const [showProfile, setShowProfile] = useState(false); const [notifications, setNotifications] = useState([]); const [adminName, setAdminName] = useState('Super Admin'); const [adminEmail, setAdminEmail] = useState('admin@agendapro.com'); const notifRef = useRef(null); const profileRef = useRef(null); const fetchNotifications = async () => { try { const res = await fetch('/api/notifications'); if (res.ok) { const data = await res.json(); setNotifications(data); } } catch (err) { console.error('Error fetching admin notifications:', err); } }; useEffect(() => { // Auth Check const token = localStorage.getItem('agendapro_admin_token'); if (!token) { router.push('/login'); } else { setAdminName(localStorage.getItem('agendapro_admin_name') || 'Super Admin'); setAdminEmail(localStorage.getItem('agendapro_admin_email') || 'admin@agendapro.com'); setIsLoading(false); } fetchNotifications(); const interval = setInterval(fetchNotifications, 15000); // Auto-refresh every 15s const handleClickOutside = (e: MouseEvent) => { if (notifRef.current && !notifRef.current.contains(e.target as Node)) setShowNotifications(false); if (profileRef.current && !profileRef.current.contains(e.target as Node)) setShowProfile(false); }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); clearInterval(interval); }; }, [router]); const playNotificationSound = () => { try { const audioCtx = new (window.AudioContext || (window as any).webkitAudioContext)(); const oscillator = audioCtx.createOscillator(); const gainNode = audioCtx.createGain(); oscillator.type = 'sine'; oscillator.frequency.setValueAtTime(880, audioCtx.currentTime); // A5 oscillator.frequency.exponentialRampToValueAtTime(1760, audioCtx.currentTime + 0.1); // A6 gainNode.gain.setValueAtTime(0.1, audioCtx.currentTime); gainNode.gain.exponentialRampToValueAtTime(0.01, audioCtx.currentTime + 0.3); oscillator.connect(gainNode); gainNode.connect(audioCtx.destination); oscillator.start(); oscillator.stop(audioCtx.currentTime + 0.3); } catch (e) { console.log('Audio not supported or blocked'); } }; const handleNotificationClick = () => { if (!showNotifications) { const unreadCount = notifications.filter(n => !n.read).length; if (unreadCount > 0) { playNotificationSound(); } } setShowNotifications(!showNotifications); setShowProfile(false); }; const handleProfileClick = () => { setShowProfile(!showProfile); setShowNotifications(false); }; const markAllAsRead = async () => { setNotifications(notifications.map(n => ({ ...n, read: true }))); try { await fetch('/api/notifications', { method: 'PUT' }); } catch (err) { console.error(err); } }; const clearAllNotifications = async () => { setNotifications([]); try { await fetch('/api/notifications', { method: 'DELETE' }); } catch (err) { console.error(err); } }; const logout = () => { localStorage.removeItem('agendapro_admin_token'); localStorage.removeItem('agendapro_admin_email'); localStorage.removeItem('agendapro_admin_name'); router.push('/login'); }; if (isLoading) { return
Carregando painel...
; } const unreadCount = notifications.filter(n => !n.read).length; const adminInitials = adminName.split(' ').map(n => n[0]).join('').substring(0, 2).toUpperCase(); const renderPage = () => { switch (currentPage) { case 'dashboard': return ; case 'tenants': return ; case 'plans': return ; case 'payments': return ; case 'settings': return ; default: return ; } }; return (

{pages[currentPage]?.title}

{pages[currentPage]?.subtitle}

{showNotifications && (

Notificações

{unreadCount > 0 && ( )} {notifications.length > 0 && ( )}
{notifications.map(n => (
{n.type === 'success' ? : n.type === 'warning' ? : }
{n.title}
{n.message}
{n.time}
))}
)}
{adminInitials}
{showProfile && (
{adminName}
{adminEmail}
)}
{renderPage()}
); }