import React, { useState, useMemo } from 'react'; import { EventData, EventStatus, UserRole } from '../types'; import { Calendar, MapPin, Users, CheckCircle, Clock, ArrowUpDown, ArrowUp, ArrowDown } from 'lucide-react'; import { STATUS_COLORS } from '../constants'; interface EventTableProps { events: EventData[]; onEventClick: (event: EventData) => void; onApprove?: (e: React.MouseEvent, eventId: string) => void; userRole: UserRole; } type SortField = 'name' | 'type' | 'date' | 'time' | 'city' | 'location' | 'status'; type SortOrder = 'asc' | 'desc' | null; export const EventTable: React.FC = ({ events, onEventClick, onApprove, userRole }) => { const canApprove = (userRole === UserRole.BUSINESS_OWNER || userRole === UserRole.SUPERADMIN); const [sortField, setSortField] = useState(null); const [sortOrder, setSortOrder] = useState(null); const handleSort = (field: SortField) => { if (sortField === field) { // Se já está ordenando por este campo, alterna a ordem if (sortOrder === 'asc') { setSortOrder('desc'); } else if (sortOrder === 'desc') { setSortOrder(null); setSortField(null); } } else { // Novo campo, começa com ordem ascendente setSortField(field); setSortOrder('asc'); } }; const sortedEvents = useMemo(() => { if (!sortField || !sortOrder) { return events; } const sorted = [...events].sort((a, b) => { let aValue: any; let bValue: any; switch (sortField) { case 'name': aValue = a.name.toLowerCase(); bValue = b.name.toLowerCase(); break; case 'type': aValue = a.type.toLowerCase(); bValue = b.type.toLowerCase(); break; case 'date': aValue = new Date(a.date + 'T00:00:00').getTime(); bValue = new Date(b.date + 'T00:00:00').getTime(); break; case 'time': aValue = a.time; bValue = b.time; break; case 'city': aValue = a.address.city.toLowerCase(); bValue = b.address.city.toLowerCase(); break; case 'location': aValue = `${a.address.city}, ${a.address.state}`.toLowerCase(); bValue = `${b.address.city}, ${b.address.state}`.toLowerCase(); break; case 'status': aValue = a.status.toLowerCase(); bValue = b.status.toLowerCase(); break; default: return 0; } if (aValue < bValue) return sortOrder === 'asc' ? -1 : 1; if (aValue > bValue) return sortOrder === 'asc' ? 1 : -1; return 0; }); return sorted; }, [events, sortField, sortOrder]); const getSortIcon = (field: SortField) => { if (sortField !== field) { return ; } if (sortOrder === 'asc') { return ; } return ; }; const formatDate = (date: string) => { const eventDate = new Date(date + 'T00:00:00'); return eventDate.toLocaleDateString('pt-BR', { day: '2-digit', month: '2-digit', year: 'numeric' }); }; const getStatusDisplay = (status: EventStatus) => { const statusLabels: Record = { [EventStatus.PENDING_APPROVAL]: 'Pendente', [EventStatus.CONFIRMED]: 'Confirmado', [EventStatus.PLANNING]: 'Planejamento', [EventStatus.IN_PROGRESS]: 'Em Andamento', [EventStatus.COMPLETED]: 'Concluído', [EventStatus.ARCHIVED]: 'Arquivado', }; return statusLabels[status] || status; }; return (
{canApprove && ( )} {sortedEvents.map((event) => ( onEventClick(event)} className="hover:bg-gray-50 cursor-pointer transition-colors" > {canApprove && ( )} ))}
Ações handleSort('name')} >
Nome do Evento {getSortIcon('name')}
handleSort('type')} >
Tipo {getSortIcon('type')}
handleSort('date')} >
Data {getSortIcon('date')}
handleSort('time')} >
Horário {getSortIcon('time')}
handleSort('city')} >
Cidade {getSortIcon('city')}
handleSort('location')} >
Local {getSortIcon('location')}
Contatos Equipe handleSort('status')} >
Status {getSortIcon('status')}
e.stopPropagation()}> {event.status === EventStatus.PENDING_APPROVAL && ( )}
{event.name}
{event.type}
{formatDate(event.date)}
{event.time}
{event.address.city}
{event.address.city}, {event.address.state}
{event.contacts.length}
{event.photographerIds.length > 0 ? (
{event.photographerIds.slice(0, 3).map((id, idx) => (
))} {event.photographerIds.length > 3 && (
+{event.photographerIds.length - 3}
)}
) : ( - )}
{getStatusDisplay(event.status)}
{sortedEvents.length === 0 && (

Nenhum evento encontrado.

)}
); };