Sindbad~EG File Manager

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

<?php
session_start();
require_once 'includes/functions.php';

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

$message = '';
$messageType = '';

// Handle check-out form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $registrationCode = trim($_POST['registration_code']);
    
    if (empty($registrationCode)) {
        $message = 'Please enter your registration code.';
        $messageType = 'error';
    } else {
        // Check member registrations first
        $stmt = $conn->prepare("
            SELECT er.*, e.title as event_title, u.first_name, u.last_name, u.email
            FROM event_registrations er
            JOIN events e ON er.event_id = e.id
            JOIN users u ON er.user_id = u.id
            WHERE er.registration_code = ? AND e.status = 'published' AND er.status = 'attended'
        ");
        $stmt->execute([$registrationCode]);
        $memberRegistration = $stmt->fetch();
        
        if ($memberRegistration) {
            if ($memberRegistration['checked_out_at']) {
                $message = 'You have already checked out of this event.';
                $messageType = 'info';
            } else {
                // Update check-out time
                $updateStmt = $conn->prepare("UPDATE event_registrations SET checked_out_at = NOW(), updated_at = NOW() WHERE registration_code = ?");
                $result = $updateStmt->execute([$registrationCode]);
                
                if ($result) {
                    // Calculate duration
                    $durationStmt = $conn->prepare("SELECT TIMESTAMPDIFF(MINUTE, checked_in_at, NOW()) as duration_minutes FROM event_registrations WHERE registration_code = ?");
                    $durationStmt->execute([$registrationCode]);
                    $duration = $durationStmt->fetch()['duration_minutes'];
                    
                    // Log attendance
                    $logStmt = $conn->prepare("INSERT INTO attendance_logs (event_id, user_id, registration_code, registration_type, action, action_time, duration_minutes, ip_address, user_agent) VALUES (?, ?, ?, 'member', 'check_out', NOW(), ?, ?, ?)");
                    $logStmt->execute([
                        $memberRegistration['event_id'],
                        $memberRegistration['user_id'],
                        $registrationCode,
                        $duration,
                        $_SERVER['REMOTE_ADDR'] ?? null,
                        $_SERVER['HTTP_USER_AGENT'] ?? null
                    ]);
                    
                    $hours = floor($duration / 60);
                    $minutes = $duration % 60;
                    $durationText = ($hours > 0 ? $hours . 'h ' : '') . $minutes . 'm';
                    
                    $message = "Thank you {$memberRegistration['first_name']}! You have been checked out of '{$memberRegistration['event_title']}'. Duration: {$durationText}";
                    $messageType = 'success';
                    
                    // Log the check-out activity
                    logActivity($memberRegistration['user_id'], 'self_check_out', "Self checked out of event: {$memberRegistration['event_title']} (Duration: {$durationText})");
                } else {
                    $message = 'Failed to check out. Please try again or contact support.';
                    $messageType = 'error';
                }
            }
        } else {
            // Check non-member registrations
            $stmt = $conn->prepare("
                SELECT nr.*, e.title as event_title
                FROM nonmember_registrations nr
                JOIN events e ON nr.event_id = e.id
                WHERE nr.registration_code = ? AND e.status = 'published' AND nr.status = 'attended'
            ");
            $stmt->execute([$registrationCode]);
            $nonMemberRegistration = $stmt->fetch();
            
            if ($nonMemberRegistration) {
                if ($nonMemberRegistration['checked_out_at']) {
                    $message = 'You have already checked out of this event.';
                    $messageType = 'info';
                } else {
                    // Update check-out time
                    $updateStmt = $conn->prepare("UPDATE nonmember_registrations SET checked_out_at = NOW(), updated_at = NOW() WHERE registration_code = ?");
                    $result = $updateStmt->execute([$registrationCode]);
                    
                    if ($result) {
                        // Calculate duration
                        $durationStmt = $conn->prepare("SELECT TIMESTAMPDIFF(MINUTE, checked_in_at, NOW()) as duration_minutes FROM nonmember_registrations WHERE registration_code = ?");
                        $durationStmt->execute([$registrationCode]);
                        $duration = $durationStmt->fetch()['duration_minutes'];
                        
                        // Log attendance
                        $logStmt = $conn->prepare("INSERT INTO attendance_logs (event_id, user_id, registration_code, registration_type, action, action_time, duration_minutes, ip_address, user_agent) VALUES (?, NULL, ?, 'nonmember', 'check_out', NOW(), ?, ?, ?)");
                        $logStmt->execute([
                            $nonMemberRegistration['event_id'],
                            $registrationCode,
                            $duration,
                            $_SERVER['REMOTE_ADDR'] ?? null,
                            $_SERVER['HTTP_USER_AGENT'] ?? null
                        ]);
                        
                        $formData = json_decode($nonMemberRegistration['form_data'], true);
                        $guestName = ($formData['first_name'] ?? '') . ' ' . ($formData['last_name'] ?? '');
                        $guestName = trim($guestName) ?: 'Guest';
                        
                        $hours = floor($duration / 60);
                        $minutes = $duration % 60;
                        $durationText = ($hours > 0 ? $hours . 'h ' : '') . $minutes . 'm';
                        
                        $message = "Thank you {$guestName}! You have been checked out of '{$nonMemberRegistration['event_title']}'. Duration: {$durationText}";
                        $messageType = 'success';
                    } else {
                        $message = 'Failed to check out. Please try again or contact support.';
                        $messageType = 'error';
                    }
                }
            } else {
                $message = 'Registration code not found or you are not currently checked in to any event.';
                $messageType = 'error';
            }
        }
    }
}

// Get current checked-in attendees for display
$checkedInStmt = $conn->prepare("
    SELECT 'member' as type, er.registration_code, e.title as event_title, u.first_name, u.last_name, 
           er.checked_in_at, TIMESTAMPDIFF(MINUTE, er.checked_in_at, NOW()) as current_duration
    FROM event_registrations er
    JOIN events e ON er.event_id = e.id
    JOIN users u ON er.user_id = u.id
    WHERE er.status = 'attended' AND er.checked_in_at IS NOT NULL AND er.checked_out_at IS NULL
    AND e.status = 'published' AND e.start_date <= DATE_ADD(NOW(), INTERVAL 1 DAY) AND e.end_date >= NOW()
    
    UNION ALL
    
    SELECT 'nonmember' as type, nr.registration_code, e.title as event_title, 
           JSON_UNQUOTE(JSON_EXTRACT(nr.form_data, '$.first_name')) as first_name,
           JSON_UNQUOTE(JSON_EXTRACT(nr.form_data, '$.last_name')) as last_name,
           nr.checked_in_at, TIMESTAMPDIFF(MINUTE, nr.checked_in_at, NOW()) as current_duration
    FROM nonmember_registrations nr
    JOIN events e ON nr.event_id = e.id
    WHERE nr.status = 'attended' AND nr.checked_in_at IS NOT NULL AND nr.checked_out_at IS NULL
    AND e.status = 'published' AND e.start_date <= DATE_ADD(NOW(), INTERVAL 1 DAY) AND e.end_date >= NOW()
    
    ORDER BY checked_in_at DESC
    LIMIT 20
");
$checkedInStmt->execute();
$checkedInAttendees = $checkedInStmt->fetchAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Event Check-Out - 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>
        tailwind.config = {
            theme: {
                extend: {
                    animation: {
                        'fade-in': 'fadeIn 0.5s ease-out',
                        'slide-up': 'slideUp 0.6s ease-out',
                        'pulse-slow': 'pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite'
                    },
                    keyframes: {
                        fadeIn: {
                            '0%': { opacity: '0' },
                            '100%': { opacity: '1' }
                        },
                        slideUp: {
                            '0%': { opacity: '0', transform: 'translateY(20px)' },
                            '100%': { opacity: '1', transform: 'translateY(0)' }
                        }
                    }
                }
            }
        }
    </script>
</head>
<body class="bg-gradient-to-br from-red-50 via-white to-orange-100 min-h-screen">
    <!-- Header -->
    <header class="bg-white/80 backdrop-blur-sm shadow-lg border-b border-slate-200/50 sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between items-center py-4">
                <div class="flex items-center space-x-4">
                    <a href="index.php" class="flex items-center space-x-3">
                        <div class="w-10 h-10 bg-gradient-to-br from-red-600 to-orange-600 rounded-xl flex items-center justify-center">
                            <i class="fas fa-church text-white text-lg"></i>
                        </div>
                        <div>
                            <h1 class="text-xl font-bold bg-gradient-to-r from-red-600 to-orange-600 bg-clip-text text-transparent">
                                COP Madina Conference
                            </h1>
                            <p class="text-sm text-slate-600">Event Check-Out</p>
                        </div>
                    </a>
                </div>
                
                <nav class="hidden md:flex items-center space-x-6">
                    <a href="index.php" class="text-slate-600 hover:text-red-600 transition-colors">
                        <i class="fas fa-home mr-2"></i>Home
                    </a>
                    <a href="check-in.php" class="text-slate-600 hover:text-blue-600 transition-colors">
                        <i class="fas fa-sign-in-alt mr-2"></i>Check-In
                    </a>
                    <a href="login.php" class="bg-gradient-to-r from-red-600 to-orange-600 text-white px-4 py-2 rounded-lg hover:from-red-700 hover:to-orange-700 transition-all duration-200">
                        <i class="fas fa-sign-in-alt mr-2"></i>Login
                    </a>
                </nav>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Hero Section -->
        <div class="text-center mb-12 animate-fade-in">
            <div class="inline-flex items-center justify-center w-20 h-20 bg-gradient-to-br from-red-500 to-orange-600 rounded-2xl mb-6 shadow-lg">
                <i class="fas fa-sign-out-alt text-white text-3xl"></i>
            </div>
            <h1 class="text-4xl md:text-5xl font-bold text-slate-800 mb-4">
                Event Check-Out
            </h1>
            <p class="text-xl text-slate-600 max-w-2xl mx-auto">
                Enter your registration code to check out of your event and record your attendance duration.
            </p>
        </div>

        <!-- Message Display -->
        <?php if ($message): ?>
        <div class="mb-8 animate-slide-up">
            <div class="p-6 rounded-2xl border-l-4 <?php echo $messageType === 'success' ? 'bg-emerald-50 border-emerald-500' : ($messageType === 'info' ? 'bg-blue-50 border-blue-500' : 'bg-red-50 border-red-500'); ?>">
                <div class="flex items-center">
                    <i class="fas <?php echo $messageType === 'success' ? 'fa-check-circle text-emerald-600' : ($messageType === 'info' ? 'fa-info-circle text-blue-600' : 'fa-exclamation-circle text-red-600'); ?> text-xl mr-4"></i>
                    <div>
                        <p class="font-semibold <?php echo $messageType === 'success' ? 'text-emerald-800' : ($messageType === 'info' ? 'text-blue-800' : 'text-red-800'); ?>">
                            <?php echo htmlspecialchars($message); ?>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Check-Out Form -->
        <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl p-8 mb-12 border border-slate-200/50 animate-slide-up">
            <form method="POST" class="space-y-6">
                <div>
                    <label for="registration_code" class="block text-lg font-semibold text-slate-700 mb-3">
                        <i class="fas fa-ticket-alt mr-2 text-red-600"></i>
                        Registration Code
                    </label>
                    <input 
                        type="text" 
                        id="registration_code" 
                        name="registration_code" 
                        required 
                        class="w-full px-6 py-4 text-lg border-2 border-slate-300 rounded-xl focus:ring-4 focus:ring-red-500/20 focus:border-red-500 transition-all duration-200 bg-white/50 backdrop-blur-sm"
                        placeholder="Enter your registration code (e.g., REG-12345)"
                        value="<?php echo htmlspecialchars($_POST['registration_code'] ?? ''); ?>"
                    >
                    <p class="mt-2 text-sm text-slate-600">
                        <i class="fas fa-info-circle mr-1"></i>
                        Only attendees who are currently checked in can check out.
                    </p>
                </div>
                
                <button 
                    type="submit" 
                    class="w-full bg-gradient-to-r from-red-600 to-orange-600 hover:from-red-700 hover:to-orange-700 text-white font-semibold py-4 px-8 rounded-xl transition-all duration-200 shadow-lg hover:shadow-xl transform hover:-translate-y-1 text-lg"
                >
                    <i class="fas fa-sign-out-alt mr-3"></i>
                    Check Out of Event
                </button>
            </form>
        </div>

        <!-- Currently Checked-In Attendees -->
        <?php if (!empty($checkedInAttendees)): ?>
        <div class="animate-slide-up">
            <h2 class="text-2xl font-bold text-slate-800 mb-6 text-center">
                <i class="fas fa-users mr-2 text-red-600"></i>
                Currently Checked-In Attendees
            </h2>
            
            <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead class="bg-gradient-to-r from-red-500 to-orange-500 text-white">
                            <tr>
                                <th class="px-6 py-4 text-left font-semibold">Name</th>
                                <th class="px-6 py-4 text-left font-semibold">Event</th>
                                <th class="px-6 py-4 text-left font-semibold">Type</th>
                                <th class="px-6 py-4 text-left font-semibold">Check-In Time</th>
                                <th class="px-6 py-4 text-left font-semibold">Duration</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-200">
                            <?php foreach ($checkedInAttendees as $attendee): ?>
                            <?php 
                                $hours = floor($attendee['current_duration'] / 60);
                                $minutes = $attendee['current_duration'] % 60;
                                $durationText = ($hours > 0 ? $hours . 'h ' : '') . $minutes . 'm';
                            ?>
                            <tr class="hover:bg-slate-50 transition-colors">
                                <td class="px-6 py-4">
                                    <div class="font-semibold text-slate-800">
                                        <?php echo htmlspecialchars(trim($attendee['first_name'] . ' ' . $attendee['last_name'])); ?>
                                    </div>
                                </td>
                                <td class="px-6 py-4 text-slate-600">
                                    <?php echo htmlspecialchars($attendee['event_title']); ?>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="px-3 py-1 rounded-full text-xs font-semibold <?php echo $attendee['type'] === 'member' ? 'bg-blue-100 text-blue-800' : 'bg-purple-100 text-purple-800'; ?>">
                                        <?php echo ucfirst($attendee['type']); ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-slate-600">
                                    <?php echo date('M j, g:i A', strtotime($attendee['checked_in_at'])); ?>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="font-semibold text-emerald-600"><?php echo $durationText; ?></span>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Help Section -->
        <div class="mt-12 bg-gradient-to-r from-red-50 to-orange-50 rounded-2xl p-8 border border-red-200/50 animate-slide-up">
            <h3 class="text-xl font-bold text-slate-800 mb-4">
                <i class="fas fa-question-circle mr-2 text-red-600"></i>
                Check-Out Information
            </h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 text-sm text-slate-600">
                <div>
                    <h4 class="font-semibold text-slate-800 mb-2">Important Notes:</h4>
                    <ul class="space-y-1">
                        <li><i class="fas fa-check text-green-600 mr-2"></i>You must be currently checked in to check out</li>
                        <li><i class="fas fa-check text-green-600 mr-2"></i>Your attendance duration will be automatically calculated</li>
                        <li><i class="fas fa-check text-green-600 mr-2"></i>Check-out is final and cannot be undone</li>
                    </ul>
                </div>
                <div>
                    <h4 class="font-semibold text-slate-800 mb-2">Need Help?</h4>
                    <ul class="space-y-1">
                        <li><i class="fas fa-check text-green-600 mr-2"></i>Use the same registration code from check-in</li>
                        <li><i class="fas fa-check text-green-600 mr-2"></i>Contact event organizers if you have issues</li>
                        <li><i class="fas fa-check text-green-600 mr-2"></i>Your attendance record will be saved</li>
                    </ul>
                </div>
            </div>
        </div>
    </main>

    <!-- Footer -->
    <footer class="bg-slate-800 text-white py-8 mt-16">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
            <p class="text-slate-300">
                © <?php echo date('Y'); ?> The Church of Pentecost - Madina Area. All rights reserved.
            </p>
        </div>
    </footer>
</body>
</html>

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