Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/special_reports.php

<?php
require_once '../config/config.php';

// Check if user is logged in
if (!isLoggedIn()) {
    redirect('login.php');
}

// Handle export requests
if (isset($_GET['export']) && isset($_GET['format'])) {
    $format = $_GET['format'];
    $district_id = $_GET['district_id'] ?? '';
    $assembly_id = $_GET['assembly_id'] ?? '';
    $program_id = $_GET['program_id'] ?? '';
    
    // Get the data for export
    $db = new Database();
    $conn = $db->getConnection();
    $export_data = getSpecialReportData($conn, $district_id, $assembly_id, $program_id);
    
    if (empty($export_data)) {
        $_SESSION['error_message'] = 'No data available for export with the selected filters.';
        redirect('special_reports.php');
    }
    
    // Export based on format
    switch ($format) {
        case 'csv':
            exportToCSV($export_data);
            break;
        case 'excel':
            exportToExcel($export_data);
            break;
        case 'pdf':
            exportToPDF($export_data);
            break;
        default:
            $_SESSION['error_message'] = 'Invalid export format.';
            redirect('special_reports.php');
    }
    exit;
}

// Get database connection
$db = new Database();
$conn = $db->getConnection();

// Get filter values
$district_id = $_GET['district_id'] ?? '';
$assembly_id = $_GET['assembly_id'] ?? '';
$program_id = $_GET['program_id'] ?? '';

// Get districts for filter dropdown
$districts_query = "SELECT id, name FROM locations WHERE type = 'district' AND is_active = 1 ORDER BY name";
$districts_stmt = $conn->prepare($districts_query);
$districts_stmt->execute();
$districts = $districts_stmt->fetchAll();

// Get assemblies for filter dropdown
$assemblies_query = "SELECT id, name, parent_id FROM locations WHERE type = 'assembly' AND is_active = 1 ORDER BY name";
$assemblies_stmt = $conn->prepare($assemblies_query);
$assemblies_stmt->execute();
$assemblies = $assemblies_stmt->fetchAll();

// Get programs for filter dropdown
$programs_query = "SELECT id, name FROM programs WHERE is_active = 1 ORDER BY name";
$programs_stmt = $conn->prepare($programs_query);
$programs_stmt->execute();
$programs = $programs_stmt->fetchAll();

// Get special report data
$report_data = getSpecialReportData($conn, $district_id, $assembly_id, $program_id);

