import React, { useState, useEffect } from 'react'; import { Head, router, Link } from '@inertiajs/react'; import AdminLayout from '@/Layouts/AdminLayout'; import { Users, Building2, ShieldAlert, DollarSign, LifeBuoy, Search, RefreshCw, Download, Calendar, Filter, FileSpreadsheet, Globe, X, FileText } from 'lucide-react'; export default function ReportsHub({ reportType = 'workers', startDate = '', endDate = '', status = 'all', search = '', headers = [], reportData = {}, nationalities = [], perPage = 10, nationality = 'all' }) { // Local filter state const [localStartDate, setLocalStartDate] = useState(startDate); const [localEndDate, setLocalEndDate] = useState(endDate); const [localStatus, setLocalStatus] = useState(status); const [localSearch, setLocalSearch] = useState(search); const [localNationality, setLocalNationality] = useState(nationality || 'all'); const [localPerPage, setLocalPerPage] = useState(perPage || 10); // Sync with props when props change (e.g. after tab switch) useEffect(() => { setLocalStartDate(startDate); setLocalEndDate(endDate); setLocalStatus(status); setLocalSearch(search); setLocalNationality(nationality || 'all'); setLocalPerPage(perPage || 10); }, [reportType, startDate, endDate, status, search, nationality, perPage]); const isPaginated = reportData && typeof reportData === 'object' && !Array.isArray(reportData); const dataList = isPaginated ? (reportData.data || []) : (reportData || []); const totalRecords = isPaginated ? (reportData.total || 0) : dataList.length; const handleTabChange = (type) => { router.get('/admin/analytics', { report_type: type, start_date: localStartDate, end_date: localEndDate, status: 'all', search: '', per_page: localPerPage }, { preserveState: false }); }; const applyFilters = () => { router.get('/admin/analytics', { report_type: reportType, start_date: localStartDate, end_date: localEndDate, status: localStatus, search: localSearch, nationality: reportType === 'workers' ? localNationality : undefined, per_page: localPerPage }, { preserveState: true, replace: true }); }; const resetFilters = () => { setLocalStartDate(''); setLocalEndDate(''); setLocalStatus('all'); setLocalSearch(''); setLocalNationality('all'); setLocalPerPage(10); router.get('/admin/analytics', { report_type: reportType, start_date: '', end_date: '', status: 'all', search: '', per_page: 10 }); }; const handlePerPageChange = (val) => { setLocalPerPage(val); router.get('/admin/analytics', { report_type: reportType, start_date: localStartDate, end_date: localEndDate, status: localStatus, search: localSearch, nationality: reportType === 'workers' ? localNationality : undefined, per_page: val, page: 1 }, { preserveState: true, replace: true }); }; const handleExportCSV = () => { const params = new URLSearchParams({ report_type: reportType, start_date: localStartDate, end_date: localEndDate, status: localStatus, search: localSearch, export: 'csv' }); if (reportType === 'workers' && localNationality !== 'all') { params.append('nationality', localNationality); } window.location.href = `/admin/analytics?${params.toString()}`; }; // Context-aware status list for dropdown const getStatusOptions = () => { switch (reportType) { case 'workers': return [ { value: 'all', label: 'All Statuses' }, { value: 'Available', label: 'Available' }, { value: 'hired', label: 'Hired' }, { value: 'inactive', label: 'Inactive' }, { value: 'suspended', label: 'Suspended' }, { value: 'banned', label: 'Banned' } ]; case 'employers': return [ { value: 'all', label: 'All Subscriptions' }, { value: 'active', label: 'Active Pass' }, { value: 'inactive', label: 'Inactive Pass' } ]; case 'payments': return [ { value: 'all', label: 'All Payments' }, { value: 'success', label: 'Success' }, { value: 'failed', label: 'Failed' }, { value: 'refunded', label: 'Refunded' } ]; case 'tickets': return [ { value: 'all', label: 'All Statuses' }, { value: 'Open', label: 'Open' }, { value: 'In-Progress', label: 'In-Progress' }, { value: 'Resolved', label: 'Resolved' }, { value: 'Closed', label: 'Closed' } ]; case 'safety': return [ { value: 'all', label: 'All Statuses' }, { value: 'Pending', label: 'Pending' }, { value: 'Resolved', label: 'Resolved' } ]; default: return [{ value: 'all', label: 'All Statuses' }]; } }; const tabs = [ { id: 'workers', label: 'Workers List', icon: Users, desc: 'Registration & status report' }, { id: 'employers', label: 'Employers List', icon: Building2, desc: 'Subscription & spending report' }, { id: 'payments', label: 'Payments Ledger', icon: DollarSign, desc: 'Revenue & transactions report' }, { id: 'tickets', label: 'Support Inquiries', icon: LifeBuoy, desc: 'Inquiries & voice notes report' }, { id: 'safety', label: 'Safety Moderation', icon: ShieldAlert, desc: 'Moderation & flagged user reports' }, ]; const renderCell = (val, cellIdx, type) => { const lowerVal = String(val).toLowerCase(); // Status Badges if (['active', 'available', 'success', 'yes', 'approved', 'resolved'].includes(lowerVal)) { return ( {val} ); } if (['pending', 'in-progress', 'under review', 'no'].includes(lowerVal)) { return ( {val} ); } if (['failed', 'inactive', 'suspended', 'banned', 'closed', 'rejected'].includes(lowerVal)) { return ( {val} ); } // ID Formats if (typeof val === 'string' && (val.startsWith('WRK-') || val.startsWith('EMP-') || val.startsWith('PAY-') || val.startsWith('TCK-') || val.startsWith('REP-'))) { return ( {val} ); } // Email style if (typeof val === 'string' && val.includes('@')) { return {val}; } return {val}; }; return (
{/* Header Title & Description */}
Management Portal

Platform Reports Portal

Gain direct access to database operational reports, track registrations, monitor logs, analyze payment histories, and export filtered tabular datasets.

{/* Report Module Navigation Tabs */}
{tabs.map((tab) => { const Icon = tab.icon; const isActive = reportType === tab.id; return ( ); })}
{/* Filter & Table Block */}
{/* Filter Bar */}
Filters & Query Parameters
{/* Start Date */}
setLocalStartDate(e.target.value)} />
{/* End Date */}
setLocalEndDate(e.target.value)} />
{/* Status filter */}
{/* Workers Tab specific filter: Nationality */} {reportType === 'workers' ? (
) : (
)} {/* Search Box */}
setLocalSearch(e.target.value)} />
{/* Action buttons */}
Matches found: {totalRecords} records
{/* Report Table View */}
{headers.map((hdr, idx) => ( ))} {dataList.map((row, idx) => ( {Object.values(row).map((val, cellIdx) => ( ))} ))} {dataList.length === 0 && ( )}
{hdr}
{renderCell(val, cellIdx, reportType)}
No Records Found There are no database records matching your current filter configuration.
{/* Pagination */}
{isPaginated && reportData.total > 0 ? ( `Showing ${reportData.from || 0} to ${reportData.to || 0} of ${reportData.total} records` ) : ( `Showing ${dataList.length} records` )} {/* Per Page Select */}
Per Page:
{isPaginated && reportData.last_page > 1 && (
Previous Page {reportData.current_page} of {reportData.last_page} Next
)}
); }