Sindbad~EG File Manager

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

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

$pageTitle = "Attendance Admin - " . APP_NAME;

$db = Database::getInstance()->getConnection();
$stmt = $db->query("SELECT * FROM general_settings ORDER BY id DESC LIMIT 1");
$settings = $stmt->fetch();

$settings = array_merge([
    'site_title' => 'Church Membership System',
    'theme_primary_color' => '#1E40AF',
    'theme_secondary_color' => '#F97316',
], $settings ?: []);

$error = '';
$success = '';
$adminCode = $_GET['code'] ?? $_SESSION['temp_admin_code'] ?? '';
$userData = null;
$assemblyId = null;
$programs = [];
$selectedProgram = null;
$attendanceList = [];
$stats = [];

// Verify admin code
if (empty($adminCode) && !isset($_SESSION['temp_admin_id'])) {
    header('Location: attendance.php');
    exit();
}

// Get admin user data
try {
    if (isset($_SESSION['temp_admin_id'])) {
        $stmt = $db->prepare("
            SELECT u.*, a.assembly_name, d.district_name, ar.area_name
            FROM users u
            LEFT JOIN assemblies a ON u.assembly_id = a.id
            LEFT JOIN districts d ON u.district_id = d.id
            LEFT JOIN areas ar ON u.area_id = ar.id
            WHERE u.id = :id AND u.is_active = 1
        ");
        $stmt->execute(['id' => $_SESSION['temp_admin_id']]);
        $userData = $stmt->fetch();
    } else {
        $stmt = $db->prepare("
            SELECT u.*, mc.tracking_code, a.assembly_name, d.district_name, ar.area_name
            FROM users u
            LEFT JOIN memberuser_codes mc ON mc.user_id = u.id AND mc.code_type = 'user' AND mc.is_active = 1
            LEFT JOIN assemblies a ON u.assembly_id = a.id
            LEFT JOIN districts d ON u.district_id = d.id
            LEFT JOIN areas ar ON u.area_id = ar.id
            WHERE mc.tracking_code = :code AND u.is_active = 1
            LIMIT 1
        ");
        $stmt->execute(['code' => $adminCode]);
        $userData = $stmt->fetch();
        
        if ($userData) {
            $_SESSION['temp_admin_id'] = $userData['id'];
            $_SESSION['temp_admin_assembly'] = $userData['assembly_id'];
            $_SESSION['temp_admin_code'] = $adminCode;
        }
    }
    
    if (!$userData) {
        header('Location: attendance.php?error=invalid_code');
        exit();
    }
    
    $assemblyId = $userData['assembly_id'];
    
} catch (Exception $e) {
    $error = "Error: " . $e->getMessage();
}

// Get programs for this assembly
$progStmt = $db->prepare("
    SELECT p.*,
        (SELECT COUNT(*) FROM program_attendance pa WHERE pa.program_id = p.id AND DATE(pa.check_in_time) = CURDATE()) as today_checkins,
        (SELECT COUNT(*) FROM program_attendance pa WHERE pa.program_id = p.id AND DATE(pa.check_in_time) = CURDATE() AND pa.check_out_time IS NOT NULL) as today_checkouts
    FROM programs p
    WHERE p.is_active = 1 
    AND (p.assembly_id = :assembly_id OR p.assembly_id IS NULL)
    ORDER BY p.program_name
");
$progStmt->execute(['assembly_id' => $assemblyId]);
$programs = $progStmt->fetchAll();

// Get selected program
$selectedProgramId = $_GET['program_id'] ?? $_POST['program_id'] ?? null;
if ($selectedProgramId) {
    $selStmt = $db->prepare("SELECT * FROM programs WHERE id = :id");
    $selStmt->execute(['id' => $selectedProgramId]);
    $selectedProgram = $selStmt->fetch();
}

// Handle manual check-in
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['manual_checkin'])) {
    try {
        $memberId = $_POST['member_id'];
        $programId = $_POST['program_id'];
        
        // Check if already checked in
        $checkStmt = $db->prepare("
            SELECT id FROM program_attendance 
            WHERE member_id = :member_id AND program_id = :program_id 
            AND DATE(check_in_time) = CURDATE()
        ");
        $checkStmt->execute(['member_id' => $memberId, 'program_id' => $programId]);
        
        if (!$checkStmt->fetch()) {
            $insertStmt = $db->prepare("
                INSERT INTO program_attendance (program_id, member_id, check_in_time, attendance_type, checked_in_by)
                VALUES (:program_id, :member_id, NOW(), 'manual', :checked_in_by)
            ");
            $insertStmt->execute([
                'program_id' => $programId,
                'member_id' => $memberId,
                'checked_in_by' => $userData['id']
            ]);
            $success = "Member checked in successfully!";
        } else {
            $error = "Member already checked in today.";
        }
    } catch (Exception $e) {
        $error = "Check-in failed: " . $e->getMessage();
    }
}

// Handle manual check-out
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['manual_checkout'])) {
    try {
        $attendanceId = $_POST['attendance_id'];
        $updateStmt = $db->prepare("
            UPDATE program_attendance SET check_out_time = NOW() WHERE id = :id
        ");
        $updateStmt->execute(['id' => $attendanceId]);
        $success = "Member checked out successfully!";
    } catch (Exception $e) {
        $error = "Check-out failed: " . $e->getMessage();
    }
}

// Handle guest check-in
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['guest_checkin'])) {
    try {
        $programId = $_POST['program_id'];
        $guestName = trim($_POST['guest_name']);
        $guestPhone = trim($_POST['guest_phone'] ?? '');
        $guestType = $_POST['guest_type'] ?? 'visitor';
        
        if (empty($guestName)) {
            throw new Exception("Guest name is required");
        }
        
        $insertStmt = $db->prepare("
            INSERT INTO program_attendance (program_id, check_in_time, attendance_type, guest_name, guest_phone, guest_type, checked_in_by)
            VALUES (:program_id, NOW(), 'guest', :guest_name, :guest_phone, :guest_type, :checked_in_by)
        ");
        $insertStmt->execute([
            'program_id' => $programId,
            'guest_name' => $guestName,
            'guest_phone' => $guestPhone,
            'guest_type' => $guestType,
            'checked_in_by' => $userData['id']
        ]);
        $success = "Guest '{$guestName}' checked in successfully!";
    } catch (Exception $e) {
        $error = "Guest check-in failed: " . $e->getMessage();
    }
}

// Handle end program
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['end_program'])) {
    try {
        $programId = $_POST['program_id'];
        
        // Check out all members who haven't checked out
        $updateStmt = $db->prepare("
            UPDATE program_attendance 
            SET check_out_time = NOW() 
            WHERE program_id = :program_id 
            AND DATE(check_in_time) = CURDATE() 
            AND check_out_time IS NULL
        ");
        $updateStmt->execute(['program_id' => $programId]);
        
        $success = "Program ended. All attendees have been checked out.";
    } catch (Exception $e) {
        $error = "Failed to end program: " . $e->getMessage();
    }
}

// Get attendance list for selected program
if ($selectedProgram) {
    $attStmt = $db->prepare("
        SELECT pa.*, 
               m.first_name, m.last_name, m.membershipcard_id, m.phone as member_phone,
               TIMESTAMPDIFF(MINUTE, pa.check_in_time, COALESCE(pa.check_out_time, NOW())) as duration_minutes
        FROM program_attendance pa
        LEFT JOIN members m ON pa.member_id = m.id
        WHERE pa.program_id = :program_id AND DATE(pa.check_in_time) = CURDATE()
        ORDER BY pa.check_in_time DESC
    ");
    $attStmt->execute(['program_id' => $selectedProgram['id']]);
    $attendanceList = $attStmt->fetchAll();
}

// Get stats for assembly
$totalMembers = 0;
$memberStmt = $db->prepare("SELECT COUNT(*) FROM members WHERE assembly_id = :assembly_id AND is_active = 1");
$memberStmt->execute(['assembly_id' => $assemblyId]);
$totalMembers = $memberStmt->fetchColumn();

$todayTotalCheckins = 0;
$todayStmt = $db->prepare("
    SELECT COUNT(DISTINCT pa.member_id) 
    FROM program_attendance pa
    JOIN programs p ON pa.program_id = p.id
    WHERE (p.assembly_id = :assembly_id OR p.assembly_id IS NULL)
    AND DATE(pa.check_in_time) = CURDATE()
    AND pa.member_id IS NOT NULL
");
$todayStmt->execute(['assembly_id' => $assemblyId]);
$todayTotalCheckins = $todayStmt->fetchColumn();

$todayGuests = 0;
$guestStmt = $db->prepare("
    SELECT COUNT(*) 
    FROM program_attendance pa
    JOIN programs p ON pa.program_id = p.id
    WHERE (p.assembly_id = :assembly_id OR p.assembly_id IS NULL)
    AND DATE(pa.check_in_time) = CURDATE()
    AND pa.guest_name IS NOT NULL
");
$guestStmt->execute(['assembly_id' => $assemblyId]);
$todayGuests = $guestStmt->fetchColumn();

// Get members for manual check-in dropdown
$membersStmt = $db->prepare("
    SELECT id, first_name, last_name, membershipcard_id 
    FROM members 
    WHERE assembly_id = :assembly_id AND is_active = 1 
    ORDER BY first_name, last_name
");
$membersStmt->execute(['assembly_id' => $assemblyId]);
$assemblyMembers = $membersStmt->fetchAll();

// Handle export
if (isset($_GET['export']) && $selectedProgram) {
    $exportType = $_GET['export'];
    
    if ($exportType === 'csv') {
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="attendance_' . $selectedProgram['program_name'] . '_' . date('Y-m-d') . '.csv"');
        
        $output = fopen('php://output', 'w');
        fputcsv($output, ['#', 'Name', 'Type', 'Member ID', 'Phone', 'Check In', 'Check Out', 'Duration']);
        
        $i = 1;
        foreach ($attendanceList as $att) {
            $name = $att['member_id'] ? ($att['first_name'] . ' ' . $att['last_name']) : $att['guest_name'];
            $type = $att['member_id'] ? 'Member' : ucfirst($att['guest_type'] ?? 'Guest');
            $phone = $att['member_id'] ? $att['member_phone'] : $att['guest_phone'];
            
            fputcsv($output, [
                $i++,
                $name,
                $type,
                $att['membershipcard_id'] ?? 'N/A',
                $phone ?? 'N/A',
                date('g:i A', strtotime($att['check_in_time'])),
                $att['check_out_time'] ? date('g:i A', strtotime($att['check_out_time'])) : 'Still Present',
                $att['duration_minutes'] . ' min'
            ]);
        }
        fclose($output);
        exit();
    }
    
    if ($exportType === 'report') {
        // Generate attendance report
        $presentIds = array_filter(array_column($attendanceList, 'member_id'));
        
        // Get absent members
        $absentStmt = $db->prepare("
            SELECT id, first_name, last_name, membershipcard_id, phone
            FROM members 
            WHERE assembly_id = :assembly_id AND is_active = 1
            " . (!empty($presentIds) ? "AND id NOT IN (" . implode(',', $presentIds) . ")" : "") . "
            ORDER BY first_name, last_name
        ");
        $absentStmt->execute(['assembly_id' => $assemblyId]);
        $absentMembers = $absentStmt->fetchAll();
        
        header('Content-Type: text/html; charset=utf-8');
        ?>
        <!DOCTYPE html>
        <html>
        <head>
            <title>Attendance Report - <?php echo htmlspecialchars($selectedProgram['program_name']); ?></title>
            <style>
                body { font-family: Arial, sans-serif; margin: 20px; font-size: 12px; }
                h1 { color: #1E40AF; font-size: 20px; }
                h2 { color: #333; font-size: 16px; margin-top: 30px; }
                .info { color: #666; margin-bottom: 20px; }
                table { width: 100%; border-collapse: collapse; margin-top: 10px; }
                th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
                th { background-color: #1E40AF; color: white; }
                .present { background-color: #d1fae5; }
                .absent { background-color: #fee2e2; }
                .guest { background-color: #fef3c7; }
                .stats { display: flex; gap: 20px; margin: 20px 0; }
                .stat-box { padding: 15px; border-radius: 8px; text-align: center; }
                .print-btn { background: #1E40AF; color: white; padding: 10px 20px; border: none; cursor: pointer; margin-bottom: 20px; }
                @media print { .print-btn { display: none; } }
            </style>
        </head>
        <body>
            <button class="print-btn" onclick="window.print()">Print Report</button>
            
            <h1><?php echo htmlspecialchars($settings['site_title']); ?></h1>
            <h2><?php echo htmlspecialchars($selectedProgram['program_name']); ?> - Attendance Report</h2>
            <p class="info">
                Date: <?php echo date('F j, Y'); ?> | 
                Assembly: <?php echo htmlspecialchars($userData['assembly_name'] ?? 'N/A'); ?>
            </p>
            
            <div class="stats">
                <div class="stat-box present">
                    <strong><?php echo count(array_filter($attendanceList, fn($a) => $a['member_id'])); ?></strong><br>Members Present
                </div>
                <div class="stat-box absent">
                    <strong><?php echo count($absentMembers); ?></strong><br>Members Absent
                </div>
                <div class="stat-box guest">
                    <strong><?php echo count(array_filter($attendanceList, fn($a) => !$a['member_id'])); ?></strong><br>Guests/Visitors
                </div>
            </div>
            
            <h2 style="color: green;">✓ Present (<?php echo count(array_filter($attendanceList, fn($a) => $a['member_id'])); ?>)</h2>
            <table>
                <thead>
                    <tr><th>#</th><th>Name</th><th>Member ID</th><th>Check In</th><th>Check Out</th></tr>
                </thead>
                <tbody>
                    <?php $i = 1; foreach ($attendanceList as $att): if ($att['member_id']): ?>
                    <tr class="present">
                        <td><?php echo $i++; ?></td>
                        <td><?php echo htmlspecialchars($att['first_name'] . ' ' . $att['last_name']); ?></td>
                        <td><?php echo htmlspecialchars($att['membershipcard_id'] ?? 'N/A'); ?></td>
                        <td><?php echo date('g:i A', strtotime($att['check_in_time'])); ?></td>
                        <td><?php echo $att['check_out_time'] ? date('g:i A', strtotime($att['check_out_time'])) : 'Still Present'; ?></td>
                    </tr>
                    <?php endif; endforeach; ?>
                </tbody>
            </table>
            
            <h2 style="color: red;">✗ Absent (<?php echo count($absentMembers); ?>)</h2>
            <table>
                <thead>
                    <tr><th>#</th><th>Name</th><th>Member ID</th><th>Phone</th></tr>
                </thead>
                <tbody>
                    <?php $i = 1; foreach ($absentMembers as $member): ?>
                    <tr class="absent">
                        <td><?php echo $i++; ?></td>
                        <td><?php echo htmlspecialchars($member['first_name'] . ' ' . $member['last_name']); ?></td>
                        <td><?php echo htmlspecialchars($member['membershipcard_id'] ?? 'N/A'); ?></td>
                        <td><?php echo htmlspecialchars($member['phone'] ?? 'N/A'); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            
            <?php $guests = array_filter($attendanceList, fn($a) => !$a['member_id']); if (!empty($guests)): ?>
            <h2 style="color: orange;">★ Guests & Visitors (<?php echo count($guests); ?>)</h2>
            <table>
                <thead>
                    <tr><th>#</th><th>Name</th><th>Type</th><th>Phone</th><th>Check In</th></tr>
                </thead>
                <tbody>
                    <?php $i = 1; foreach ($guests as $guest): ?>
                    <tr class="guest">
                        <td><?php echo $i++; ?></td>
                        <td><?php echo htmlspecialchars($guest['guest_name']); ?></td>
                        <td><?php echo htmlspecialchars(ucfirst($guest['guest_type'] ?? 'Guest')); ?></td>
                        <td><?php echo htmlspecialchars($guest['guest_phone'] ?? 'N/A'); ?></td>
                        <td><?php echo date('g:i A', strtotime($guest['check_in_time'])); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>
            
            <p style="margin-top: 30px; color: #666; font-size: 10px;">
                Generated on <?php echo date('F j, Y g:i A'); ?> by <?php echo htmlspecialchars($userData['full_name'] ?? 'Admin'); ?>
            </p>
        </body>
        </html>
        <?php
        exit();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .hero-gradient { background: linear-gradient(135deg, #1E40AF 0%, #9333EA 50%, #F97316 100%); }
        .btn-gradient { background: linear-gradient(135deg, #1E40AF 0%, #9333EA 100%); }
        .btn-gradient-orange { background: linear-gradient(135deg, #F97316 0%, #FBBF24 100%); }
        .card-hover:hover { transform: translateY(-3px); box-shadow: 0 15px 30px rgba(0,0,0,0.12); }
        .card-hover { transition: all 0.3s ease; }
        @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
        .animate-fadeIn { animation: fadeIn 0.3s ease-out; }
    </style>
</head>
<body class="bg-gray-100 min-h-screen">
    <!-- Header -->
    <header class="bg-white shadow-lg sticky top-0 z-50">
        <div class="container mx-auto px-4">
            <div class="flex items-center justify-between h-16">
                <div class="flex items-center space-x-3">
                    <div class="w-10 h-10 rounded-lg flex items-center justify-center hero-gradient">
                        <i class="fas fa-clipboard-check text-white"></i>
                    </div>
                    <div>
                        <h1 class="text-lg font-bold text-gray-800">Attendance Admin</h1>
                        <p class="text-xs text-gray-500"><?php echo htmlspecialchars($userData['assembly_name'] ?? 'Assembly'); ?></p>
                    </div>
                </div>
                
                <div class="flex items-center space-x-4">
                    <span class="text-sm text-gray-600 hidden sm:block">
                        <i class="fas fa-user-shield mr-1"></i><?php echo htmlspecialchars($userData['full_name'] ?? 'Admin'); ?>
                    </span>
                    <a href="attendance.php" class="text-red-600 hover:text-red-800">
                        <i class="fas fa-sign-out-alt mr-1"></i>Exit
                    </a>
                </div>
            </div>
        </div>
    </header>

    <main class="container mx-auto px-4 py-6">
        <?php if ($error): ?>
        <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded mb-4">
            <i class="fas fa-exclamation-circle mr-2"></i><?php echo htmlspecialchars($error); ?>
        </div>
        <?php endif; ?>
        
        <?php if ($success): ?>
        <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 rounded mb-4">
            <i class="fas fa-check-circle mr-2"></i><?php echo htmlspecialchars($success); ?>
        </div>
        <?php endif; ?>
        
        <!-- Stats Cards -->
        <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-lg shadow p-4 border-l-4 border-blue-500">
                <p class="text-xs text-gray-500 uppercase">Total Members</p>
                <p class="text-2xl font-bold text-gray-800"><?php echo $totalMembers; ?></p>
            </div>
            <div class="bg-white rounded-lg shadow p-4 border-l-4 border-green-500">
                <p class="text-xs text-gray-500 uppercase">Today's Check-ins</p>
                <p class="text-2xl font-bold text-green-600"><?php echo $todayTotalCheckins; ?></p>
            </div>
            <div class="bg-white rounded-lg shadow p-4 border-l-4 border-yellow-500">
                <p class="text-xs text-gray-500 uppercase">Guests Today</p>
                <p class="text-2xl font-bold text-yellow-600"><?php echo $todayGuests; ?></p>
            </div>
            <div class="bg-white rounded-lg shadow p-4 border-l-4 border-purple-500">
                <p class="text-xs text-gray-500 uppercase">Active Programs</p>
                <p class="text-2xl font-bold text-purple-600"><?php echo count($programs); ?></p>
            </div>
        </div>
        
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <!-- Left: Program Selection -->
            <div class="lg:col-span-1">
                <div class="bg-white rounded-lg shadow p-4 mb-4">
                    <h3 class="font-bold text-gray-800 mb-3">
                        <i class="fas fa-calendar-alt mr-2 text-blue-500"></i>Select Program
                    </h3>
                    <div class="space-y-2">
                        <?php foreach ($programs as $prog): ?>
                        <a href="?code=<?php echo urlencode($adminCode); ?>&program_id=<?php echo $prog['id']; ?>" 
                           class="block p-3 rounded-lg border <?php echo ($selectedProgram && $selectedProgram['id'] == $prog['id']) ? 'border-blue-500 bg-blue-50' : 'border-gray-200 hover:border-blue-300'; ?> transition">
                            <div class="flex justify-between items-center">
                                <div>
                                    <p class="font-medium text-gray-800"><?php echo htmlspecialchars($prog['program_name']); ?></p>
                                    <p class="text-xs text-gray-500">
                                        <?php echo $prog['start_time'] ? date('g:i A', strtotime($prog['start_time'])) : 'Flexible'; ?>
                                    </p>
                                </div>
                                <span class="bg-green-100 text-green-800 text-xs px-2 py-1 rounded-full">
                                    <?php echo $prog['today_checkins']; ?>
                                </span>
                            </div>
                        </a>
                        <?php endforeach; ?>
                        
                        <?php if (empty($programs)): ?>
                        <p class="text-gray-500 text-center py-4">No active programs</p>
                        <?php endif; ?>
                    </div>
                </div>
                
                <!-- Quick Actions -->
                <?php if ($selectedProgram): ?>
                <div class="bg-white rounded-lg shadow p-4">
                    <h3 class="font-bold text-gray-800 mb-3">
                        <i class="fas fa-bolt mr-2 text-yellow-500"></i>Quick Actions
                    </h3>
                    <div class="space-y-2">
                        <a href="?code=<?php echo urlencode($adminCode); ?>&program_id=<?php echo $selectedProgram['id']; ?>&export=csv" 
                           class="block w-full text-center py-2 bg-green-500 text-white rounded-lg hover:bg-green-600 transition">
                            <i class="fas fa-file-csv mr-1"></i>Export CSV
                        </a>
                        <a href="?code=<?php echo urlencode($adminCode); ?>&program_id=<?php echo $selectedProgram['id']; ?>&export=report" 
                           target="_blank"
                           class="block w-full text-center py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition">
                            <i class="fas fa-file-alt mr-1"></i>Full Report
                        </a>
                        <form method="POST" onsubmit="return confirm('End program and check out all attendees?')">
                            <input type="hidden" name="program_id" value="<?php echo $selectedProgram['id']; ?>">
                            <button type="submit" name="end_program" 
                                    class="w-full py-2 bg-red-500 text-white rounded-lg hover:bg-red-600 transition">
                                <i class="fas fa-stop-circle mr-1"></i>End Program
                            </button>
                        </form>
                    </div>
                </div>
                <?php endif; ?>
            </div>
            
            <!-- Right: Attendance Management -->
            <div class="lg:col-span-2">
                <?php if ($selectedProgram): ?>
                <!-- Program Header -->
                <div class="bg-gradient-to-r from-blue-500 to-purple-600 text-white rounded-lg p-4 mb-4">
                    <h2 class="text-xl font-bold"><?php echo htmlspecialchars($selectedProgram['program_name']); ?></h2>
                    <p class="text-white/80 text-sm">
                        <i class="fas fa-clock mr-1"></i>
                        <?php echo $selectedProgram['start_time'] ? date('g:i A', strtotime($selectedProgram['start_time'])) : 'Flexible'; ?>
                        <?php if ($selectedProgram['end_time']): ?> - <?php echo date('g:i A', strtotime($selectedProgram['end_time'])); ?><?php endif; ?>
                        | <i class="fas fa-users ml-2 mr-1"></i><?php echo count($attendanceList); ?> checked in today
                    </p>
                </div>
                
                <!-- Check-in Forms -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-4">
                    <!-- Manual Member Check-in -->
                    <div class="bg-white rounded-lg shadow p-4">
                        <h4 class="font-bold text-gray-800 mb-3">
                            <i class="fas fa-user-plus mr-2 text-green-500"></i>Check In Member
                        </h4>
                        <form method="POST">
                            <input type="hidden" name="program_id" value="<?php echo $selectedProgram['id']; ?>">
                            <select name="member_id" required class="w-full px-3 py-2 border border-gray-300 rounded-lg mb-2">
                                <option value="">Select Member...</option>
                                <?php foreach ($assemblyMembers as $member): ?>
                                <option value="<?php echo $member['id']; ?>">
                                    <?php echo htmlspecialchars($member['first_name'] . ' ' . $member['last_name']); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                            <button type="submit" name="manual_checkin" class="w-full py-2 bg-green-500 text-white rounded-lg hover:bg-green-600">
                                <i class="fas fa-check mr-1"></i>Check In
                            </button>
                        </form>
                    </div>
                    
                    <!-- Guest Check-in -->
                    <div class="bg-white rounded-lg shadow p-4">
                        <h4 class="font-bold text-gray-800 mb-3">
                            <i class="fas fa-user-friends mr-2 text-yellow-500"></i>Check In Guest
                        </h4>
                        <form method="POST">
                            <input type="hidden" name="program_id" value="<?php echo $selectedProgram['id']; ?>">
                            <input type="text" name="guest_name" placeholder="Guest Name" required
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg mb-2">
                            <input type="tel" name="guest_phone" placeholder="Phone (optional)"
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg mb-2">
                            <select name="guest_type" class="w-full px-3 py-2 border border-gray-300 rounded-lg mb-2">
                                <option value="visitor">Visitor</option>
                                <option value="guest">Guest</option>
                                <option value="first_timer">First Timer</option>
                            </select>
                            <button type="submit" name="guest_checkin" class="w-full py-2 bg-yellow-500 text-white rounded-lg hover:bg-yellow-600">
                                <i class="fas fa-plus mr-1"></i>Add Guest
                            </button>
                        </form>
                    </div>
                </div>
                
                <!-- Attendance List -->
                <div class="bg-white rounded-lg shadow overflow-hidden">
                    <div class="p-4 border-b border-gray-200 flex justify-between items-center">
                        <h4 class="font-bold text-gray-800">
                            <i class="fas fa-list mr-2 text-blue-500"></i>Today's Attendance
                        </h4>
                        <span class="text-sm text-gray-500"><?php echo count($attendanceList); ?> total</span>
                    </div>
                    
                    <div class="max-h-96 overflow-y-auto">
                        <?php if (empty($attendanceList)): ?>
                        <div class="text-center py-8 text-gray-500">
                            <i class="fas fa-inbox text-4xl mb-2"></i>
                            <p>No check-ins yet today</p>
                        </div>
                        <?php else: ?>
                        <table class="w-full">
                            <thead class="bg-gray-50 sticky top-0">
                                <tr>
                                    <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Name</th>
                                    <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Type</th>
                                    <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">In</th>
                                    <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Out</th>
                                    <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Action</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-200">
                                <?php foreach ($attendanceList as $att): ?>
                                <tr class="hover:bg-gray-50">
                                    <td class="px-4 py-3">
                                        <p class="font-medium text-gray-800">
                                            <?php echo $att['member_id'] ? htmlspecialchars($att['first_name'] . ' ' . $att['last_name']) : htmlspecialchars($att['guest_name']); ?>
                                        </p>
                                        <?php if ($att['membershipcard_id']): ?>
                                        <p class="text-xs text-gray-500"><?php echo htmlspecialchars($att['membershipcard_id']); ?></p>
                                        <?php endif; ?>
                                    </td>
                                    <td class="px-4 py-3">
                                        <?php if ($att['member_id']): ?>
                                        <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded-full">Member</span>
                                        <?php else: ?>
                                        <span class="bg-yellow-100 text-yellow-800 text-xs px-2 py-1 rounded-full"><?php echo ucfirst($att['guest_type'] ?? 'Guest'); ?></span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="px-4 py-3 text-sm text-gray-600">
                                        <?php echo date('g:i A', strtotime($att['check_in_time'])); ?>
                                    </td>
                                    <td class="px-4 py-3 text-sm">
                                        <?php if ($att['check_out_time']): ?>
                                        <span class="text-gray-600"><?php echo date('g:i A', strtotime($att['check_out_time'])); ?></span>
                                        <?php else: ?>
                                        <span class="text-green-600 font-medium">Present</span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="px-4 py-3">
                                        <?php if (!$att['check_out_time']): ?>
                                        <form method="POST" class="inline">
                                            <input type="hidden" name="attendance_id" value="<?php echo $att['id']; ?>">
                                            <button type="submit" name="manual_checkout" 
                                                    class="text-orange-600 hover:text-orange-800 text-sm">
                                                <i class="fas fa-sign-out-alt mr-1"></i>Check Out
                                            </button>
                                        </form>
                                        <?php else: ?>
                                        <span class="text-gray-400 text-sm">Done</span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                        <?php endif; ?>
                    </div>
                </div>
                
                <?php else: ?>
                <!-- No Program Selected -->
                <div class="bg-white rounded-lg shadow p-8 text-center">
                    <i class="fas fa-hand-pointer text-6xl text-gray-300 mb-4"></i>
                    <h3 class="text-xl font-bold text-gray-800 mb-2">Select a Program</h3>
                    <p class="text-gray-600">Choose a program from the left to manage attendance</p>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </main>

    <script>
    // Auto-refresh attendance list every 30 seconds
    <?php if ($selectedProgram): ?>
    setInterval(function() {
        location.reload();
    }, 30000);
    <?php endif; ?>
    </script>
</body>
</html>

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