Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/check-in.php

<?php
require_once '../includes/functions.php';

// Check if user is logged in and has admin privileges
if (!isLoggedIn()) {
    header('Location: ' . BASE_URL . 'login.php');
    exit();
}

$user = getCurrentUser();
if (!in_array($user['role'], ['superuser', 'area_admin', 'district_admin', 'assembly_admin'])) {
    header('Location: ' . BASE_URL . 'dashboard.php');
    exit();
}

$db = new CopMadinaDB();
$conn = $db->getConnection();

// Handle check-in/check-out actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'check_in':
                $registrationId = $_POST['registration_id'];
                $registrationType = $_POST['registration_type'];
                
                if ($registrationType === 'member') {
                    $stmt = $conn->prepare("UPDATE event_registrations SET status = 'attended', updated_at = NOW() WHERE id = ?");
                } else {
                    $stmt = $conn->prepare("UPDATE nonmember_registrations SET status = 'attended', updated_at = NOW() WHERE id = ?");
                }
                
                $result = $stmt->execute([$registrationId]);
                
                if ($result) {
                    addNotification('success', 'Attendee checked in successfully!', $user['id']);
                    logActivity($user['id'], 'attendee_checked_in', "Checked in registration ID: $registrationId ($registrationType)");
                } else {
                    addNotification('error', 'Failed to check in attendee.', $user['id']);
                }
                break;
                
            case 'check_out':
                $registrationId = $_POST['registration_id'];
                $registrationType = $_POST['registration_type'];
                
                if ($registrationType === 'member') {
                    $stmt = $conn->prepare("UPDATE event_registrations SET status = 'confirmed', updated_at = NOW() WHERE id = ?");
                } else {
                    $stmt = $conn->prepare("UPDATE nonmember_registrations SET status = 'confirmed', updated_at = NOW() WHERE id = ?");
                }
                
                $result = $stmt->execute([$registrationId]);
                
                if ($result) {
                    addNotification('success', 'Attendee checked out successfully!', $user['id']);
                    logActivity($user['id'], 'attendee_checked_out', "Checked out registration ID: $registrationId ($registrationType)");
                } else {
                    addNotification('error', 'Failed to check out attendee.', $user['id']);
                }
                break;
                
            case 'bulk_check_in':
                $eventId = $_POST['event_id'];
                
                // Check in all confirmed registrations for the event
                $memberStmt = $conn->prepare("UPDATE event_registrations SET status = 'attended', updated_at = NOW() WHERE event_id = ? AND status = 'confirmed'");
                $nonMemberStmt = $conn->prepare("UPDATE nonmember_registrations SET status = 'attended', updated_at = NOW() WHERE event_id = ? AND status = 'confirmed'");
                
                $memberResult = $memberStmt->execute([$eventId]);
                $nonMemberResult = $nonMemberStmt->execute([$eventId]);
                
                if ($memberResult && $nonMemberResult) {
                    $totalCheckedIn = $memberStmt->rowCount() + $nonMemberStmt->rowCount();
                    addNotification('success', "Bulk check-in completed! $totalCheckedIn attendees checked in.", $user['id']);
                    logActivity($user['id'], 'bulk_check_in', "Bulk checked in $totalCheckedIn attendees for event ID: $eventId");
                } else {
                    addNotification('error', 'Failed to perform bulk check-in.', $user['id']);
                }
                break;
        }
        
        header('Location: check-in.php' . (isset($_GET['event_id']) ? '?event_id=' . $_GET['event_id'] : ''));
        exit();
    }
}

// Get events for filtering
$whereClause = "WHERE e.status = 'published'";
$params = [];

// Role-based filtering
if ($user['role'] === 'area_admin') {
    $whereClause .= " AND e.area_id = ?";
    $params[] = $user['area_id'];
} elseif ($user['role'] === 'district_admin') {
    $whereClause .= " AND e.district_id = ?";
    $params[] = $user['district_id'];
} elseif ($user['role'] === 'assembly_admin') {
    $whereClause .= " AND e.assembly_id = ?";
    $params[] = $user['assembly_id'];
}

