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

$exams = $pdo->query("SELECT * FROM exams ORDER BY created_at DESC")->fetchAll();
$exam_id = $_GET['exam_id'] ?? null;

$students = [];
if ($exam_id) {
    $stmt = $pdo->prepare("
        SELECT s.*, se.status, se.obtained_marks, se.percentage, se.result 
        FROM students s
        LEFT JOIN student_exams se ON s.id = se.student_id
        WHERE s.exam_id = ?
    ");
    $stmt->execute([$exam_id]);
    $students = $stmt->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>Students</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-100">
    <?php include 'includes/nav.php'; ?>
    
    <div class="max-w-7xl mx-auto px-4 py-8">
        <h1 class="text-3xl font-bold text-gray-800 mb-6">
            <i class="fas fa-users text-green-600 mr-2"></i> View Students
        </h1>
        
        <div class="bg-white rounded-xl shadow-lg p-6 mb-6">
            <label class="block text-gray-700 font-semibold mb-2">Select Exam</label>
            <select onchange="window.location.href='?exam_id='+this.value" 
                    class="w-full px-4 py-3 border rounded-lg">
                <option value="">-- Select an Exam --</option>
                <?php foreach ($exams as $e): ?>
                <option value="<?php echo $e->id; ?>" <?php echo $exam_id == $e->id ? 'selected' : ''; ?>>
                    <?php echo escape($e->title); ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>
        
        <?php if ($exam_id && !empty($students)): ?>
        <div class="bg-white rounded-xl shadow-lg overflow-hidden">
            <table class="min-w-full">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Name</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Mobile</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Company</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Result</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200">
                    <?php foreach ($students as $s): ?>
                    <tr>
                        <td class="px-6 py-4"><?php echo escape($s->full_name); ?></td>
                        <td class="px-6 py-4"><?php echo escape($s->mobile); ?></td>
                        <td class="px-6 py-4"><?php echo escape($s->company); ?></td>
                        <td class="px-6 py-4">
                            <span class="px-2 py-1 text-xs rounded-full
                                <?php echo $s->status === 'completed' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800'; ?>">
                                <?php echo ucfirst($s->status ?? 'Not Started'); ?>
                            </span>
                        </td>
                        <td class="px-6 py-4">
                            <?php if ($s->status === 'completed'): ?>
                                <?php echo $s->percentage; ?>% (<?php echo strtoupper($s->result); ?>)
                            <?php else: ?>
                                -
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php elseif ($exam_id): ?>
        <div class="bg-white rounded-xl shadow-lg p-12 text-center">
            <i class="fas fa-inbox text-gray-400 text-6xl mb-4"></i>
            <p class="text-gray-600">No students registered for this exam yet.</p>
        </div>
        <?php endif; ?>
    </div>
</body>
</html>