<?php
// Enable error reporting for debugging
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', '../logs/php_errors.log');

require_once '../includes/db_connect.php';

// Check session
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    error_log("Unauthorized access attempt to admin_dashboard.php. Session: " . print_r($_SESSION, true));
    header("Location: ../login.php");
    exit;
}

// Fetch admin username
$admin_name = 'Admin';
try {
    $stmt = $pdo->prepare("SELECT username FROM admins WHERE id = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $admin = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($admin) {
        $admin_name = $admin['username'];
    }
} catch (PDOException $e) {
    error_log("Error fetching admin username: " . $e->getMessage());
}

// Initialize variables with fallback values
$stats = [
    'total_applicants' => 'N/A',
    'active_cbts' => 'N/A',
    'pending_applications' => 'N/A',
    'total_classes' => 'N/A',
    'total_questions' => 'N/A',
    'total_subjects' => 'N/A'
];

try {
    $stats['total_applicants'] = $pdo->query("SELECT COUNT(*) FROM applicants")->fetchColumn();
    $stats['active_cbts'] = $pdo->query("SELECT COUNT(*) FROM subjects WHERE status = 'active'")->fetchColumn();
    $stats['pending_applications'] = $pdo->query("SELECT COUNT(*) FROM applicants WHERE status = 'pending'")->fetchColumn();
    $stats['total_classes'] = $pdo->query("SELECT COUNT(*) FROM classes")->fetchColumn();
    $stats['total_questions'] = $pdo->query("SELECT COUNT(*) FROM questions")->fetchColumn();
    $stats['total_subjects'] = $pdo->query("SELECT COUNT(*) FROM subjects")->fetchColumn();
} catch (PDOException $e) {
    error_log("Database error in admin_dashboard.php: " . $e->getMessage());
    $error_message = "Database error. Check logs for details.";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - Verbum Dei Academy & Verbum Dei Int'l College</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">
    <style>
        :root {
            --primary: #4e73df;
            --secondary: #858796;
            --success: #1cc88a;
            --info: #36b9cc;
            --warning: #f6c23e;
            --danger: #e74a3b;
            --light: #f8f9fc;
            --dark: #5a5c69;
        }
        
        body {
            background-color: #f8f9fc;
            font-family: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        .dashboard-header {
            background: white;
            border-radius: 15px;
            padding: 1.5rem;
            margin-bottom: 2rem;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
        }
        
        .stat-card {
            border-radius: 15px;
            border: none;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.1);
            transition: all 0.3s;
            overflow: hidden;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 0.5rem 1.5rem 0 rgba(58, 59, 69, 0.2);
        }
        
        .stat-card .card-icon {
            font-size: 2rem;
            opacity: 0.3;
            position: absolute;
            right: 20px;
            top: 20px;
        }
        
        .stat-card.bg-success {
            background: linear-gradient(135deg, var(--success) 0%, #224abe 100%) !important;
            color: white;
        }
        
        .stat-card.bg-success {
            background: linear-gradient(135deg, var(--success) 0%, #13855c 100%) !important;
            color: white;
        }
        
        .stat-card.bg-warning {
            background: linear-gradient(135deg, var(--warning) 0%, #dda20a 100%) !important;
            color: white;
        }
        
        .stat-card.bg-danger {
            background: linear-gradient(135deg, var(--danger) 0%, #be2617 100%) !important;
            color: white;
        }
        
        .stat-card.bg-info {
            background: linear-gradient(135deg, var(--info) 0%, #258391 100%) !important;
            color: white;
        }
        
        .stat-card.bg-secondary {
            background: linear-gradient(135deg, var(--secondary) 0%, #60616f 100%) !important;
            color: white;
        }
        
        .action-card {
            border-radius: 15px;
            border: none;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.1);
            transition: all 0.3s;
            height: 100%;
        }
        
        .action-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 0.5rem 1.5rem 0 rgba(58, 59, 69, 0.2);
        }
        
        .action-card .card-icon {
            font-size: 2.5rem;
            margin-bottom: 1rem;
            color: var(--success);
        }
        
        .action-card .btn {
            border-radius: 50px;
            padding: 0.5rem 1.5rem;
        }
        
        .section-title {
            position: relative;
            padding-bottom: 10px;
            margin-bottom: 2rem;
        }
        
        .section-title:after {
            content: "";
            position: absolute;
            left: 0;
            bottom: 0;
            width: 50px;
            height: 3px;
            background: var(--success);
        }
        
        .navbar {
            background: white;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
            border-radius: 15px;
            padding: 1rem;
        }
        
        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: var(--success);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <!-- Top Navigation Bar -->
    <nav class="navbar navbar-expand-lg mb-4">
        <div class="container-fluid">
            <a class="navbar-brand fw-bold text-success" href="#">
                <i class="bi bi-mortarboard me-2"></i>Verbum Dei Academy & Verbum Dei Int'l College
            </a>
            <div class="d-flex align-items-center">
                <div class="me-3">
                    <span class="text-muted">Welcome,</span>
                    <span class="fw-bold ms-1"><?php echo htmlspecialchars($admin_name); ?></span>
                </div>
                <div class="user-avatar me-2">
                    <?php echo strtoupper(substr($admin_name, 0, 1)); ?>
                </div>
                <a href="../logout.php" class="btn btn-sm btn-outline-danger">
                    <i class="bi bi-box-arrow-right"></i>
                </a>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <!-- Dashboard Header -->
        <div class="dashboard-header animate__animated animate__fadeIn">
            <div class="row align-items-center">
                <div class="col-md-6">
                    <h1 class="h3 fw-bold text-success mb-0">Admin Dashboard</h1>
                    <p class="mb-0 text-muted">Overview of Verbum Dei Academy & Verbum Dei Int'l College</p>
                </div>
                <div class="col-md-6 text-md-end">
                    <span class="badge bg-success">
                        <i class="bi bi-calendar me-1"></i> <?php echo date('l, F j, Y'); ?>
                    </span>
                </div>
            </div>
        </div>
        
        <?php if (isset($error_message)): ?>
            <div class="alert alert-danger animate__animated animate__shakeX mb-4"><?php echo htmlspecialchars($error_message); ?></div>
        <?php endif; ?>
        
        <!-- Stats Cards -->
        <div class="row g-4 mb-4">
            <div class="col-xl-3 col-md-6">
                <div class="stat-card card bg-primary text-white animate__animated animate__fadeInUp">
                    <div class="card-body">
                        <i class="bi bi-people card-icon"></i>
                        <h5 class="card-title text-uppercase mb-2">Total Applicants</h5>
                        <h2 class="mb-0 fw-bold"><?php echo htmlspecialchars($stats['total_applicants']); ?></h2>
                        <a href="manage_applicants.php" class="text-white small mt-2 d-block">View all applicants <i class="bi bi-arrow-right"></i></a>
                    </div>
                </div>
            </div>
            
            <div class="col-xl-3 col-md-6">
                <div class="stat-card card bg-success text-white animate__animated animate__fadeInUp animate__delay-1s">
                    <div class="card-body">
                        <i class="bi bi-laptop card-icon"></i>
                        <h5 class="card-title text-uppercase mb-2">Active CBT SUBJECTS</h5>
                        <h2 class="mb-0 fw-bold"><?php echo htmlspecialchars($stats['active_cbts']); ?></h2>
                        <a href="subjects.php" class="text-white small mt-2 d-block">Manage CBTs <i class="bi bi-arrow-right"></i></a>
                    </div>
                </div>
            </div>
            
            <div class="col-xl-3 col-md-6">
                <div class="stat-card card bg-warning text-white animate__animated animate__fadeInUp animate__delay-2s">
                    <div class="card-body">
                        <i class="bi bi-clock-history card-icon"></i>
                        <h5 class="card-title text-uppercase mb-2">Pending Applications</h5>
                        <h2 class="mb-0 fw-bold"><?php echo htmlspecialchars($stats['pending_applications']); ?></h2>
                        <a href="applications.php?status=pending" class="text-white small mt-2 d-block">Review applications <i class="bi bi-arrow-right"></i></a>
                    </div>
                </div>
            </div>
            
            <div class="col-xl-3 col-md-6">
                <div class="stat-card card bg-info text-white animate__animated animate__fadeInUp animate__delay-3s">
                    <div class="card-body">
                        <i class="bi bi-book card-icon"></i>
                        <h5 class="card-title text-uppercase mb-2">Total Classes</h5>
                        <h2 class="mb-0 fw-bold"><?php echo htmlspecialchars($stats['total_classes']); ?></h2>
                        <a href="manage_classes.php" class="text-white small mt-2 d-block">Manage classes <i class="bi bi-arrow-right"></i></a>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Quick Actions Section -->
        <div class="row mb-4">
            <div class="col-12">
                <h3 class="section-title">Quick Actions</h3>
            </div>
            
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="action-card card h-100 animate__animated animate__fadeIn">
                    <div class="card-body text-center">
                        <i class="bi bi-person-plus card-icon"></i>
                        <h5 class="card-title">Register Applicant</h5>
                        <p class="card-text text-muted">Add new applicant to the system</p>
                        <a href="admin_applicant_register.php" class="btn btn-success">
                            <i class="bi bi-plus-circle me-1"></i> Register
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="action-card card h-100 animate__animated animate__fadeIn animate__delay-1s">
                    <div class="card-body text-center">
                        <i class="bi bi-book card-icon"></i>
                        <h5 class="card-title">Manage Subjects</h5>
                        <p class="card-text text-muted">Add, edit or remove subjects</p>
                        <a href="subjects.php" class="btn btn-success">
                            <i class="bi bi-gear me-1"></i> Manage
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="action-card card h-100 animate__animated animate__fadeIn animate__delay-2s">
                    <div class="card-body text-center">
                        <i class="bi bi-question-circle card-icon"></i>
                        <h5 class="card-title">Manage Questions</h5>
                        <p class="card-text text-muted">Add or edit exam questions</p>
                        <a href="questions.php" class="btn btn-success">
                            <i class="bi bi-gear me-1"></i> Manage
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="action-card card h-100 animate__animated animate__fadeIn animate__delay-3s">
                    <div class="card-body text-center">
                        <i class="bi bi-clipboard-data card-icon"></i>
                        <h5 class="card-title">View Results</h5>
                        <p class="card-text text-muted">Check CBT exam results</p>
                        <a href="results.php" class="btn btn-success">
                            <i class="bi bi-eye me-1"></i> View
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- System Management Section -->
        <div class="row">
            <div class="col-lg-6 mb-4">
                <div class="card h-100 animate__animated animate__fadeIn">
                    <div class="card-body">
                        <h3 class="section-title">System Management</h3>
                        <div class="list-group">
                            <a href="manage_classes.php" class="list-group-item list-group-item-action d-flex align-items-center">
                                <i class="bi bi-journal-bookmark fs-4 me-3 text-success"></i>
                                <div>
                                    <h6 class="mb-1">Manage Classes</h6>
                                    <p class="mb-1 small text-muted">Create and manage classes</p>
                                </div>
                                <i class="bi bi-chevron-right ms-auto"></i>
                            </a>
                            <a href="settings.php" class="list-group-item list-group-item-action d-flex align-items-center">
                                <i class="bi bi-gear fs-4 me-3 text-success"></i>
                                <div>
                                    <h6 class="mb-1">System Settings</h6>
                                    <p class="mb-1 small text-muted">Configure system preferences</p>
                                </div>
                                <i class="bi bi-chevron-right ms-auto"></i>
                            </a>
                            <a href="reports.php" class="list-group-item list-group-item-action d-flex align-items-center">
                                <i class="bi bi-graph-up fs-4 me-3 text-success"></i>
                                <div>
                                    <h6 class="mb-1">Reports & Analytics</h6>
                                    <p class="mb-1 small text-muted">View system reports</p>
                                </div>
                                <i class="bi bi-chevron-right ms-auto"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-6 mb-4">
                <div class="card h-100 animate__animated animate__fadeIn animate__delay-1s">
                    <div class="card-body">
                        <h3 class="section-title">Recent Activity</h3>
                        <div class="d-flex align-items-center mb-3">
                            <div class="bg-light p-2 rounded me-3">
                                <i class="bi bi-person-plus text-success"></i>
                            </div>
                            <div>
                                <h6 class="mb-0">New Applicant Registered</h6>
                                <small class="text-muted">2 minutes ago</small>
                            </div>
                        </div>
                        <div class="d-flex align-items-center mb-3">
                            <div class="bg-light p-2 rounded me-3">
                                <i class="bi bi-book text-success"></i>
                            </div>
                            <div>
                                <h6 class="mb-0">New Subject Added</h6>
                                <small class="text-muted">15 minutes ago</small>
                            </div>
                        </div>
                        <div class="d-flex align-items-center">
                            <div class="bg-light p-2 rounded me-3">
                                <i class="bi bi-question-circle text-warning"></i>
                            </div>
                            <div>
                                <h6 class="mb-0">10 New Questions Added</h6>
                                <small class="text-muted">1 hour ago</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Add animation to cards on scroll
            const observer = new IntersectionObserver((entries) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        entry.target.classList.add('animate__fadeIn');
                    }
                });
            }, {threshold: 0.1});
            
            document.querySelectorAll('.card').forEach(card => {
                observer.observe(card);
            });
        });
    </script>
</body>
</html>