'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { useLanguage } from '@/context/LanguageContext'; import { useAuth } from '@/context/AuthContext'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import { dashboardApi, DashboardSummary, NextEventInfo, UserTicket, UserPayment } from '@/lib/api'; import { formatDateLong, formatTime } from '@/lib/utils'; import toast from 'react-hot-toast'; import Link from 'next/link'; import { socialConfig, getWhatsAppUrl, getInstagramUrl, getTelegramUrl } from '@/lib/socialLinks'; // Tab components import TicketsTab from './components/TicketsTab'; import PaymentsTab from './components/PaymentsTab'; import ProfileTab from './components/ProfileTab'; import SecurityTab from './components/SecurityTab'; type Tab = 'overview' | 'tickets' | 'payments' | 'profile' | 'security'; export default function DashboardPage() { const router = useRouter(); const { t, locale: language } = useLanguage(); const { user, isLoading: authLoading, token } = useAuth(); const [activeTab, setActiveTab] = useState('overview'); const [summary, setSummary] = useState(null); const [nextEvent, setNextEvent] = useState(null); const [tickets, setTickets] = useState([]); const [payments, setPayments] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { if (!authLoading && !user) { router.push('/login'); return; } if (user && token) { loadDashboardData(); } }, [user, authLoading, token]); const loadDashboardData = async () => { setLoading(true); try { const [summaryRes, nextEventRes, ticketsRes, paymentsRes] = await Promise.all([ dashboardApi.getSummary(), dashboardApi.getNextEvent(), dashboardApi.getTickets(), dashboardApi.getPayments(), ]); setSummary(summaryRes.summary); setNextEvent(nextEventRes.nextEvent); setTickets(ticketsRes.tickets); setPayments(paymentsRes.payments); } catch (error: any) { console.error('Failed to load dashboard:', error); toast.error('Failed to load dashboard data'); } finally { setLoading(false); } }; const tabs: { id: Tab; label: string }[] = [ { id: 'overview', label: language === 'es' ? 'Resumen' : 'Overview' }, { id: 'tickets', label: language === 'es' ? 'Mis Entradas' : 'My Tickets' }, { id: 'payments', label: language === 'es' ? 'Pagos y Facturas' : 'Payments & Invoices' }, { id: 'profile', label: language === 'es' ? 'Perfil' : 'Profile' }, { id: 'security', label: language === 'es' ? 'Seguridad' : 'Security' }, ]; if (authLoading || !user) { return (
); } const formatDate = (dateStr: string) => formatDateLong(dateStr, language as 'en' | 'es'); const fmtTime = (dateStr: string) => formatTime(dateStr, language as 'en' | 'es'); return (
{/* Welcome Header */}

{language === 'es' ? `Hola, ${user.name}!` : `Welcome, ${user.name}!`}

{summary && (

{language === 'es' ? `Miembro desde hace ${summary.user.membershipDays} días` : `Member for ${summary.user.membershipDays} days` }

)}
{/* Tab Navigation */}
{/* Tab Content */} {loading ? (
) : ( <> {activeTab === 'overview' && ( )} {activeTab === 'tickets' && ( )} {activeTab === 'payments' && ( )} {activeTab === 'profile' && ( )} {activeTab === 'security' && ( )} )}
); } // Overview Tab Component function OverviewTab({ summary, nextEvent, tickets, language, formatDate, formatTime, }: { summary: DashboardSummary | null; nextEvent: NextEventInfo | null; tickets: UserTicket[]; language: string; formatDate: (date: string) => string; formatTime: (date: string) => string; }) { return (
{/* Stats Cards */} {summary && (
{summary.stats.totalTickets}
{language === 'es' ? 'Total Entradas' : 'Total Tickets'}
{summary.stats.confirmedTickets}
{language === 'es' ? 'Confirmadas' : 'Confirmed'}
{summary.stats.upcomingEvents}
{language === 'es' ? 'Próximos' : 'Upcoming'}
{summary.stats.pendingPayments}
{language === 'es' ? 'Pagos Pendientes' : 'Pending Payments'}
)} {/* Next Event Card */} {nextEvent ? (

{language === 'es' ? 'Tu Próximo Evento' : 'Your Next Event'}

{nextEvent.event.bannerUrl && (
{nextEvent.event.title}
)}

{language === 'es' && nextEvent.event.titleEs ? nextEvent.event.titleEs : nextEvent.event.title}

{language === 'es' ? 'Fecha:' : 'Date:'} {' '} {formatDate(nextEvent.event.startDatetime)}

{language === 'es' ? 'Hora:' : 'Time:'} {' '} {formatTime(nextEvent.event.startDatetime)}

{language === 'es' ? 'Lugar:' : 'Location:'} {' '} {nextEvent.event.location}

{language === 'es' ? 'Estado:' : 'Status:'} {' '} {nextEvent.payment?.status === 'paid' ? (language === 'es' ? 'Pagado' : 'Paid') : (language === 'es' ? 'Pendiente' : 'Pending')}

{nextEvent.event.locationUrl && ( )}
) : (

{language === 'es' ? 'No tienes eventos próximos' : 'You have no upcoming events'}

)} {/* Recent Tickets */} {tickets.length > 0 && (

{language === 'es' ? 'Entradas Recientes' : 'Recent Tickets'}

{tickets.slice(0, 3).map((ticket) => (

{language === 'es' && ticket.event?.titleEs ? ticket.event.titleEs : ticket.event?.title || 'Event'}

{ticket.event?.startDatetime ? formatDate(ticket.event.startDatetime) : ''}

{ticket.status}
))}
{tickets.length > 3 && (
)}
)} {/* Community Links */}

{language === 'es' ? 'Comunidad' : 'Community'}

{getWhatsAppUrl(socialConfig.whatsapp) && (
WhatsApp
)} {getInstagramUrl(socialConfig.instagram) && (
Instagram
)} {getTelegramUrl(socialConfig.telegram) && (
Telegram
)}
); }