<?php
// admin/dashboard.php
require_once '../config/config.php';
require_login();

// Get statistics
$total_exams = $pdo->query("SELECT COUNT(*) FROM exams")->fetchColumn();
$total_students = $pdo->query("SELECT COUNT(*) FROM students")->fetchColumn();
$total_questions = $pdo->query("SELECT COUNT(*) FROM questions")->fetchColumn();
$completed_exams = $pdo->query("SELECT COUNT(*) FROM student_exams WHERE status = 'completed'")->fetchColumn();

// Get recent activity - Fixed query without completed_at
$recent_students = $pdo->query("SELECT * FROM students ORDER BY id DESC LIMIT 5")->fetchAll();
$recent_completions = $pdo->query("
    SELECT se.*, s.full_name, e.title as exam_title 
    FROM student_exams se 
    JOIN students s ON se.student_id = s.id 
    JOIN exams e ON se.exam_id = e.id 
    WHERE se.status = 'completed' 
    ORDER BY se.id DESC 
    LIMIT 5
")->fetchAll();

$flash = get_flash();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - Edoxi Training Center</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="bg-gray-50">
    <!-- Navbar -->
    <?php include 'includes/nav.php'; ?>
    
    <!-- Main Content -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <!-- Flash Message -->
        <?php if ($flash): ?>
            <div class="mb-6 bg-<?php echo $flash['type'] === 'success' ? 'green' : 'red'; ?>-50 border-l-4 border-<?php echo $flash['type'] === 'success' ? 'green' : 'red'; ?>-500 p-4 rounded-lg shadow-md">
                <div class="flex items-center">
                    <i class="fas fa-<?php echo $flash['type'] === 'success' ? 'check-circle' : 'exclamation-circle'; ?> text-<?php echo $flash['type'] === 'success' ? 'green' : 'red'; ?>-600 mr-3 text-xl"></i>
                    <p class="text-<?php echo $flash['type'] === 'success' ? 'green' : 'red'; ?>-800 font-medium">
                        <?php echo escape($flash['message']); ?>
                    </p>
                </div>
            </div>
        <?php endif; ?>
        
        <!-- Welcome Header -->
        <div class="mb-8">
            <div class="flex items-center justify-between">
                <div>
                    <h1 class="text-3xl font-bold text-gray-800 mb-2">
                        Welcome back, <?php echo escape($_SESSION['admin_name'] ?? 'Admin'); ?>!
                    </h1>
                    <p class="text-gray-600">Here's what's happening with your assessments today.</p>
                </div>
                <div class="hidden md:block">
                    <img src="https://www.edoxi.com/assets/images/edoxi-logo.svg" 
                         alt="Edoxi Training Center" 
                         class="h-16">
                </div>
            </div>
        </div>
        
        <!-- Statistics Cards -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <!-- Total Assessments -->
            <div class="bg-white rounded-xl shadow-lg overflow-hidden transform hover:scale-105 transition duration-200">
                <div class="bg-gradient-to-br from-blue-500 to-blue-600 p-6">
                    <div class="flex justify-between items-start">
                        <div class="text-white">
                            <p class="text-blue-100 text-sm font-semibold uppercase tracking-wide">Total Assessments</p>
                            <h3 class="text-4xl font-bold mt-2"><?php echo $total_exams; ?></h3>
                        </div>
                        <div class="bg-white bg-opacity-20 rounded-lg p-3">
                            <i class="fas fa-clipboard-list text-white text-3xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white px-6 py-4">
                    <a href="<?php echo base_url('admin/exams.php'); ?>" 
                       class="text-blue-600 hover:text-blue-800 text-sm font-semibold flex items-center justify-between group">
                        <span>Manage Assessments</span>
                        <i class="fas fa-arrow-right group-hover:translate-x-1 transition"></i>
                    </a>
                </div>
            </div>
            
            <!-- Total Participants -->
            <div class="bg-white rounded-xl shadow-lg overflow-hidden transform hover:scale-105 transition duration-200">
                <div class="bg-gradient-to-br from-green-500 to-green-600 p-6">
                    <div class="flex justify-between items-start">
                        <div class="text-white">
                            <p class="text-green-100 text-sm font-semibold uppercase tracking-wide">Total Participants</p>
                            <h3 class="text-4xl font-bold mt-2"><?php echo $total_students; ?></h3>
                        </div>
                        <div class="bg-white bg-opacity-20 rounded-lg p-3">
                            <i class="fas fa-user-graduate text-white text-3xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white px-6 py-4">
                    <a href="<?php echo base_url('admin/students.php'); ?>" 
                       class="text-green-600 hover:text-green-800 text-sm font-semibold flex items-center justify-between group">
                        <span>View Participants</span>
                        <i class="fas fa-arrow-right group-hover:translate-x-1 transition"></i>
                    </a>
                </div>
            </div>
            
            <!-- Question Bank -->
            <div class="bg-white rounded-xl shadow-lg overflow-hidden transform hover:scale-105 transition duration-200">
                <div class="bg-gradient-to-br from-purple-500 to-purple-600 p-6">
                    <div class="flex justify-between items-start">
                        <div class="text-white">
                            <p class="text-purple-100 text-sm font-semibold uppercase tracking-wide">Question Bank</p>
                            <h3 class="text-4xl font-bold mt-2"><?php echo $total_questions; ?></h3>
                        </div>
                        <div class="bg-white bg-opacity-20 rounded-lg p-3">
                            <i class="fas fa-question-circle text-white text-3xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white px-6 py-4">
                    <a href="<?php echo base_url('admin/questions.php'); ?>" 
                       class="text-purple-600 hover:text-purple-800 text-sm font-semibold flex items-center justify-between group">
                        <span>Manage Questions</span>
                        <i class="fas fa-arrow-right group-hover:translate-x-1 transition"></i>
                    </a>
                </div>
            </div>
            
            <!-- Completed Assessments -->
            <div class="bg-white rounded-xl shadow-lg overflow-hidden transform hover:scale-105 transition duration-200">
                <div class="bg-gradient-to-br from-orange-500 to-orange-600 p-6">
                    <div class="flex justify-between items-start">
                        <div class="text-white">
                            <p class="text-orange-100 text-sm font-semibold uppercase tracking-wide">Completed</p>
                            <h3 class="text-4xl font-bold mt-2"><?php echo $completed_exams; ?></h3>
                        </div>
                        <div class="bg-white bg-opacity-20 rounded-lg p-3">
                            <i class="fas fa-check-double text-white text-3xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white px-6 py-4">
                    <a href="<?php echo base_url('admin/results.php'); ?>" 
                       class="text-orange-600 hover:text-orange-800 text-sm font-semibold flex items-center justify-between group">
                        <span>View Results</span>
                        <i class="fas fa-arrow-right group-hover:translate-x-1 transition"></i>
                    </a>
                </div>
            </div>
        </div>
        
        <!-- Quick Actions -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <div class="flex items-center mb-6">
                <div class="bg-yellow-100 rounded-lg p-2 mr-3">
                    <i class="fas fa-bolt text-yellow-600 text-xl"></i>
                </div>
                <h2 class="text-xl font-bold text-gray-800">Quick Actions</h2>
            </div>
            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
                <a href="<?php echo base_url('admin/exams.php?action=create'); ?>" 
                   class="group border-2 border-blue-200 hover:border-blue-500 hover:bg-blue-50 rounded-xl p-6 text-center transition duration-200">
                    <div class="bg-blue-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3 group-hover:scale-110 transition">
                        <i class="fas fa-plus-circle text-blue-600 text-2xl"></i>
                    </div>
                    <p class="font-semibold text-gray-800 group-hover:text-blue-600">Create Assessment</p>
                    <p class="text-xs text-gray-500 mt-1">Set up new assessment</p>
                </a>
                
                <a href="<?php echo base_url('admin/questions.php?action=create'); ?>" 
                   class="group border-2 border-green-200 hover:border-green-500 hover:bg-green-50 rounded-xl p-6 text-center transition duration-200">
                    <div class="bg-green-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3 group-hover:scale-110 transition">
                        <i class="fas fa-edit text-green-600 text-2xl"></i>
                    </div>
                    <p class="font-semibold text-gray-800 group-hover:text-green-600">Add Question</p>
                    <p class="text-xs text-gray-500 mt-1">Create new question</p>
                </a>
                
                <a href="<?php echo base_url('admin/questions.php'); ?>" 
                   class="group border-2 border-purple-200 hover:border-purple-500 hover:bg-purple-50 rounded-xl p-6 text-center transition duration-200">
                    <div class="bg-purple-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3 group-hover:scale-110 transition">
                        <i class="fas fa-database text-purple-600 text-2xl"></i>
                    </div>
                    <p class="font-semibold text-gray-800 group-hover:text-purple-600">Question Bank</p>
                    <p class="text-xs text-gray-500 mt-1">Browse all questions</p>
                </a>
                
                <a href="<?php echo base_url('admin/results.php'); ?>" 
                   class="group border-2 border-orange-200 hover:border-orange-500 hover:bg-orange-50 rounded-xl p-6 text-center transition duration-200">
                    <div class="bg-orange-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3 group-hover:scale-110 transition">
                        <i class="fas fa-chart-line text-orange-600 text-2xl"></i>
                    </div>
                    <p class="font-semibold text-gray-800 group-hover:text-orange-600">View Reports</p>
                    <p class="text-xs text-gray-500 mt-1">Assessment analytics</p>
                </a>
            </div>
        </div>
        
        <!-- Recent Activity Section -->
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
            <!-- Recent Registrations -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <div class="flex items-center justify-between mb-4">
                    <h3 class="text-lg font-bold text-gray-800 flex items-center">
                        <i class="fas fa-user-plus text-blue-600 mr-2"></i>
                        Recent Registrations
                    </h3>
                    <a href="<?php echo base_url('admin/students.php'); ?>" class="text-blue-600 hover:text-blue-800 text-sm font-semibold">
                        View All →
                    </a>
                </div>
                <div class="space-y-3">
                    <?php if (empty($recent_students)): ?>
                        <p class="text-gray-500 text-center py-4">No recent registrations</p>
                    <?php else: ?>
                        <?php foreach ($recent_students as $student): ?>
                            <div class="flex items-center p-3 bg-gray-50 rounded-lg hover:bg-gray-100 transition">
                                <div class="bg-blue-100 rounded-full w-10 h-10 flex items-center justify-center mr-3">
                                    <i class="fas fa-user text-blue-600"></i>
                                </div>
                                <div class="flex-1">
                                    <p class="font-semibold text-gray-800"><?php echo escape($student->full_name); ?></p>
                                    <p class="text-xs text-gray-500"><?php echo escape($student->company); ?></p>
                                </div>
                                <span class="text-xs text-gray-400">
                                    <?php echo isset($student->created_at) ? date('d M', strtotime($student->created_at)) : 'Recent'; ?>
                                </span>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- Recent Completions -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <div class="flex items-center justify-between mb-4">
                    <h3 class="text-lg font-bold text-gray-800 flex items-center">
                        <i class="fas fa-trophy text-green-600 mr-2"></i>
                        Recent Completions
                    </h3>
                    <a href="<?php echo base_url('admin/results.php'); ?>" class="text-green-600 hover:text-green-800 text-sm font-semibold">
                        View All →
                    </a>
                </div>
                <div class="space-y-3">
                    <?php if (empty($recent_completions)): ?>
                        <p class="text-gray-500 text-center py-4">No recent completions</p>
                    <?php else: ?>
                        <?php foreach ($recent_completions as $completion): ?>
                            <div class="flex items-center p-3 bg-gray-50 rounded-lg hover:bg-gray-100 transition">
                                <div class="bg-<?php echo $completion->result === 'pass' ? 'green' : 'red'; ?>-100 rounded-full w-10 h-10 flex items-center justify-center mr-3">
                                    <i class="fas fa-<?php echo $completion->result === 'pass' ? 'check' : 'times'; ?> text-<?php echo $completion->result === 'pass' ? 'green' : 'red'; ?>-600"></i>
                                </div>
                                <div class="flex-1">
                                    <p class="font-semibold text-gray-800"><?php echo escape($completion->full_name); ?></p>
                                    <p class="text-xs text-gray-500"><?php echo escape($completion->exam_title); ?></p>
                                </div>
                                <div class="text-right">
                                    <span class="text-sm font-bold text-<?php echo $completion->result === 'pass' ? 'green' : 'red'; ?>-600">
                                        <?php echo $completion->percentage; ?>%
                                    </span>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- System Status Banner -->
        <div class="bg-gradient-to-r from-green-50 via-blue-50 to-purple-50 border-l-4 border-green-500 rounded-xl p-6 shadow-md">
            <div class="flex items-start">
                <div class="flex-shrink-0">
                    <div class="bg-green-100 rounded-full p-3">
                        <i class="fas fa-check-circle text-green-600 text-2xl"></i>
                    </div>
                </div>
                <div class="ml-4 flex-1">
                    <h3 class="text-lg font-bold text-gray-800 mb-2">
                        System Status: All Systems Operational
                    </h3>
                    <p class="text-gray-700 mb-3">
                        Welcome to Edoxi Training Center Assessment Management System. All services are running smoothly and ready for use.
                    </p>
                    <div class="flex flex-wrap gap-3 text-sm">
                        <span class="bg-white px-3 py-1 rounded-full text-gray-700 border border-gray-200">
                            <i class="fas fa-circle text-green-500 mr-1"></i> Assessment System
                        </span>
                        <span class="bg-white px-3 py-1 rounded-full text-gray-700 border border-gray-200">
                            <i class="fas fa-circle text-green-500 mr-1"></i> Question Bank
                        </span>
                        <span class="bg-white px-3 py-1 rounded-full text-gray-700 border border-gray-200">
                            <i class="fas fa-circle text-green-500 mr-1"></i> Results Processing
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>