import React, { useState, useEffect } from 'react'; import { Head, useForm } from '@inertiajs/react'; import EmployerLayout from '../../Layouts/EmployerLayout'; import { useTranslation } from '../../lib/LanguageContext'; import { Megaphone, Send, CheckCircle2, Users, Search, Calendar, Target, Filter, ChevronRight, MessageSquare, Info, Heart, MapPin, Clock, Sparkles, BellRing, ChevronDown, ChevronUp, Gift, Plus } from 'lucide-react'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, DialogFooter } from '@/components/ui/dialog'; const hoursList = [ '12:00', '12:30', '01:00', '01:30', '02:00', '02:30', '03:00', '03:30', '04:00', '04:30', '05:00', '05:30', '06:00', '06:30', '07:00', '07:30', '08:00', '08:30', '09:00', '09:30', '10:00', '10:30', '11:00', '11:30' ]; export default function Announcements({ initialAnnouncements }) { const { t } = useTranslation(); const [isDialogOpen, setIsDialogOpen] = useState(false); const [searchTerm, setSearchTerm] = useState(''); const [toastMessage, setToastMessage] = useState(null); const [toastSub, setToastSub] = useState(null); const [announcements, setAnnouncements] = useState(initialAnnouncements || []); // Time picker states const [startTimeHour, setStartTimeHour] = useState('09:00'); const [startTimeAmpm, setStartTimeAmpm] = useState('AM'); const [endTimeHour, setEndTimeHour] = useState('04:00'); const [endTimeAmpm, setEndTimeAmpm] = useState('PM'); const [isStartPickerOpen, setIsStartPickerOpen] = useState(false); const [isEndPickerOpen, setIsEndPickerOpen] = useState(false); const [expandedCards, setExpandedCards] = useState({}); const [statusFilter, setStatusFilter] = useState('All'); useEffect(() => { setAnnouncements(initialAnnouncements || []); }, [initialAnnouncements]); const { data: newAnnouncement, setData: setNewAnnouncement, post, processing, reset } = useForm({ title: '', content: '', audience: 'Charity', type: 'Charity', provided_items: '', event_date: '', event_time: '09:00 AM - 04:00 PM', location_details: '', location_pin: '' }); const updateEventTime = (sh, sa, eh, ea) => { setNewAnnouncement({ ...newAnnouncement, event_time: `${sh} ${sa} - ${eh} ${ea}` }); }; const toggleExpand = (id) => { setExpandedCards(prev => ({ ...prev, [id]: !prev[id] })); }; const showToast = (message, subMessage = null) => { setToastMessage(message); setToastSub(subMessage); setTimeout(() => { setToastMessage(null); setToastSub(null); }, 5000); }; const handleCreate = (e) => { e.preventDefault(); post('/employer/announcements/create', { preserveScroll: true, onSuccess: () => { reset(); setStartTimeHour('09:00'); setStartTimeAmpm('AM'); setEndTimeHour('04:00'); setEndTimeAmpm('PM'); setIsDialogOpen(false); showToast( t('community_charity_event_posted', 'COMMUNITY CHARITY EVENT POSTED!'), t('community_charity_event_posted_desc', 'Instant Push Notification sent to all workers in Dubai! Morning-of reminder notification scheduled successfully.') ); } }); }; const filteredAnnouncements = announcements.filter(ann => { const matchesSearch = ann.title.toLowerCase().includes(searchTerm.toLowerCase()) || ann.content.toLowerCase().includes(searchTerm.toLowerCase()); const matchesStatus = statusFilter === 'All' || ann.status.toLowerCase() === statusFilter.toLowerCase(); return matchesSearch && matchesStatus; }); return ( {toastMessage && (
{toastMessage}
{toastSub &&

{toastSub}

}
)}
{/* Community Drive & Charity Banner */}
{t('dubai_community_support_drives', 'Dubai Community Support Drives')}

{t('free_medical_checks_title', 'Free Medical Checks, Food Drives & Support Services')}

