import React, { useState, useEffect } from "react"; import { UserRole, EventData, EventStatus, EventType } from "../types"; import { EventCard } from "../components/EventCard"; import { EventForm } from "../components/EventForm"; import { Button } from "../components/Button"; import { PlusCircle, Search, CheckCircle, Clock, Edit, Users, Map, Building2, } from "lucide-react"; import { useAuth } from "../contexts/AuthContext"; import { useData } from "../contexts/DataContext"; import { STATUS_COLORS } from "../constants"; interface DashboardProps { initialView?: "list" | "create"; } export const Dashboard: React.FC = ({ initialView = "list", }) => { const { user } = useAuth(); const { events, getEventsByRole, addEvent, updateEventStatus, assignPhotographer, getInstitutionById, } = useData(); const [view, setView] = useState<"list" | "create" | "edit" | "details">( initialView ); const [searchTerm, setSearchTerm] = useState(""); const [selectedEvent, setSelectedEvent] = useState(null); const [activeFilter, setActiveFilter] = useState("all"); // Reset view when initialView prop changes useEffect(() => { if (initialView) { setView(initialView); if (initialView === "create") setSelectedEvent(null); } }, [initialView]); // Guard Clause for basic security if (!user) return
Acesso Negado. Faça login.
; const myEvents = getEventsByRole(user.id, user.role); // Filter Logic const filteredEvents = myEvents.filter((e) => { const matchesSearch = e.name .toLowerCase() .includes(searchTerm.toLowerCase()); const matchesStatus = activeFilter === "all" || (activeFilter === "pending" && e.status === EventStatus.PENDING_APPROVAL) || (activeFilter === "active" && e.status !== EventStatus.ARCHIVED && e.status !== EventStatus.PENDING_APPROVAL); return matchesSearch && matchesStatus; }); const handleSaveEvent = (data: any) => { const isClient = user.role === UserRole.EVENT_OWNER; if (view === "edit" && selectedEvent) { const updatedEvent = { ...selectedEvent, ...data }; console.log("Updated", updatedEvent); setSelectedEvent(updatedEvent); setView("details"); } else { const initialStatus = isClient ? EventStatus.PENDING_APPROVAL : EventStatus.PLANNING; const newEvent: EventData = { ...data, id: Math.random().toString(36).substr(2, 9), status: initialStatus, checklist: [], ownerId: isClient ? user.id : "unknown", photographerIds: [], }; addEvent(newEvent); setView("list"); } }; const handleApprove = (e: React.MouseEvent, eventId: string) => { e.stopPropagation(); updateEventStatus(eventId, EventStatus.CONFIRMED); }; const handleOpenMaps = () => { if (!selectedEvent) return; if (selectedEvent.address.mapLink) { window.open(selectedEvent.address.mapLink, "_blank"); return; } const { street, number, city, state } = selectedEvent.address; const query = encodeURIComponent( `${street}, ${number}, ${city} - ${state}` ); window.open( `https://www.google.com/maps/search/?api=1&query=${query}`, "_blank" ); }; const handleManageTeam = () => { if (!selectedEvent) return; const newId = window.prompt( "ID do Fotógrafo para adicionar (ex: photographer-1):", "photographer-1" ); if (newId) { assignPhotographer(selectedEvent.id, newId); alert("Fotógrafo atribuído com sucesso!"); const updated = events.find((e) => e.id === selectedEvent.id); if (updated) setSelectedEvent(updated); } }; // --- RENDERS PER ROLE --- const renderRoleSpecificHeader = () => { if (user.role === UserRole.EVENT_OWNER) { return (

Meus Eventos

Acompanhe seus eventos ou solicite novos orçamentos.

); } if (user.role === UserRole.PHOTOGRAPHER) { return (

Eventos Designados

Gerencie seus trabalhos e visualize detalhes.

); } return (

Gestão Geral

Controle total de eventos, aprovações e equipes.

); }; const renderRoleSpecificActions = () => { if (user.role === UserRole.PHOTOGRAPHER) return null; const label = user.role === UserRole.EVENT_OWNER ? "Solicitar Novo Evento" : "Novo Evento"; return ( ); }; const renderAdminActions = (event: EventData) => { if ( user.role !== UserRole.BUSINESS_OWNER && user.role !== UserRole.SUPERADMIN ) return null; if (event.status === EventStatus.PENDING_APPROVAL) { return (
); } return null; }; // --- MAIN RENDER --- return (
{/* Header */} {view === "list" && (
{renderRoleSpecificHeader()} {renderRoleSpecificActions()}
)} {/* Content Switcher */} {view === "list" && (
{/* Filters Bar */}
setSearchTerm(e.target.value)} />
{(user.role === UserRole.BUSINESS_OWNER || user.role === UserRole.SUPERADMIN) && (
)}
{/* Grid */}
{filteredEvents.map((event) => (
{renderAdminActions(event)} { setSelectedEvent(event); setView("details"); }} />
))}
{filteredEvents.length === 0 && (

Nenhum evento encontrado com os filtros atuais.

)}
)} {(view === "create" || view === "edit") && ( setView(view === "edit" ? "details" : "list")} onSubmit={handleSaveEvent} initialData={view === "edit" ? selectedEvent : undefined} /> )} {view === "details" && selectedEvent && (
{/* Status Banner */} {selectedEvent.status === EventStatus.PENDING_APPROVAL && user.role === UserRole.EVENT_OWNER && (

Solicitação em Análise

Seu evento foi enviado e está aguardando aprovação da equipe Photum.

)}
Cover

{selectedEvent.name}

{/* Actions Toolbar */}
{(user.role === UserRole.BUSINESS_OWNER || user.role === UserRole.SUPERADMIN) && ( <> )} {user.role === UserRole.EVENT_OWNER && selectedEvent.status !== EventStatus.ARCHIVED && ( )}
{/* Institution Information */} {selectedEvent.institutionId && (() => { const institution = getInstitutionById( selectedEvent.institutionId ); if (institution) { return (

{institution.name}

{institution.type}

Contato

{institution.phone}

{institution.email}

{institution.address && (

Endereço

{institution.address.street},{" "} {institution.address.number}

{institution.address.city} -{" "} {institution.address.state}

)}
{institution.description && (

{institution.description}

)}
); } return null; })()}

Sobre o Evento

{selectedEvent.briefing || "Sem briefing detalhado."}

{selectedEvent.contacts.length > 0 && (

Contatos & Responsáveis

{selectedEvent.contacts.map((c, i) => (

{c.name}

{c.role}

{c.phone}

))}
)}

Status Atual

{selectedEvent.status}

Localização

{selectedEvent.address.street},{" "} {selectedEvent.address.number}

{selectedEvent.address.city} -{" "} {selectedEvent.address.state}

{selectedEvent.address.mapLink ? ( ) : ( )}
{(selectedEvent.photographerIds.length > 0 || user.role === UserRole.BUSINESS_OWNER) && (

Equipe Designada

{(user.role === UserRole.BUSINESS_OWNER || user.role === UserRole.SUPERADMIN) && ( )}
{selectedEvent.photographerIds.length > 0 ? (
{selectedEvent.photographerIds.map((id, idx) => (
))}
) : (

Nenhum profissional atribuído.

)}
)}
)}
); };