import React, { useState } from "react"; import { base44 } from "@/api/base44Client"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { Link, useNavigate } from "react-router-dom"; import { createPageUrl } from "@/utils"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { ArrowLeft, Calendar, MapPin, Users, DollarSign, Send, Edit3, X, AlertTriangle } from "lucide-react"; import ShiftCard from "@/components/events/ShiftCard"; import OrderStatusBadge from "@/components/orders/OrderStatusBadge"; import { useToast } from "@/components/ui/use-toast"; import { format } from "date-fns"; const safeFormatDate = (dateString) => { if (!dateString) return "—"; try { return format(new Date(dateString), "MMMM d, yyyy"); } catch { return "—"; } }; export default function EventDetail() { const navigate = useNavigate(); const queryClient = useQueryClient(); const { toast } = useToast(); const [notifyDialog, setNotifyDialog] = useState(false); const [cancelDialog, setCancelDialog] = useState(false); const urlParams = new URLSearchParams(window.location.search); const eventId = urlParams.get("id"); const { data: user } = useQuery({ queryKey: ['current-user-event-detail'], queryFn: () => base44.auth.me(), }); const { data: allEvents, isLoading } = useQuery({ queryKey: ['events'], queryFn: () => base44.entities.Event.list(), initialData: [], }); const event = allEvents.find(e => e.id === eventId); // Cancel order mutation const cancelOrderMutation = useMutation({ mutationFn: () => base44.entities.Event.update(eventId, { status: "Canceled" }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['events'] }); queryClient.invalidateQueries({ queryKey: ['all-events-client'] }); toast({ title: "✅ Order Canceled", description: "Your order has been canceled successfully", }); setCancelDialog(false); navigate(createPageUrl("ClientOrders")); }, onError: () => { toast({ title: "❌ Failed to Cancel", description: "Could not cancel order. Please try again.", variant: "destructive", }); }, }); const handleNotifyStaff = async () => { const assignedStaff = event?.assigned_staff || []; for (const staff of assignedStaff) { try { await base44.integrations.Core.SendEmail({ to: staff.email || `${staff.staff_name}@example.com`, subject: `Shift Update: ${event.event_name}`, body: `You have an update for: ${event.event_name}\nDate: ${event.date}\nLocation: ${event.event_location || event.hub}\n\nPlease check the platform for details.` }); } catch (error) { console.error("Failed to send email:", error); } } toast({ title: "✅ Notifications Sent", description: `Notified ${assignedStaff.length} staff members`, }); setNotifyDialog(false); }; const isClient = user?.user_role === 'client' || event?.created_by === user?.email || event?.client_email === user?.email; const canEditOrder = () => { if (!event) return false; const eventDate = new Date(event.date); const now = new Date(); return isClient && event.status !== "Completed" && event.status !== "Canceled" && eventDate > now; }; const canCancelOrder = () => { if (!event) return false; return isClient && event.status !== "Completed" && event.status !== "Canceled"; }; if (isLoading) { return (
); } if (!event) { return (

Event not found

); } // Get shifts from event.shifts array (primary source) const eventShifts = event.shifts || []; return (
{/* Header */}

{event.event_name}

Order Details & Information

{canEditOrder() && ( )} {canCancelOrder() && ( )} {!isClient && event.assigned_staff?.length > 0 && ( )}
{/* Order Details Card */} Order Information

Event Date

{safeFormatDate(event.date)}

Location

{event.hub || event.event_location || "—"}

Staff Assigned

{event.assigned_staff?.length || 0} / {event.requested || 0}

Total Cost

${(event.total || 0).toLocaleString()}

{/* Client Information (if not client viewing) */} {!isClient && ( Client Information

Business Name

{event.business_name || "—"}

Contact Name

{event.client_name || "—"}

Contact Email

{event.client_email || "—"}

)} {/* Shifts - Using event.shifts array */}

Event Shifts & Staff Assignment

{eventShifts.length > 0 ? ( eventShifts.map((shift, idx) => ( )) ) : (

No shifts defined for this event

Add roles and staff requirements to get started

)}
{/* Notes */} {event.notes && ( Additional Notes

{event.notes}

)}
{/* Notify Staff Dialog */} Notify Assigned Staff Send notification to all {event.assigned_staff?.length || 0} assigned staff members about this event. {/* Cancel Order Dialog */} Cancel Order? Are you sure you want to cancel this order? This action cannot be undone and the vendor will be notified immediately.

{event.event_name}

{safeFormatDate(event.date)}
{event.hub || event.event_location}
); }