{t('free_medical_checks_desc', 'Organizing a support program? Share community campaigns directly. The platform will automatically push an instant pop-up notification to workers, followed by a morning-of event reminder to keep attendance strong.')}

{t('post_charity_event', 'Post Charity Event')} {t('broadcast_support_program_desc', 'Broadcast a support program, medical camp, or distribution drive to the worker community.')}
{t('charity_drive_metadata', 'Charity Drive Metadata (Dubai Support)')}
setNewAnnouncement({ ...newAnnouncement, event_date: e.target.value })} className="w-full h-11 px-3.5 bg-white border border-slate-200 rounded-xl text-xs font-bold focus:ring-2 focus:ring-[#185FA5]/20 outline-none" />
{isStartPickerOpen && (
setIsStartPickerOpen(false)} /> )}
{isStartPickerOpen && (
{hoursList.map(h => ( ))}
{['AM', 'PM'].map(ampm => ( ))}
)}
{isEndPickerOpen && (
setIsEndPickerOpen(false)} /> )}
{isEndPickerOpen && (
{hoursList.map(h => ( ))}
{['AM', 'PM'].map(ampm => ( ))}
)}
setNewAnnouncement({ ...newAnnouncement, provided_items: e.target.value })} className="w-full px-3.5 py-2.5 bg-white border border-slate-200 rounded-xl text-xs font-bold focus:ring-2 focus:ring-[#185FA5]/20 outline-none" />
setNewAnnouncement({ ...newAnnouncement, location_details: e.target.value })} className="w-full px-3.5 py-2.5 bg-white border border-slate-200 rounded-xl text-xs font-bold focus:ring-2 focus:ring-[#185FA5]/20 outline-none mb-2" /> setNewAnnouncement({ ...newAnnouncement, location_pin: e.target.value })} className="w-full px-3.5 py-2.5 bg-white border border-slate-200 rounded-xl text-xs font-bold focus:ring-2 focus:ring-[#185FA5]/20 outline-none" />
setNewAnnouncement({ ...newAnnouncement, title: e.target.value })} className="w-full px-5 py-3.5 bg-slate-50 border-none rounded-2xl text-sm font-bold focus:ring-2 focus:ring-[#185FA5]/20 transition-all outline-none" placeholder={t('title_placeholder', 'e.g. Free Dental checkup by Emirates Charity')} />
setSearchTerm(e.target.value)} className="w-full pl-12 pr-4 py-3 bg-slate-50 border-none rounded-2xl text-sm font-bold focus:ring-2 focus:ring-[#185FA5]/20 transition-all outline-none" />
{/* Status Filters */}
{['All', 'Pending', 'Approved', 'Rejected'].map((status) => ( ))}
{filteredAnnouncements.length === 0 ? (

{t('no_announcements_title', 'No Announcements')}

{t('no_announcements_desc', "You haven't posted any announcements yet")}

) : ( filteredAnnouncements.map((ann) => { const details = ann.charityDetails; const isExpanded = !!expandedCards[ann.id]; const statusColorClass = ann.status === 'approved' ? 'border-l-emerald-500' : ann.status === 'rejected' ? 'border-l-rose-500' : 'border-l-amber-500'; return (
{/* Top Card Area */}

{ann.title}

{ann.status === 'approved' ? t('status_approved', 'APPROVED') : (ann.status === 'rejected' ? t('status_rejected', 'REJECTED') : t('status_pending', 'PENDING'))}
{/* Collapsible Content */}

{ann.content}

{ann.content.length > 90 && ( )} {ann.status === 'rejected' && ann.remarks && (
{t('rejection_remarks_label', 'Remarks')}: {ann.remarks}
)}
{/* Details metadata row (compact icons) */} {details && (
{details.provided_items}
{details.event_date}
{details.location_details}
)}
{/* Bottom Card Footer */}
{t('posted_label', 'Posted')}: {ann.created_at} {details?.location_pin && ( {t('view_on_map', 'View on Map')} )}
); }) )}
); }