// Functions for data processing and export
function getSpecialReportData($conn, $district_id = '', $assembly_id = '', $program_id = '') {
    
    // Build the query with filters
    $where_conditions = [];
    $params = [];
    
    if (!empty($district_id)) {
        $where_conditions[] = "ar.district_id = ?";
        $params[] = $district_id;
    }
    
    if (!empty($assembly_id)) {
        $where_conditions[] = "ar.assembly_id = ?";
        $params[] = $assembly_id;
    }
    
    if (!empty($program_id)) {
        $where_conditions[] = "ar.program_id = ?";
        $params[] = $program_id;
    }
    
    $where_clause = !empty($where_conditions) ? 'WHERE ' . implode(' AND ', $where_conditions) : '';
    
    $query = "
        SELECT 
            p.id as program_id,
            p.name as program_name,
            p.start_date,
            p.end_date,
            ar.full_name,
            ar.email,
            ar.telephone,
            ar.officer_type,
            ld.name as district_name,
            la.name as assembly_name,
            COUNT(ar.id) as attendance_count,
            MIN(ar.submitted_at) as first_attendance,
            MAX(ar.submitted_at) as last_attendance,
            GROUP_CONCAT(DATE(ar.submitted_at) ORDER BY ar.submitted_at SEPARATOR ', ') as attendance_dates,
            GROUP_CONCAT(
                CASE 
                    WHEN ar.latitude IS NOT NULL AND ar.longitude IS NOT NULL 
                    THEN CONCAT(ROUND(ar.latitude, 6), ',', ROUND(ar.longitude, 6))
                    ELSE NULL 
                END 
                ORDER BY ar.submitted_at SEPARATOR ' | '
            ) as gps_coordinates,
            GROUP_CONCAT(
                CASE 
                    WHEN ar.location_accuracy IS NOT NULL 
                    THEN CONCAT('±', ROUND(ar.location_accuracy), 'm')
                    ELSE NULL 
                END 
                ORDER BY ar.submitted_at SEPARATOR ' | '
            ) as gps_accuracy,
            GROUP_CONCAT(ar.location_address ORDER BY ar.submitted_at SEPARATOR ' | ') as gps_addresses,
            COUNT(CASE WHEN ar.latitude IS NOT NULL AND ar.longitude IS NOT NULL THEN 1 END) as gps_count
        FROM attendance_records ar
        JOIN programs p ON ar.program_id = p.id
        LEFT JOIN locations ld ON ar.district_id = ld.id
        LEFT JOIN locations la ON ar.assembly_id = la.id
        $where_clause
        GROUP BY ar.program_id, ar.full_name, ar.email, ar.district_id, ar.assembly_id
        ORDER BY p.name, ar.full_name
    ";
    
    $stmt = $conn->prepare($query);
    $stmt->execute($params);
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

function exportToCSV($data) {
    $filename = 'special_report_' . date('Y-m-d_H-i-s') . '.csv';
    
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    
    $output = fopen('php://output', 'w');
    
    // CSV headers
    $headers = [
        'Program Name', 'Attendee Name', 'Email', 'Phone', 'Officer Type', 'District', 'Assembly',
        'Attendance Count', 'First Attendance', 'Last Attendance', 'Program Start', 'Program End', 'Attendance Dates',
        'GPS Coordinates', 'GPS Accuracy', 'GPS Addresses', 'GPS Records Count'
    ];
    fputcsv($output, $headers);
    
    // Data rows
    foreach ($data as $row) {
        $csv_row = [
            $row['program_name'],
            $row['full_name'],
            $row['email'] ?: '-',
            $row['telephone'] ?: '-',
            $row['officer_type'] ?: '-',
            $row['district_name'],
            $row['assembly_name'],
            $row['attendance_count'],
            $row['first_attendance'],
            $row['last_attendance'],
            $row['start_date'],
            $row['end_date'],
            $row['attendance_dates'],
            $row['gps_coordinates'] ?: '-',
            $row['gps_accuracy'] ?: '-',
            $row['gps_addresses'] ?: '-',
            $row['gps_count'] ?: '0'
        ];
        fputcsv($output, $csv_row);
    }
    
    fclose($output);
}

function exportToExcel($data) {
    $filename = 'special_report_' . date('Y-m-d_H-i-s') . '.xls';
    
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    
    echo '<html>';
    echo '<head><meta charset="UTF-8"></head>';
    echo '<body>';
    echo '<table border="1" style="border-collapse: collapse;">';
    
    // Headers
    echo '<tr style="background-color: #f0f0f0; font-weight: bold;">';
    echo '<th>Program Name</th>';
    echo '<th>Attendee Name</th>';
    echo '<th>Email</th>';
    echo '<th>Phone</th>';
    echo '<th>Officer Type</th>';
    echo '<th>District</th>';
    echo '<th>Assembly</th>';
    echo '<th>Attendance Count</th>';
    echo '<th>First Attendance</th>';
    echo '<th>Last Attendance</th>';
    echo '<th>Program Start</th>';
    echo '<th>Program End</th>';
    echo '<th>Attendance Dates</th>';
    echo '<th>GPS Coordinates</th>';
    echo '<th>GPS Accuracy</th>';
    echo '<th>GPS Addresses</th>';
    echo '<th>GPS Count</th>';
    echo '</tr>';
    
    // Data rows
    foreach ($data as $row) {
        echo '<tr>';
        echo '<td>' . htmlspecialchars($row['program_name'] ?? '') . '</td>';
        echo '<td>' . htmlspecialchars($row['full_name'] ?? '') . '</td>';
        echo '<td>' . htmlspecialchars($row['email'] ?: '-') . '</td>';
        echo '<td>' . htmlspecialchars($row['telephone'] ?: '-') . '</td>';
        echo '<td>' . htmlspecialchars($row['officer_type'] ?: '-') . '</td>';
        echo '<td>' . htmlspecialchars($row['district_name'] ?? '') . '</td>';
        echo '<td>' . htmlspecialchars($row['assembly_name'] ?? '') . '</td>';
        echo '<td>' . $row['attendance_count'] . '</td>';
        echo '<td>' . date('M j, Y g:i A', strtotime($row['first_attendance'])) . '</td>';
        echo '<td>' . date('M j, Y g:i A', strtotime($row['last_attendance'])) . '</td>';
        echo '<td>' . ($row['start_date'] ? date('M j, Y', strtotime($row['start_date'])) : '-') . '</td>';
        echo '<td>' . ($row['end_date'] ? date('M j, Y', strtotime($row['end_date'])) : '-') . '</td>';
        echo '<td style="font-family: monospace; font-size: 10px;">' . htmlspecialchars($row['attendance_dates'] ?? '') . '</td>';
        echo '<td style="font-family: monospace; font-size: 9px;">' . htmlspecialchars($row['gps_coordinates'] ?: '-') . '</td>';
        echo '<td style="font-family: monospace; font-size: 9px;">' . htmlspecialchars($row['gps_accuracy'] ?: '-') . '</td>';
        echo '<td style="font-size: 9px;">' . htmlspecialchars($row['gps_addresses'] ?: '-') . '</td>';
        echo '<td>' . ($row['gps_count'] ?: '0') . '</td>';
        echo '</tr>';
    }
    
    echo '</table>';
    echo '</body>';
    echo '</html>';
}

function exportToPDF($data) {
    $filename = 'special_report_' . date('Y-m-d_H-i-s') . '.pdf';
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Special Report - Program Attendance Analysis</title>
        <style>
            @media print {
                body { margin: 0; font-family: Arial, sans-serif; font-size: 10px; }
                table { width: 100%; border-collapse: collapse; margin-top: 20px; }
                th, td { border: 1px solid #ddd; padding: 4px; text-align: left; }
                th { background-color: #f5f5f5; font-weight: bold; }
                .header { text-align: center; margin-bottom: 20px; }
                .dates { font-family: monospace; font-size: 8px; }
                .no-print { display: none; }
            }
            body { font-family: Arial, sans-serif; margin: 20px; }
            table { width: 100%; border-collapse: collapse; margin-top: 20px; }
            th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
            th { background-color: #f5f5f5; }
            .header { text-align: center; margin-bottom: 20px; }
            .dates { font-family: monospace; font-size: 10px; }
        </style>
    </head>
    <body>
        <div class="header">
            <h1>Special Report - Program Attendance Analysis</h1>
            <p>Generated on <?php echo date('F j, Y g:i A'); ?></p>
        </div>
        
        <table>
            <thead>
                <tr>
                    <th>Program</th>
                    <th>Attendee</th>
                    <th>Contact</th>
                    <th>Location</th>
                    <th>Count</th>
                    <th>Duration</th>
                    <th>GPS Info</th>
                    <th>Dates</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($data as $row): ?>
                <tr>
                    <td><?php echo htmlspecialchars($row['program_name'] ?? ''); ?></td>
                    <td><?php echo htmlspecialchars($row['full_name'] ?? ''); ?></td>
                    <td>
                        <?php if ($row['email']): ?>
                            <?php echo htmlspecialchars($row['email'] ?? ''); ?><br>
                        <?php endif; ?>
                        <?php if ($row['telephone']): ?>
                            <?php echo htmlspecialchars($row['telephone'] ?? ''); ?>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php echo htmlspecialchars($row['district_name'] ?? ''); ?><br>
                        <small><?php echo htmlspecialchars($row['assembly_name'] ?? ''); ?></small>
                    </td>
                    <td><?php echo $row['attendance_count']; ?></td>
                    <td>
                        <?php echo date('M j', strtotime($row['first_attendance'])); ?> - 
                        <?php echo date('M j, Y', strtotime($row['last_attendance'])); ?>
                    </td>
                    <td class="dates" style="font-size: 8px;">
                        <?php if ($row['gps_count'] > 0): ?>
                            <strong><?php echo $row['gps_count']; ?>/<?php echo $row['attendance_count']; ?> GPS</strong><br>
                            <?php if ($row['gps_coordinates']): ?>
                                <span style="font-family: monospace;"><?php echo htmlspecialchars(substr($row['gps_coordinates'], 0, 30)) . (strlen($row['gps_coordinates']) > 30 ? '...' : ''); ?></span><br>
                            <?php endif; ?>
                            <?php if ($row['gps_addresses']): ?>
                                <?php echo htmlspecialchars(substr($row['gps_addresses'], 0, 40)) . (strlen($row['gps_addresses']) > 40 ? '...' : ''); ?>
                            <?php endif; ?>
                        <?php else: ?>
                            <em>No GPS data</em>
                        <?php endif; ?>
                    </td>
                    <td class="dates"><?php echo htmlspecialchars(substr($row['attendance_dates'] ?? '', 0, 50)) . (strlen($row['attendance_dates'] ?? '') > 50 ? '...' : ''); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <script>
            window.onload = function() {
                setTimeout(function() {
                    window.print();
                }, 1000);
            };
        </script>
    </body>
    </html>
    <?php
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Special Reports - Program Attendance Analysis</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#F59E0B',
                        accent: '#6B7280'
                    }
                }
            }
        }
    </script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/mobile.css" rel="stylesheet">
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, #3B82F6 0%, #F59E0B 50%, #6B7280 100%);
        }
        .export-btn {
            transition: all 0.3s ease;
        }
        .export-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }
    </style>