$eventStmt = $conn->prepare("
    SELECT e.*, 
           (SELECT COUNT(*) FROM event_registrations er WHERE er.event_id = e.id) as member_registrations,
           (SELECT COUNT(*) FROM nonmember_registrations nr WHERE nr.event_id = e.id) as nonmember_registrations,
           (SELECT COUNT(*) FROM event_registrations er WHERE er.event_id = e.id AND er.status = 'attended') as member_attended,
           (SELECT COUNT(*) FROM nonmember_registrations nr WHERE nr.event_id = e.id AND nr.status = 'attended') as nonmember_attended
    FROM events e 
    $whereClause
    ORDER BY e.start_date DESC
");
$eventStmt->execute($params);
$events = $eventStmt->fetchAll(PDO::FETCH_ASSOC);

// Get registrations for selected event
$selectedEventId = $_GET['event_id'] ?? null;
$registrations = [];

if ($selectedEventId) {
    // Get member registrations
    $memberStmt = $conn->prepare("
        SELECT er.*, u.first_name, u.last_name, u.email, u.phone, 
               e.title as event_title, tt.name as ticket_type,
               'member' as registration_type
        FROM event_registrations er
        JOIN users u ON er.user_id = u.id
        JOIN events e ON er.event_id = e.id
        LEFT JOIN ticket_types tt ON er.ticket_type_id = tt.id
        WHERE er.event_id = ?
        ORDER BY er.registration_date DESC
    ");
    $memberStmt->execute([$selectedEventId]);
    $memberRegistrations = $memberStmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Get non-member registrations
    $nonMemberStmt = $conn->prepare("
        SELECT nr.*, e.title as event_title, tt.name as ticket_type,
               'nonmember' as registration_type
        FROM nonmember_registrations nr
        JOIN events e ON nr.event_id = e.id
        LEFT JOIN ticket_types tt ON nr.ticket_type_id = tt.id
        WHERE nr.event_id = ?
        ORDER BY nr.registration_date DESC
    ");
    $nonMemberStmt->execute([$selectedEventId]);
    $nonMemberRegistrations = $nonMemberStmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Combine registrations
    $registrations = array_merge($memberRegistrations, $nonMemberRegistrations);
}

$notifications = getNotifications($user['id']);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Event Check-In - COP Madina Conference</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    animation: {
                        'fade-in': 'fadeIn 0.5s ease-out',
                        'slide-up': 'slideUp 0.6s ease-out',
                        'scale-in': 'scaleIn 0.3s ease-out'
                    },
                    keyframes: {
                        fadeIn: {
                            '0%': { opacity: '0' },
                            '100%': { opacity: '1' }
                        },
                        slideUp: {
                            '0%': { opacity: '0', transform: 'translateY(20px)' },
                            '100%': { opacity: '1', transform: 'translateY(0)' }
                        },
                        scaleIn: {
                            '0%': { opacity: '0', transform: 'scale(0.95)' },
                            '100%': { opacity: '1', transform: 'scale(1)' }
                        }
                    }
                }
            }
        }
    </script>
