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

// Auth check
if (!isset($_SESSION['user_id']) || ($_SESSION['user_role'] != 'admin' && $_SESSION['user_role'] != 'teacher')) {
    header('Location: ../login.php');
    exit();
}

echo "<!DOCTYPE html>
<html lang='bn'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Find Classes</title>
    <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css' rel='stylesheet'>
    <link href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css' rel='stylesheet'>
</head>
<body>
<div class='container mt-4'>
    <div class='card'>
        <div class='card-header'>
            <h4><i class='fas fa-school'></i> Available Classes</h4>
        </div>
        <div class='card-body'>";

try {
    // Get all classes
    echo "<h5>All Available Classes</h5>";
    $stmt = $pdo->query("SELECT id, name, section FROM classes WHERE status = 'active' ORDER BY name, section");
    $classes = $stmt->fetchAll();
    
    if (empty($classes)) {
        echo "<div class='alert alert-warning'>
                <i class='fas fa-exclamation-triangle'></i> 
                No active classes found in the system
              </div>";
    } else {
        echo "<div class='alert alert-info'>
                <i class='fas fa-info-circle'></i> 
                Found " . count($classes) . " active classes
              </div>";
              
        echo "<div class='table-responsive'>
                <table class='table table-striped table-bordered'>
                    <thead>
                        <tr>
                            <th>Class ID</th>
                            <th>Class Name</th>
                            <th>Section</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>";
        
        foreach ($classes as $class) {
            $highlight = (stripos($class['name'], 'ELEVEN') !== false) ? 'table-success' : '';
            
            echo "<tr class='$highlight'>
                    <td>{$class['id']}</td>
                    <td>{$class['name']}</td>
                    <td>{$class['section']}</td>
                    <td>
                        <a href='subject_selection.php?class_id={$class['id']}' class='btn btn-sm btn-primary'>
                            <i class='fas fa-tasks'></i> Select Subjects
                        </a>
                        <a href='check_enrollment.php?class_id={$class['id']}&session=2025-2026&subject_id=' class='btn btn-sm btn-info'>
                            <i class='fas fa-search'></i> Check Enrollment
                        </a>
                    </td>
                  </tr>";
        }
        
        echo "      </tbody>
                </table>
              </div>";
    }
    
    // Also show academic sessions
    echo "<h5 class='mt-4'>Available Academic Sessions</h5>";
    $stmt = $pdo->query("SELECT session_name FROM academic_sessions WHERE status = 'active' ORDER BY session_name DESC");
    $sessions = $stmt->fetchAll();
    
    if (!empty($sessions)) {
        echo "<ul>";
        foreach ($sessions as $session) {
            echo "<li>{$session['session_name']}</li>";
        }
        echo "</ul>";
    }
    
} catch (Exception $e) {
    echo "<div class='alert alert-danger'>
            <i class='fas fa-exclamation-triangle'></i> 
            Error: " . $e->getMessage() . "
          </div>";
}

echo "</div></div></div></body></html>";