<?php
session_start();
require_once '../config/database.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    echo '<div class="alert alert-danger text-center">অনুমোদন ব্যর্থ হয়েছে</div>';
    exit();
}

// Get search parameters
$class_id = $_GET['class_id'] ?? '';
$session = $_GET['session'] ?? date('Y');
$exam_type = $_GET['exam_type'] ?? '';
$student_search = $_GET['student_search'] ?? '';

// Validate required parameters
if (!$class_id || !$session || !$exam_type) {
    echo '<div class="alert alert-info text-center"><i class="fas fa-info-circle"></i> কোন তথ্য পাওয়া যায়নি</div>';
    exit();
}

try {
    // Get students based on class, session, and exam type
    $stmt = $pdo->prepare("
        SELECT s.id, s.student_id, s.first_name, s.last_name, s.roll_number
        FROM students s 
        WHERE s.class_id = ? AND s.status = 'active'
        AND EXISTS (
            SELECT 1 FROM results r 
            WHERE r.student_id = s.id 
            AND r.session = ? 
            AND r.exam_type = ?
        )
        ORDER BY s.roll_number, s.first_name, s.last_name
    ");
    $stmt->execute([$class_id, $session, $exam_type]);
    $all_students_in_class = $stmt->fetchAll();

    // Filter students based on search term
    if (!empty($student_search)) {
        $students_in_class = array_filter($all_students_in_class, function($student) use ($student_search) {
            $search_term = strtolower($student_search);
            return strpos(strtolower($student['first_name']), $search_term) !== false ||
                   strpos(strtolower($student['last_name']), $search_term) !== false ||
                   strpos($student['roll_number'], $search_term) !== false ||
                   strpos($student['student_id'], $search_term) !== false;
        });
    } else {
        $students_in_class = $all_students_in_class;
    }

    // Output the table content
    if (empty($students_in_class)) {
        echo '<div class="alert alert-info text-center"><i class="fas fa-info-circle"></i> কোন তথ্য পাওয়া যায়নি</div>';
    } else {
        echo '<table class="student-list-table">
                <thead>
                    <tr>
                        <th>রোল</th>
                        <th>শিক্ষার্থী আইডি</th>
                        <th>নাম</th>
                        <th>কার্যক্রম</th>
                    </tr>
                </thead>
                <tbody>';
        
        foreach ($students_in_class as $student_item) {
            echo '<tr>';
            echo '<td>' . htmlspecialchars($student_item['roll_number']) . '</td>';
            echo '<td>' . htmlspecialchars($student_item['student_id']) . '</td>';
            echo '<td>' . htmlspecialchars($student_item['first_name'] . ' ' . $student_item['last_name']) . '</td>';
            echo '<td>
                    <a href="?student_id=' . $student_item['id'] . '&session=' . urlencode($session) . '&exam_type=' . urlencode($exam_type) . '" class="btn btn-primary btn-sm">
                        <i class="fas fa-eye"></i> ফলাফল দেখুন
                    </a>
                  </td>';
            echo '</tr>';
        }
        
        echo '</tbody></table>';
    }
} catch (Exception $e) {
    echo '<div class="alert alert-danger text-center">ত্রুটি দেখা দিয়েছে, অনুগ্রহ করে আবার চেষ্টা করুন</div>';
}
?>
<style>
.student-list-table {
    width: 100%;
    border-collapse: collapse;
    margin: 15px 0;
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.05);
    border-radius: 6px;
    overflow: hidden;
    font-size: 12px;
}

.student-list-table th,
.student-list-table td {
    border: 1px solid #dee2e6;
    padding: 8px;
    text-align: center;
}

.student-list-table th {
    background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
    color: white;
    font-weight: 600;
}

.student-list-table tbody tr:nth-child(even) {
    background-color: #f8f9fa;
}

.student-list-table tbody tr:hover {
    background-color: #e3f2fd;
    cursor: pointer;
    transition: background-color 0.2s;
}

.alert {
    padding: 1rem;
    margin-bottom: 1rem;
    border: 1px solid transparent;
    border-radius: 0.375rem;
}

.alert-info {
    color: #055160;
    background-color: #cff4fc;
    border-color: #b6effb;
}

.alert-danger {
    color: #842029;
    background-color: #f8d7da;
    border-color: #f5c2c7;
}

.text-center {
    text-align: center;
}

/* Spinner styles */
.spinner-border {
    display: inline-block;
    width: 2rem;
    height: 2rem;
    vertical-align: -0.125em;
    border: 0.25em solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: spinner-border 0.75s linear infinite;
}

@keyframes spinner-border {
    to {
        transform: rotate(360deg);
    }
}

.text-primary {
    color: #0d6efd;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}
</style>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">