</head>
<body class="bg-gradient-to-br from-slate-50 to-blue-50 min-h-screen">
    <div id="app" class="flex h-screen">
        <!-- Sidebar -->
        <?php include 'includes/admin_sidebar.php'; ?>
        
        <!-- Main Content -->
        <div class="flex-1 flex flex-col overflow-hidden ml-72">
            <!-- Header -->
            <header class="bg-white/80 backdrop-blur-sm shadow-lg border-b border-slate-200/50">
                <div class="flex items-center justify-between px-8 py-6">
                    <div class="animate-fade-in">
                        <h1 class="text-3xl font-bold bg-gradient-to-r from-indigo-600 to-purple-600 bg-clip-text text-transparent flex items-center">
                            <div class="p-2 rounded-xl bg-gradient-to-br from-indigo-500 to-purple-600 mr-3">
                                <i class="fas fa-sign-in-alt text-white"></i>
                            </div>
                            Event Check-In
                        </h1>
                        <p class="text-slate-600 mt-1">Manage attendee check-in and check-out</p>
                    </div>
                    <?php if ($selectedEventId): ?>
                    <div class="flex space-x-4">
                        <button @click="showBulkCheckInModal = true" class="px-6 py-3 bg-gradient-to-r from-emerald-600 to-green-600 hover:from-emerald-700 hover:to-green-700 text-white font-medium rounded-xl transition-all duration-200 flex items-center space-x-2 shadow-lg hover:shadow-xl animate-scale-in">
                            <i class="fas fa-users"></i>
                            <span>Bulk Check-In</span>
                        </button>
                        <a href="check-in.php" class="px-6 py-3 bg-gradient-to-r from-slate-600 to-slate-700 hover:from-slate-700 hover:to-slate-800 text-white font-medium rounded-xl transition-all duration-200 flex items-center space-x-2 shadow-lg hover:shadow-xl animate-scale-in">
                            <i class="fas fa-arrow-left"></i>
                            <span>Back to Events</span>
                        </a>
                    </div>
                    <?php endif; ?>
                </div>
            </header>

            <!-- Content -->
            <main class="flex-1 overflow-y-auto p-8">

                <!-- Notifications -->
                <?php if (!empty($notifications)): ?>
                <div class="mb-6 space-y-2">
                    <?php foreach ($notifications as $notification): ?>
                        <div class="alert alert-<?php echo $notification['type']; ?> p-4 rounded-xl border-l-4 <?php echo $notification['type'] === 'success' ? 'bg-emerald-50 border-emerald-500 text-emerald-800' : 'bg-red-50 border-red-500 text-red-800'; ?>">
                            <div class="flex items-center">
                                <i class="fas <?php echo $notification['type'] === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'; ?> mr-3"></i>
                                <?php echo htmlspecialchars($notification['message']); ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>

                <?php if (!$selectedEventId): ?>
                <!-- Events Grid -->
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <?php foreach ($events as $index => $event): ?>
                    <?php 
                        $totalRegistrations = $event['member_registrations'] + $event['nonmember_registrations'];
                        $totalAttended = $event['member_attended'] + $event['nonmember_attended'];
                        $attendanceRate = $totalRegistrations > 0 ? ($totalAttended / $totalRegistrations) * 100 : 0;
                    ?>
                    <div class="group bg-white/70 backdrop-blur-sm rounded-2xl shadow-lg hover:shadow-xl transition-all duration-300 p-6 border border-slate-200/50 hover:border-indigo-300/50 animate-slide-up" 
                         style="animation-delay: <?php echo $index * 0.1; ?>s">
                        <div class="mb-4">
                            <h3 class="text-xl font-bold text-slate-800 mb-2"><?php echo htmlspecialchars($event['title']); ?></h3>
                            <p class="text-sm text-slate-600 mb-2">
                                <i class="fas fa-calendar mr-2"></i>
                                <?php echo date('M j, Y g:i A', strtotime($event['start_date'])); ?>
                            </p>
                            <p class="text-sm text-slate-600">
                                <i class="fas fa-map-marker-alt mr-2"></i>
                                <?php echo htmlspecialchars($event['venue'] ?: 'TBA'); ?>
                            </p>
                        </div>
                        
                        <div class="space-y-3 mb-6">
                            <div class="flex justify-between items-center">
                                <span class="text-sm text-slate-600">Total Registered:</span>
                                <span class="font-semibold text-slate-800"><?php echo $totalRegistrations; ?></span>
                            </div>
                            
                            <div class="flex justify-between items-center">
                                <span class="text-sm text-slate-600">Checked In:</span>
                                <span class="font-semibold text-emerald-600"><?php echo $totalAttended; ?></span>
                            </div>
                            
                            <div class="flex justify-between items-center">
                                <span class="text-sm text-slate-600">Attendance Rate:</span>
                                <span class="font-semibold text-slate-800"><?php echo number_format($attendanceRate, 1); ?>%</span>
                            </div>
                            
                            <div class="w-full bg-slate-200 rounded-full h-2">
                                <div class="bg-gradient-to-r from-emerald-500 to-green-600 h-2 rounded-full transition-all duration-300" style="width: <?php echo $attendanceRate; ?>%"></div>
                            </div>
                        </div>
                        
                        <a href="check-in.php?event_id=<?php echo $event['id']; ?>" class="block w-full text-center bg-gradient-to-r from-indigo-600 to-purple-600 hover:from-indigo-700 hover:to-purple-700 text-white px-6 py-3 rounded-xl font-medium shadow-lg hover:shadow-xl transition-all duration-200">
                            <i class="fas fa-sign-in-alt mr-2"></i>Manage Check-In
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>

                <?php if (empty($events)): ?>
                <div class="text-center py-12">
                    <div class="inline-flex items-center justify-center w-16 h-16 bg-slate-100 rounded-full mb-4">
                        <i class="fas fa-calendar text-2xl text-slate-400"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-slate-800 mb-2">No Events Found</h3>
                    <p class="text-slate-600">No published events available for check-in management.</p>
                </div>
                <?php endif; ?>

                <?php else: ?>
                <!-- Registrations Table -->
                <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-lg border border-slate-200/50 overflow-hidden">
                    <div class="p-6 border-b border-slate-200">
                        <h2 class="text-2xl font-bold text-slate-800">Event Registrations</h2>
                        <p class="text-slate-600 mt-1">Manage check-in status for all attendees</p>
                    </div>
                    
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-slate-50">
                                <tr>
                                    <th class="px-6 py-4 text-left text-sm font-semibold text-slate-700">Attendee</th>
                                    <th class="px-6 py-4 text-left text-sm font-semibold text-slate-700">Contact</th>
                                    <th class="px-6 py-4 text-left text-sm font-semibold text-slate-700">Ticket Type</th>
                                    <th class="px-6 py-4 text-left text-sm font-semibold text-slate-700">Registration Code</th>
                                    <th class="px-6 py-4 text-left text-sm font-semibold text-slate-700">Status</th>
                                    <th class="px-6 py-4 text-left text-sm font-semibold text-slate-700">Actions</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-slate-200">
                                <?php foreach ($registrations as $registration): ?>
                                <tr class="hover:bg-slate-50 transition-colors">
                                    <td class="px-6 py-4">
                                        <div>
                                            <div class="font-semibold text-slate-800">
                                                <?php if ($registration['registration_type'] === 'member'): ?>
                                                    <?php echo htmlspecialchars($registration['first_name'] . ' ' . $registration['last_name']); ?>
                                                    <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800 ml-2">Member</span>
                                                <?php else: ?>
                                                    <?php 
                                                        $formData = json_decode($registration['form_data'], true);
                                                        $name = ($formData['first_name'] ?? '') . ' ' . ($formData['last_name'] ?? '');
                                                        echo htmlspecialchars(trim($name) ?: 'N/A');
                                                    ?>
                                                    <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-800 ml-2">Guest</span>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4">
                                        <div class="text-sm text-slate-600">
                                            <?php if ($registration['registration_type'] === 'member'): ?>
                                                <div><?php echo htmlspecialchars($registration['email']); ?></div>
                                                <div><?php echo htmlspecialchars($registration['phone'] ?: 'N/A'); ?></div>
                                            <?php else: ?>
                                                <?php 
                                                    $formData = json_decode($registration['form_data'], true);
                                                    echo '<div>' . htmlspecialchars($formData['email'] ?? 'N/A') . '</div>';
                                                    echo '<div>' . htmlspecialchars($formData['phone'] ?? 'N/A') . '</div>';
                                                ?>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4">
                                        <span class="text-sm text-slate-600"><?php echo htmlspecialchars($registration['ticket_type'] ?: 'General'); ?></span>
                                    </td>
                                    <td class="px-6 py-4">
                                        <span class="font-mono text-sm text-slate-800"><?php echo htmlspecialchars($registration['registration_code']); ?></span>
                                    </td>
                                    <td class="px-6 py-4">
                                        <?php if ($registration['status'] === 'attended'): ?>
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-emerald-100 text-emerald-800">
                                                <i class="fas fa-check-circle mr-1"></i>Checked In
                                            </span>
                                        <?php elseif ($registration['status'] === 'confirmed'): ?>
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800">
                                                <i class="fas fa-clock mr-1"></i>Confirmed
                                            </span>
                                        <?php else: ?>
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-slate-100 text-slate-800">
                                                <?php echo ucfirst($registration['status']); ?>
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="px-6 py-4">
                                        <div class="flex space-x-2">
                                            <?php if ($registration['status'] === 'attended'): ?>
                                                <form method="POST" class="inline">
                                                    <input type="hidden" name="action" value="check_out">
                                                    <input type="hidden" name="registration_id" value="<?php echo $registration['id']; ?>">
                                                    <input type="hidden" name="registration_type" value="<?php echo $registration['registration_type']; ?>">
                                                    <button type="submit" class="text-orange-600 hover:text-orange-800 p-2 rounded-lg hover:bg-orange-50 transition-colors" title="Check Out">
                                                        <i class="fas fa-sign-out-alt"></i>
                                                    </button>
                                                </form>
                                            <?php else: ?>
                                                <form method="POST" class="inline">
                                                    <input type="hidden" name="action" value="check_in">
                                                    <input type="hidden" name="registration_id" value="<?php echo $registration['id']; ?>">
                                                    <input type="hidden" name="registration_type" value="<?php echo $registration['registration_type']; ?>">
                                                    <button type="submit" class="text-emerald-600 hover:text-emerald-800 p-2 rounded-lg hover:bg-emerald-50 transition-colors" title="Check In">
                                                        <i class="fas fa-sign-in-alt"></i>
                                                    </button>
                                                </form>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    
                    <?php if (empty($registrations)): ?>
                    <div class="text-center py-12">
                        <div class="inline-flex items-center justify-center w-16 h-16 bg-slate-100 rounded-full mb-4">
                            <i class="fas fa-users text-2xl text-slate-400"></i>
                        </div>
                        <h3 class="text-xl font-semibold text-slate-800 mb-2">No Registrations Found</h3>
                        <p class="text-slate-600">No registrations found for this event.</p>
                    </div>
                    <?php endif; ?>
                </div>
                <?php endif; ?>

                <!-- Bulk Check-In Modal -->
                <div v-if="showBulkCheckInModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
                    <div class="bg-white rounded-2xl shadow-2xl w-full max-w-md mx-4">
                        <div class="p-6 text-center">
                            <div class="inline-flex items-center justify-center w-16 h-16 bg-emerald-100 rounded-full mb-4">
                                <i class="fas fa-users text-2xl text-emerald-600"></i>
                            </div>
                            <h3 class="text-xl font-bold text-slate-800 mb-2">Bulk Check-In</h3>
                            <p class="text-slate-600 mb-6">This will check in all confirmed attendees for this event. Are you sure you want to continue?</p>
                            
                            <div class="flex justify-center space-x-4">
                                <button @click="showBulkCheckInModal = false" class="px-6 py-3 border border-slate-300 text-slate-700 rounded-xl hover:bg-slate-50 font-semibold transition-colors">
                                    Cancel
                                </button>
                                <form method="POST" class="inline">
                                    <input type="hidden" name="action" value="bulk_check_in">
                                    <input type="hidden" name="event_id" value="<?php echo $selectedEventId; ?>">
                                    <button type="submit" class="px-6 py-3 bg-emerald-600 hover:bg-emerald-700 text-white rounded-xl font-semibold transition-colors">
                                        Check In All
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <script>
        const { createApp } = Vue;
        
        createApp({
            data() {
                return {
                    showBulkCheckInModal: false
                }
            }
        }).mount('#app');
    </script>
</body>
</html>

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