</head>
<body class="bg-gray-50">
    <?php include 'includes/sidebar.php'; ?>
    
    <!-- Main Content -->
    <div class="md:ml-64 p-4 sm:p-6 lg:p-8">
        <!-- Header -->
        <div class="mb-8">
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">Special Reports</h1>
                    <p class="text-gray-600">Program attendance analysis with detailed statistics</p>
                </div>
                <div class="mt-4 sm:mt-0">
                    <div class="flex items-center space-x-2">
                        <i class="fas fa-chart-line text-primary"></i>
                        <span class="text-sm text-gray-500">Advanced Analytics</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Success/Error Messages -->
        <?php if (isset($_SESSION['success_message'])): ?>
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-6">
                <div class="flex items-center">
                    <i class="fas fa-check-circle mr-2"></i>
                    <span><?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?></span>
                </div>
            </div>
        <?php endif; ?>

        <?php if (isset($_SESSION['error_message'])): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-6">
                <div class="flex items-center">
                    <i class="fas fa-exclamation-circle mr-2"></i>
                    <span><?php echo $_SESSION['error_message']; unset($_SESSION['error_message']); ?></span>
                </div>
            </div>
        <?php endif; ?>

        <!-- Filters -->
        <div class="bg-white rounded-lg shadow-lg p-6 mb-8">
            <h2 class="text-xl font-bold text-gray-900 mb-4">
                <i class="fas fa-filter mr-2 text-primary"></i>
                Filter Reports
            </h2>
            
            <form method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <!-- Program Filter -->
                <div>
                    <label for="program_id" class="block text-sm font-medium text-gray-700 mb-2">Program</label>
                    <select id="program_id" name="program_id" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent">
                        <option value="">All Programs</option>
                        <?php foreach ($programs as $program): ?>
                            <option value="<?php echo $program['id']; ?>" <?php echo ($program_id == $program['id']) ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars($program['name'] ?? ''); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <!-- District Filter -->
                <div>
                    <label for="district_id" class="block text-sm font-medium text-gray-700 mb-2">District</label>
                    <select id="district_id" name="district_id" onchange="filterAssemblies()" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent">
                        <option value="">All Districts</option>
                        <?php foreach ($districts as $district): ?>
                            <option value="<?php echo $district['id']; ?>" <?php echo ($district_id == $district['id']) ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars($district['name'] ?? ''); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <!-- Assembly Filter -->
                <div>
                    <label for="assembly_id" class="block text-sm font-medium text-gray-700 mb-2">Assembly</label>
                    <select id="assembly_id" name="assembly_id" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent">
                        <option value="">All Assemblies</option>
                        <?php foreach ($assemblies as $assembly): ?>
                            <option value="<?php echo $assembly['id']; ?>" 
                                    data-district="<?php echo $assembly['parent_id']; ?>"
                                    <?php echo ($assembly_id == $assembly['id']) ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars($assembly['name'] ?? ''); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <!-- Filter Button -->
                <div class="flex items-end">
                    <button type="submit" class="w-full bg-primary text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition duration-300">
                        <i class="fas fa-search mr-2"></i>
                        Apply Filters
                    </button>
                </div>
            </form>
        </div>

        <!-- Export Buttons -->
        <?php if (!empty($report_data)): ?>
        <div class="bg-white rounded-lg shadow-lg p-6 mb-8">
            <h2 class="text-xl font-bold text-gray-900 mb-4">
                <i class="fas fa-download mr-2 text-primary"></i>
                Export Data
            </h2>
            
            <div class="grid grid-cols-1 sm:grid-cols-3 gap-4">
                <a href="?export=1&format=csv&district_id=<?php echo $district_id; ?>&assembly_id=<?php echo $assembly_id; ?>&program_id=<?php echo $program_id; ?>" 
                   class="export-btn bg-blue-600 text-white px-6 py-3 rounded-lg text-center hover:bg-blue-700 transition duration-300">
                    <i class="fas fa-file-csv mr-2"></i>
                    Export CSV
                </a>
                <a href="?export=1&format=excel&district_id=<?php echo $district_id; ?>&assembly_id=<?php echo $assembly_id; ?>&program_id=<?php echo $program_id; ?>" 
                   class="export-btn bg-green-600 text-white px-6 py-3 rounded-lg text-center hover:bg-green-700 transition duration-300">
                    <i class="fas fa-file-excel mr-2"></i>
                    Export Excel
                </a>
                <a href="?export=1&format=pdf&district_id=<?php echo $district_id; ?>&assembly_id=<?php echo $assembly_id; ?>&program_id=<?php echo $program_id; ?>" 
                   class="export-btn bg-red-600 text-white px-6 py-3 rounded-lg text-center hover:bg-red-700 transition duration-300">
                    <i class="fas fa-file-pdf mr-2"></i>
                    Export PDF
                </a>
            </div>
        </div>
        <?php endif; ?>

        <!-- Report Data -->
        <div class="bg-white rounded-lg shadow-lg">
            <div class="p-6 border-b border-gray-200">
                <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between">
                    <h2 class="text-xl font-bold text-gray-900">
                        <i class="fas fa-table mr-2 text-primary"></i>
                        Program Attendance Analysis
                    </h2>
                    <div class="mt-2 sm:mt-0">
                        <span class="text-sm text-gray-500">
                            Total Records: <strong><?php echo count($report_data); ?></strong>
                        </span>
                    </div>
                </div>
            </div>

            <?php if (empty($report_data)): ?>
                <div class="p-8 text-center">
                    <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
                        <i class="fas fa-chart-line text-gray-400 text-2xl"></i>
                    </div>
                    <h3 class="text-lg font-medium text-gray-900 mb-2">No Data Available</h3>
                    <p class="text-gray-500 mb-4">No attendance records found with the current filters.</p>
                    <a href="special_reports.php" class="text-primary hover:text-blue-700">
                        <i class="fas fa-refresh mr-1"></i>Clear Filters
                    </a>
                </div>
            <?php else: ?>
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Program</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Attendee</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Contact</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Location</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Attendance</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">GPS Location</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Duration</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <?php foreach ($report_data as $row): ?>
                                <tr class="hover:bg-gray-50">
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <div class="text-sm font-medium text-gray-900"><?php echo htmlspecialchars($row['program_name'] ?? ''); ?></div>
                                        <div class="text-xs text-gray-500">
                                            <?php if ($row['start_date']): ?>
                                                <?php echo date('M j, Y', strtotime($row['start_date'])); ?> - 
                                                <?php echo $row['end_date'] ? date('M j, Y', strtotime($row['end_date'])) : 'Ongoing'; ?>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <div class="text-sm font-medium text-gray-900"><?php echo htmlspecialchars($row['full_name'] ?? ''); ?></div>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <div class="text-sm text-gray-900">
                                            <?php if ($row['email']): ?>
                                                <div><i class="fas fa-envelope text-gray-400 mr-1"></i><?php echo htmlspecialchars($row['email'] ?? ''); ?></div>
                                            <?php endif; ?>
                                            <?php if ($row['telephone']): ?>
                                                <div><i class="fas fa-phone text-gray-400 mr-1"></i><?php echo htmlspecialchars($row['telephone'] ?? ''); ?></div>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <div class="text-sm text-gray-900"><?php echo htmlspecialchars($row['district_name'] ?? ''); ?></div>
                                        <div class="text-xs text-gray-500"><?php echo htmlspecialchars($row['assembly_name'] ?? ''); ?></div>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <div class="flex items-center">
                                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                                <?php echo $row['attendance_count']; ?> time<?php echo $row['attendance_count'] > 1 ? 's' : ''; ?>
                                            </span>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <?php if ($row['gps_count'] > 0): ?>
                                            <div class="text-sm text-gray-900">
                                                <div class="flex items-center mb-1">
                                                    <i class="fas fa-map-marker-alt text-green-600 mr-1"></i>
                                                    <span class="text-xs font-medium"><?php echo $row['gps_count']; ?>/<?php echo $row['attendance_count']; ?> GPS records</span>
                                                </div>
                                                <?php if ($row['gps_coordinates']): ?>
                                                    <div class="text-xs text-gray-600 font-mono">
                                                        <?php 
                                                        $coords = explode(' | ', $row['gps_coordinates']);
                                                        $first_coord = $coords[0] ?? '';
                                                        if ($first_coord) {
                                                            $coord_parts = explode(',', $first_coord);
                                                            if (count($coord_parts) == 2) {
                                                                $lat = trim($coord_parts[0]);
                                                                $lng = trim($coord_parts[1]);
                                                                echo htmlspecialchars($lat . ', ' . $lng);
                                                                if (count($coords) > 1) {
                                                                    echo ' <span class="text-gray-400">+' . (count($coords) - 1) . ' more</span>';
                                                                }
                                                            }
                                                        }
                                                        ?>
                                                    </div>
                                                <?php endif; ?>
                                                <?php if ($row['gps_addresses']): ?>
                                                    <div class="text-xs text-gray-500 mt-1">
                                                        <?php 
                                                        $addresses = explode(' | ', $row['gps_addresses']);
                                                        $first_address = $addresses[0] ?? '';
                                                        echo htmlspecialchars(substr($first_address, 0, 40)) . (strlen($first_address) > 40 ? '...' : '');
                                                        ?>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                        <?php else: ?>
                                            <div class="text-sm text-gray-400">
                                                <i class="fas fa-map-marker-alt mr-1"></i>
                                                No GPS data
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <div class="text-sm text-gray-900">
                                            <?php echo date('M j', strtotime($row['first_attendance'])); ?> - 
                                            <?php echo date('M j, Y', strtotime($row['last_attendance'])); ?>
                                        </div>
                                        <div class="text-xs text-gray-500 font-mono">
                                            <?php echo substr($row['attendance_dates'] ?? '', 0, 30) . (strlen($row['attendance_dates'] ?? '') > 30 ? '...' : ''); ?>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <script>
        // Filter assemblies based on selected district
        function filterAssemblies() {
            const districtId = document.getElementById('district_id').value;
            const assemblySelect = document.getElementById('assembly_id');
            const allOptions = assemblySelect.querySelectorAll('option[data-district]');
            
            // Reset assembly selection
            assemblySelect.value = '';
            
            // Show/hide assembly options based on selected district
            allOptions.forEach(option => {
                if (districtId === '' || option.dataset.district === districtId) {
                    option.style.display = 'block';
                } else {
                    option.style.display = 'none';
                }
            });
        }

        // Initialize assembly filtering on page load
        document.addEventListener('DOMContentLoaded', function() {
            filterAssemblies();
        });
    </script>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists