Sindbad~EG File Manager

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

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

$pageTitle = "Ministry Attendance - " . 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 = '';
$userType = $_POST['user_type'] ?? $_GET['type'] ?? '';
$userCode = $_POST['user_code'] ?? '';
$step = 'select_type'; // select_type, enter_code, select_ministry, take_attendance, admin_dashboard

$memberData = null;
$userData = null;
$userMinistries = [];
$selectedMinistry = null;
$ministryMembers = [];
$todayAttendance = [];

// Handle user code verification
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['verify_code'])) {
    $userCode = strtoupper(trim($_POST['user_code']));
    $userType = $_POST['user_type'];
    
    if (empty($userCode)) {
        $error = "Please enter your code";
        $step = 'enter_code';
    } else {
        try {
            if ($userType === 'member') {
                // Look up member by tracking code, code, or membershipcard_id
                $stmt = $db->prepare("
                    SELECT m.*, mc.tracking_code, mc.code as member_code, a.assembly_name, d.district_name, ar.area_name
                    FROM members m
                    LEFT JOIN memberuser_codes mc ON mc.member_id = m.id AND mc.code_type = 'member' AND mc.is_active = 1
                    LEFT JOIN assemblies a ON m.assembly_id = a.id
                    LEFT JOIN districts d ON m.district_id = d.id
                    LEFT JOIN areas ar ON m.area_id = ar.id
                    WHERE (mc.tracking_code = :code1 OR mc.code = :code2 OR m.membershipcard_id = :code3)
                    AND m.is_active = 1
                    LIMIT 1
                ");
                $stmt->execute(['code1' => $userCode, 'code2' => $userCode, 'code3' => $userCode]);
                $memberData = $stmt->fetch();
                
                if ($memberData) {
                    // Get ministries from the member's assembly
                    $ministryStmt = $db->prepare("
                        SELECT DISTINCT m.*, mc.category_name, mc.icon, mc.color
                        FROM ministries m
                        LEFT JOIN ministry_categories mc ON m.category_id = mc.id
                        WHERE m.is_active = 1 
                        AND m.assembly_id = :assembly_id
                        ORDER BY m.ministry_name
                    ");
                    $ministryStmt->execute([
                        'assembly_id' => $memberData['assembly_id']
                    ]);
                    $userMinistries = $ministryStmt->fetchAll();
                    
                    if (count($userMinistries) > 0) {
                        $step = 'select_ministry';
                    } else {
                        $error = "No ministries found for your assembly.";
                        $step = 'enter_code';
                    }
                } else {
                    $error = "Invalid member code. Please check and try again.";
                    $step = 'enter_code';
                }
            } else {
                // Admin/User code lookup
                $stmt = $db->prepare("
                    SELECT u.*, mc.tracking_code, mc.code as user_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 = :code1 OR mc.code = :code2)
                    AND u.is_active = 1
                    LIMIT 1
                ");
                $stmt->execute(['code1' => $userCode, 'code2' => $userCode]);
                $userData = $stmt->fetch();
                
                if ($userData) {
                    // Get ministries for admin based on their access level
                    $ministryStmt = $db->prepare("
                        SELECT m.*, mc.category_name, mc.icon, mc.color
                        FROM ministries m
                        LEFT JOIN ministry_categories mc ON m.category_id = mc.id
                        WHERE m.is_active = 1
                        AND (m.area_id = :area_id OR m.district_id = :district_id OR m.assembly_id = :assembly_id OR m.area_id IS NULL)
                        ORDER BY m.ministry_name
                    ");
                    $ministryStmt->execute([
                        'area_id' => $userData['area_id'],
                        'district_id' => $userData['district_id'],
                        'assembly_id' => $userData['assembly_id']
                    ]);
                    $userMinistries = $ministryStmt->fetchAll();
                    
                    $_SESSION['temp_ministry_admin_id'] = $userData['id'];
                    $_SESSION['temp_ministry_admin_code'] = $userCode;
                    $step = 'admin_dashboard';
                } else {
                    $error = "Invalid admin code. Please check and try again.";
                    $step = 'enter_code';
                }
            }
        } catch (Exception $e) {
            $error = "Error: " . $e->getMessage();
            $step = 'enter_code';
        }
    }
}

// Handle ministry selection
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['select_ministry'])) {
    $ministryId = intval($_POST['ministry_id']);
    $memberCode = $_POST['member_code'] ?? '';
    
    // Re-fetch member data
    $stmt = $db->prepare("
        SELECT m.*, a.assembly_name, d.district_name, ar.area_name
        FROM members m
        LEFT JOIN assemblies a ON m.assembly_id = a.id
        LEFT JOIN districts d ON m.district_id = d.id
        LEFT JOIN areas ar ON m.area_id = ar.id
        WHERE m.id = :id
    ");
    $stmt->execute(['id' => $_POST['member_id']]);
    $memberData = $stmt->fetch();
    
    // Get selected ministry
    $ministryStmt = $db->prepare("
        SELECT m.*, mc.category_name, mc.icon, mc.color
        FROM ministries m
        LEFT JOIN ministry_categories mc ON m.category_id = mc.id
        WHERE m.id = :id
    ");
    $ministryStmt->execute(['id' => $ministryId]);
    $selectedMinistry = $ministryStmt->fetch();
    
    if ($selectedMinistry) {
        // Get ministry members
        $membersStmt = $db->prepare("
            SELECT mm.*, m.id as member_id, m.first_name, m.last_name, m.membershipcard_id, m.phone
            FROM ministry_members mm
            JOIN members m ON mm.member_id = m.id
            WHERE mm.ministry_id = :ministry_id AND mm.status = 'active'
            ORDER BY m.first_name, m.last_name
        ");
        $membersStmt->execute(['ministry_id' => $ministryId]);
        $ministryMembers = $membersStmt->fetchAll();
        
        // Get today's attendance
        $attendanceStmt = $db->prepare("
            SELECT ma.*, m.first_name, m.last_name
            FROM ministry_attendance ma
            LEFT JOIN members m ON ma.member_id = m.id
            WHERE ma.ministry_id = :ministry_id AND ma.attendance_date = CURDATE()
            ORDER BY ma.check_in_time DESC
        ");
        $attendanceStmt->execute(['ministry_id' => $ministryId]);
        $todayAttendance = $attendanceStmt->fetchAll();
        
        $step = 'take_attendance';
    }
}

// Handle member check-in
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['checkin_member'])) {
    try {
        $ministryId = intval($_POST['ministry_id']);
        $memberId = intval($_POST['checkin_member_id']);
        $markedBy = intval($_POST['marked_by'] ?? 0);
        
        // Check if already checked in today
        $checkStmt = $db->prepare("
            SELECT id FROM ministry_attendance 
            WHERE ministry_id = :ministry_id AND member_id = :member_id AND attendance_date = CURDATE()
        ");
        $checkStmt->execute(['ministry_id' => $ministryId, 'member_id' => $memberId]);
        
        if (!$checkStmt->fetch()) {
            $insertStmt = $db->prepare("
                INSERT INTO ministry_attendance (ministry_id, member_id, attendance_date, check_in_time, attendance_type, status, marked_by)
                VALUES (:ministry_id, :member_id, CURDATE(), NOW(), 'member', 'present', :marked_by)
            ");
            $insertStmt->execute([
                'ministry_id' => $ministryId,
                'member_id' => $memberId,
                'marked_by' => $markedBy ?: null
            ]);
            $success = "Member checked in successfully!";
        } else {
            $error = "Member already checked in today.";
        }
        
        // Reload ministry data
        $_POST['select_ministry'] = true;
        $_POST['member_id'] = $_POST['current_member_id'];
    } catch (Exception $e) {
        $error = "Check-in failed: " . $e->getMessage();
    }
}

// Handle guest check-in
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['checkin_guest'])) {
    try {
        $ministryId = intval($_POST['ministry_id']);
        $guestName = trim($_POST['guest_name']);
        $guestPhone = trim($_POST['guest_phone'] ?? '');
        $guestType = $_POST['guest_type'] ?? 'guest';
        $markedBy = intval($_POST['marked_by'] ?? 0);
        
        if (empty($guestName)) {
            throw new Exception("Guest name is required.");
        }
        
        $insertStmt = $db->prepare("
            INSERT INTO ministry_attendance (ministry_id, attendance_date, check_in_time, attendance_type, guest_name, guest_phone, status, marked_by)
            VALUES (:ministry_id, CURDATE(), NOW(), :attendance_type, :guest_name, :guest_phone, 'present', :marked_by)
        ");
        $insertStmt->execute([
            'ministry_id' => $ministryId,
            'attendance_type' => $guestType,
            'guest_name' => $guestName,
            'guest_phone' => $guestPhone ?: null,
            'marked_by' => $markedBy ?: null
        ]);
        $success = "Guest checked in successfully!";
        
        // Reload ministry data
        $_POST['select_ministry'] = true;
        $_POST['member_id'] = $_POST['current_member_id'];
    } catch (Exception $e) {
        $error = "Guest check-in failed: " . $e->getMessage();
    }
}

// Reload ministry data after check-in
if (isset($_POST['select_ministry']) && isset($_POST['ministry_id'])) {
    $ministryId = intval($_POST['ministry_id']);
    
    $stmt = $db->prepare("
        SELECT m.*, a.assembly_name, d.district_name, ar.area_name
        FROM members m
        LEFT JOIN assemblies a ON m.assembly_id = a.id
        LEFT JOIN districts d ON m.district_id = d.id
        LEFT JOIN areas ar ON m.area_id = ar.id
        WHERE m.id = :id
    ");
    $stmt->execute(['id' => $_POST['member_id']]);
    $memberData = $stmt->fetch();
    
    $ministryStmt = $db->prepare("
        SELECT m.*, mc.category_name, mc.icon, mc.color
        FROM ministries m
        LEFT JOIN ministry_categories mc ON m.category_id = mc.id
        WHERE m.id = :id
    ");
    $ministryStmt->execute(['id' => $ministryId]);
    $selectedMinistry = $ministryStmt->fetch();
    
    $membersStmt = $db->prepare("
        SELECT mm.*, m.id as member_id, m.first_name, m.last_name, m.membershipcard_id, m.phone
        FROM ministry_members mm
        JOIN members m ON mm.member_id = m.id
        WHERE mm.ministry_id = :ministry_id AND mm.status = 'active'
        ORDER BY m.first_name, m.last_name
    ");
    $membersStmt->execute(['ministry_id' => $ministryId]);
    $ministryMembers = $membersStmt->fetchAll();
    
    $attendanceStmt = $db->prepare("
        SELECT ma.*, m.first_name, m.last_name
        FROM ministry_attendance ma
        LEFT JOIN members m ON ma.member_id = m.id
        WHERE ma.ministry_id = :ministry_id AND ma.attendance_date = CURDATE()
        ORDER BY ma.check_in_time DESC
    ");
    $attendanceStmt->execute(['ministry_id' => $ministryId]);
    $todayAttendance = $attendanceStmt->fetchAll();
    
    $step = 'take_attendance';
}

// Determine step based on GET parameters (only if not already set by POST)
if ($_SERVER['REQUEST_METHOD'] !== 'POST' && isset($_GET['type']) && in_array($_GET['type'], ['member', 'admin'])) {
    $userType = $_GET['type'];
    $step = 'enter_code';
}
?>
<!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, #9333EA 0%, #6366F1 50%, #3B82F6 100%); }
        .text-gradient { background: linear-gradient(135deg, #9333EA 0%, #3B82F6 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
        .btn-gradient { background: linear-gradient(135deg, #9333EA 0%, #6366F1 100%); }
        .btn-gradient-blue { background: linear-gradient(135deg, #3B82F6 0%, #1E40AF 100%); }
        .card-hover:hover { transform: translateY(-5px); box-shadow: 0 20px 40px rgba(0,0,0,0.15); }
        .card-hover { transition: all 0.3s ease; }
        @keyframes fadeInUp { from { opacity: 0; transform: translateY(30px); } to { opacity: 1; transform: translateY(0); } }
        .animate-fadeIn { animation: fadeInUp 0.5s ease-out; }
    </style>
</head>
<body class="bg-gray-50 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-12 h-12 rounded-xl flex items-center justify-center hero-gradient">
                        <i class="fas fa-church text-white text-xl"></i>
                    </div>
                    <div>
                        <h1 class="text-xl font-bold text-gradient"><?php echo htmlspecialchars($settings['site_title']); ?></h1>
                        <p class="text-xs text-gray-500">Ministry Attendance</p>
                    </div>
                </div>
                
                <nav class="flex items-center space-x-4">
                    <a href="index.php" class="text-gray-700 hover:text-purple-600 font-medium transition">
                        <i class="fas fa-home mr-1"></i><span class="hidden sm:inline">Home</span>
                    </a>
                    <a href="attendance.php" class="text-gray-700 hover:text-blue-600 font-medium transition">
                        <i class="fas fa-clipboard-check mr-1"></i><span class="hidden sm:inline">Program Attendance</span>
                    </a>
                </nav>
            </div>
        </div>
    </header>

    <!-- Hero Section -->
    <section class="hero-gradient text-white py-8">
        <div class="container mx-auto px-4 text-center">
            <i class="fas fa-users text-5xl mb-3 opacity-90"></i>
            <h1 class="text-3xl font-bold mb-2">Ministry Attendance</h1>
            <p class="text-white/90">Track attendance for your ministry or group meetings</p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-4 py-8">
        <div class="max-w-4xl mx-auto">
            
            <?php if ($error): ?>
            <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded mb-6 animate-fadeIn">
                <p class="font-bold"><i class="fas fa-exclamation-circle mr-2"></i>Error</p>
                <p><?php echo htmlspecialchars($error); ?></p>
            </div>
            <?php endif; ?>
            
            <?php if ($success): ?>
            <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 rounded mb-6 animate-fadeIn">
                <p class="font-bold"><i class="fas fa-check-circle mr-2"></i>Success</p>
                <p><?php echo htmlspecialchars($success); ?></p>
            </div>
            <?php endif; ?>
            
            <?php if ($step === 'select_type'): ?>
            <!-- Step 1: Select User Type -->
            <div class="bg-white rounded-xl shadow-lg p-8 animate-fadeIn">
                <h2 class="text-2xl font-bold text-gray-800 text-center mb-6">
                    <i class="fas fa-user-check mr-2 text-purple-500"></i>Welcome!
                </h2>
                <p class="text-gray-600 text-center mb-8">Select how you would like to access ministry attendance:</p>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <!-- Ministry Leader Option -->
                    <a href="?type=member" class="card-hover block bg-gradient-to-br from-purple-50 to-indigo-100 rounded-xl p-6 border-2 border-purple-200 hover:border-purple-400 text-center">
                        <div class="w-20 h-20 mx-auto mb-4 rounded-full bg-purple-500 flex items-center justify-center">
                            <i class="fas fa-user-tie text-3xl text-white"></i>
                        </div>
                        <h3 class="text-xl font-bold text-purple-800 mb-2">Ministry Leader</h3>
                        <p class="text-purple-600 text-sm">Take attendance for your assigned ministry</p>
                    </a>
                    
                    <!-- Admin Option -->
                    <a href="?type=admin" class="card-hover block bg-gradient-to-br from-blue-50 to-blue-100 rounded-xl p-6 border-2 border-blue-200 hover:border-blue-400 text-center">
                        <div class="w-20 h-20 mx-auto mb-4 rounded-full bg-blue-500 flex items-center justify-center">
                            <i class="fas fa-user-shield text-3xl text-white"></i>
                        </div>
                        <h3 class="text-xl font-bold text-blue-800 mb-2">Admin User</h3>
                        <p class="text-blue-600 text-sm">Manage attendance and generate reports</p>
                    </a>
                </div>
                
                <div class="mt-8 text-center">
                    <a href="index.php" class="text-gray-500 hover:text-gray-700">
                        <i class="fas fa-arrow-left mr-1"></i>Back to Home
                    </a>
                </div>
            </div>
            
            <?php elseif ($step === 'enter_code'): ?>
            <!-- Step 2: Enter Code -->
            <div class="bg-white rounded-xl shadow-lg p-8 animate-fadeIn">
                <div class="text-center mb-6">
                    <?php if ($userType === 'member'): ?>
                        <div class="w-16 h-16 mx-auto mb-4 rounded-full bg-purple-500 flex items-center justify-center">
                            <i class="fas fa-user-tie text-2xl text-white"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">Ministry Leader Access</h2>
                        <p class="text-gray-600">Enter your member code to access your ministries</p>
                    <?php else: ?>
                        <div class="w-16 h-16 mx-auto mb-4 rounded-full bg-blue-500 flex items-center justify-center">
                            <i class="fas fa-user-shield text-2xl text-white"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">Admin Access</h2>
                        <p class="text-gray-600">Enter your admin tracking code</p>
                    <?php endif; ?>
                </div>
                
                <form method="POST" class="space-y-6">
                    <input type="hidden" name="user_type" value="<?php echo htmlspecialchars($userType); ?>">
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">
                            <?php echo $userType === 'member' ? 'Member Code / Card ID' : 'Admin Code'; ?>
                        </label>
                        <input type="text" name="user_code" required autofocus
                               placeholder="<?php echo $userType === 'member' ? 'e.g., MEM2026123456' : 'e.g., USR2026123456'; ?>"
                               class="w-full px-4 py-3 text-lg border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent text-center uppercase"
                               style="letter-spacing: 2px;">
                    </div>
                    
                    <button type="submit" name="verify_code" 
                            class="w-full py-3 rounded-lg text-white font-semibold <?php echo $userType === 'member' ? 'btn-gradient' : 'btn-gradient-blue'; ?> transition">
                        <i class="fas fa-arrow-right mr-2"></i>Continue
                    </button>
                </form>
                
                <div class="mt-6 text-center">
                    <a href="ministry-attendance.php" class="text-gray-500 hover:text-gray-700">
                        <i class="fas fa-arrow-left mr-1"></i>Back
                    </a>
                </div>
            </div>
            
            <?php elseif ($step === 'select_ministry' && $memberData): ?>
            <!-- Step 3: Select Ministry -->
            <div class="bg-white rounded-xl shadow-lg overflow-hidden animate-fadeIn">
                <div class="bg-gradient-to-r from-purple-500 to-indigo-600 text-white p-6">
                    <div class="flex items-center space-x-4">
                        <div class="w-16 h-16 rounded-full bg-white/20 flex items-center justify-center">
                            <i class="fas fa-user text-2xl"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-bold"><?php echo htmlspecialchars($memberData['first_name'] . ' ' . $memberData['last_name']); ?></h3>
                            <p class="text-white/80 text-sm"><?php echo htmlspecialchars($memberData['assembly_name'] ?? 'N/A'); ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="p-6">
                    <h4 class="text-lg font-bold text-gray-800 mb-4">
                        <i class="fas fa-hand-point-right mr-2 text-purple-500"></i>Select a Ministry
                    </h4>
                    
                    <?php if (count($userMinistries) > 0): ?>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <?php foreach ($userMinistries as $ministry): ?>
                        <form method="POST" class="block">
                            <input type="hidden" name="ministry_id" value="<?php echo $ministry['id']; ?>">
                            <input type="hidden" name="member_id" value="<?php echo $memberData['id']; ?>">
                            <button type="submit" name="select_ministry" 
                                    class="w-full text-left card-hover bg-gray-50 rounded-lg p-4 border-2 border-gray-200 hover:border-purple-400 transition">
                                <div class="flex items-center space-x-3">
                                    <div class="w-12 h-12 rounded-lg flex items-center justify-center" 
                                         style="background-color: <?php echo $ministry['color'] ?? '#9333EA'; ?>20;">
                                        <i class="fas fa-<?php echo $ministry['icon'] ?? 'users'; ?> text-xl" 
                                           style="color: <?php echo $ministry['color'] ?? '#9333EA'; ?>;"></i>
                                    </div>
                                    <div>
                                        <h5 class="font-bold text-gray-800"><?php echo htmlspecialchars($ministry['ministry_name']); ?></h5>
                                        <p class="text-sm text-gray-500"><?php echo htmlspecialchars($ministry['category_name'] ?? ucfirst($ministry['ministry_type'])); ?></p>
                                    </div>
                                </div>
                            </button>
                        </form>
                        <?php endforeach; ?>
                    </div>
                    <?php else: ?>
                    <div class="text-center py-8 text-gray-500">
                        <i class="fas fa-folder-open text-4xl mb-3"></i>
                        <p>No ministries assigned to you.</p>
                    </div>
                    <?php endif; ?>
                    
                    <div class="mt-6 text-center">
                        <a href="ministry-attendance.php" class="text-gray-500 hover:text-gray-700">
                            <i class="fas fa-arrow-left mr-1"></i>Back
                        </a>
                    </div>
                </div>
            </div>
            
            <?php elseif ($step === 'take_attendance' && $selectedMinistry): ?>
            <!-- Step 4: Take Attendance -->
            <div class="space-y-6 animate-fadeIn">
                <!-- Ministry Header -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                    <div class="p-4" style="background-color: <?php echo $selectedMinistry['color'] ?? '#9333EA'; ?>;">
                        <div class="flex items-center justify-between text-white">
                            <div class="flex items-center space-x-3">
                                <div class="w-12 h-12 rounded-lg bg-white/20 flex items-center justify-center">
                                    <i class="fas fa-<?php echo $selectedMinistry['icon'] ?? 'users'; ?> text-xl"></i>
                                </div>
                                <div>
                                    <h3 class="text-xl font-bold"><?php echo htmlspecialchars($selectedMinistry['ministry_name']); ?></h3>
                                    <p class="text-white/80 text-sm"><?php echo date('l, F j, Y'); ?></p>
                                </div>
                            </div>
                            <div class="text-right">
                                <p class="text-2xl font-bold"><?php echo count($todayAttendance); ?></p>
                                <p class="text-white/80 text-sm">Checked In</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Quick Stats -->
                <div class="grid grid-cols-3 gap-4">
                    <div class="bg-white rounded-lg shadow p-4 text-center">
                        <p class="text-2xl font-bold text-purple-600"><?php echo count($ministryMembers); ?></p>
                        <p class="text-sm text-gray-500">Total Members</p>
                    </div>
                    <div class="bg-white rounded-lg shadow p-4 text-center">
                        <p class="text-2xl font-bold text-green-600"><?php echo count(array_filter($todayAttendance, fn($a) => $a['attendance_type'] === 'member')); ?></p>
                        <p class="text-sm text-gray-500">Members Present</p>
                    </div>
                    <div class="bg-white rounded-lg shadow p-4 text-center">
                        <p class="text-2xl font-bold text-orange-600"><?php echo count(array_filter($todayAttendance, fn($a) => $a['attendance_type'] !== 'member')); ?></p>
                        <p class="text-sm text-gray-500">Guests/Visitors</p>
                    </div>
                </div>
                
                <!-- Member Check-in -->
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h4 class="text-lg font-bold text-gray-800 mb-4">
                        <i class="fas fa-user-check mr-2 text-green-500"></i>Check In Member
                    </h4>
                    
                    <form method="POST" class="flex flex-col sm:flex-row gap-3">
                        <input type="hidden" name="ministry_id" value="<?php echo $selectedMinistry['id']; ?>">
                        <input type="hidden" name="current_member_id" value="<?php echo $memberData['id']; ?>">
                        <input type="hidden" name="member_id" value="<?php echo $memberData['id']; ?>">
                        <input type="hidden" name="marked_by" value="<?php echo $memberData['id']; ?>">
                        
                        <select name="checkin_member_id" required class="flex-1 px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500">
                            <option value="">Select Member</option>
                            <?php foreach ($ministryMembers as $member): 
                                $alreadyCheckedIn = in_array($member['member_id'], array_column($todayAttendance, 'member_id'));
                            ?>
                            <option value="<?php echo $member['member_id']; ?>" <?php echo $alreadyCheckedIn ? 'disabled' : ''; ?>>
                                <?php echo htmlspecialchars($member['first_name'] . ' ' . $member['last_name']); ?>
                                <?php echo $alreadyCheckedIn ? ' (Already checked in)' : ''; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                        
                        <button type="submit" name="checkin_member" class="px-6 py-3 btn-gradient text-white rounded-lg font-semibold">
                            <i class="fas fa-check mr-2"></i>Check In
                        </button>
                    </form>
                </div>
                
                <!-- Guest Check-in -->
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h4 class="text-lg font-bold text-gray-800 mb-4">
                        <i class="fas fa-user-plus mr-2 text-orange-500"></i>Check In Guest/Visitor
                    </h4>
                    
                    <form method="POST" class="space-y-4">
                        <input type="hidden" name="ministry_id" value="<?php echo $selectedMinistry['id']; ?>">
                        <input type="hidden" name="current_member_id" value="<?php echo $memberData['id']; ?>">
                        <input type="hidden" name="member_id" value="<?php echo $memberData['id']; ?>">
                        <input type="hidden" name="marked_by" value="<?php echo $memberData['id']; ?>">
                        
                        <div class="grid grid-cols-1 sm:grid-cols-3 gap-4">
                            <input type="text" name="guest_name" required placeholder="Guest Name"
                                   class="px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-orange-500">
                            <input type="tel" name="guest_phone" placeholder="Phone (Optional)"
                                   class="px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-orange-500">
                            <select name="guest_type" class="px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-orange-500">
                                <option value="guest">Guest</option>
                                <option value="visitor">Visitor</option>
                            </select>
                        </div>
                        
                        <button type="submit" name="checkin_guest" class="w-full sm:w-auto px-6 py-3 bg-orange-500 hover:bg-orange-600 text-white rounded-lg font-semibold">
                            <i class="fas fa-plus mr-2"></i>Add Guest
                        </button>
                    </form>
                </div>
                
                <!-- Today's Attendance List -->
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h4 class="text-lg font-bold text-gray-800 mb-4">
                        <i class="fas fa-list mr-2 text-blue-500"></i>Today's Attendance (<?php echo count($todayAttendance); ?>)
                    </h4>
                    
                    <?php if (count($todayAttendance) > 0): ?>
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">#</th>
                                    <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Name</th>
                                    <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Type</th>
                                    <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Time</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-100">
                                <?php foreach ($todayAttendance as $i => $att): ?>
                                <tr class="hover:bg-gray-50">
                                    <td class="px-4 py-3 text-sm"><?php echo $i + 1; ?></td>
                                    <td class="px-4 py-3">
                                        <span class="font-medium">
                                            <?php echo $att['member_id'] ? htmlspecialchars($att['first_name'] . ' ' . $att['last_name']) : htmlspecialchars($att['guest_name']); ?>
                                        </span>
                                    </td>
                                    <td class="px-4 py-3">
                                        <span class="px-2 py-1 rounded-full text-xs font-medium 
                                            <?php echo $att['attendance_type'] === 'member' ? 'bg-purple-100 text-purple-800' : 'bg-orange-100 text-orange-800'; ?>">
                                            <?php echo ucfirst($att['attendance_type']); ?>
                                        </span>
                                    </td>
                                    <td class="px-4 py-3 text-sm text-gray-500">
                                        <?php echo date('g:i A', strtotime($att['check_in_time'])); ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php else: ?>
                    <div class="text-center py-8 text-gray-500">
                        <i class="fas fa-clipboard-list text-4xl mb-3"></i>
                        <p>No attendance recorded yet today.</p>
                    </div>
                    <?php endif; ?>
                </div>
                
                <!-- Back Button -->
                <div class="text-center">
                    <a href="ministry-attendance.php?type=member" class="text-gray-500 hover:text-gray-700">
                        <i class="fas fa-arrow-left mr-1"></i>Back to Ministry Selection
                    </a>
                </div>
            </div>
            
            <?php elseif ($step === 'admin_dashboard' && $userData): ?>
            <!-- Admin Dashboard -->
            <div class="space-y-6 animate-fadeIn">
                <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                    <div class="bg-gradient-to-r from-blue-500 to-blue-600 text-white p-6">
                        <div class="flex items-center space-x-4">
                            <div class="w-16 h-16 rounded-full bg-white/20 flex items-center justify-center">
                                <i class="fas fa-user-shield text-2xl"></i>
                            </div>
                            <div>
                                <h3 class="text-xl font-bold"><?php echo htmlspecialchars($userData['full_name'] ?? $userData['username']); ?></h3>
                                <p class="text-white/80 text-sm">Admin Dashboard</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="p-6">
                        <h4 class="text-lg font-bold text-gray-800 mb-4">
                            <i class="fas fa-chart-bar mr-2 text-blue-500"></i>Ministry Attendance Reports
                        </h4>
                        
                        <p class="text-gray-600 mb-4">Select a ministry to view attendance reports and export data.</p>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
                            <?php foreach ($userMinistries as $ministry): ?>
                            <a href="ministry-attendance-admin.php?ministry_id=<?php echo $ministry['id']; ?>" 
                               class="card-hover bg-gray-50 rounded-lg p-4 border-2 border-gray-200 hover:border-blue-400 transition">
                                <div class="flex items-center space-x-3">
                                    <div class="w-12 h-12 rounded-lg flex items-center justify-center" 
                                         style="background-color: <?php echo $ministry['color'] ?? '#3B82F6'; ?>20;">
                                        <i class="fas fa-<?php echo $ministry['icon'] ?? 'users'; ?> text-xl" 
                                           style="color: <?php echo $ministry['color'] ?? '#3B82F6'; ?>;"></i>
                                    </div>
                                    <div>
                                        <h5 class="font-bold text-gray-800"><?php echo htmlspecialchars($ministry['ministry_name']); ?></h5>
                                        <p class="text-sm text-gray-500"><?php echo htmlspecialchars($ministry['category_name'] ?? ucfirst($ministry['ministry_type'])); ?></p>
                                    </div>
                                </div>
                            </a>
                            <?php endforeach; ?>
                        </div>
                        
                        <div class="border-t pt-4">
                            <a href="program-activity-report.php" class="inline-flex items-center px-6 py-3 btn-gradient-blue text-white rounded-lg font-semibold">
                                <i class="fas fa-file-alt mr-2"></i>Create Program Activity Report
                            </a>
                        </div>
                    </div>
                </div>
                
                <div class="text-center">
                    <a href="ministry-attendance.php" class="text-gray-500 hover:text-gray-700">
                        <i class="fas fa-arrow-left mr-1"></i>Back
                    </a>
                </div>
            </div>
            <?php endif; ?>
            
        </div>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-800 text-white py-6 mt-auto">
        <div class="container mx-auto px-4 text-center">
            <p class="text-gray-400 text-sm">
                © <?php echo date('Y'); ?> <?php echo htmlspecialchars($settings['site_title']); ?>
            </p>
        </div>
    </footer>
</body>
</html>

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