<?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 Class IDs</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> Class ID Finder</h4>
        </div>
        <div class='card-body'>";

try {
    // Get all classes with their IDs
    echo "<h5>All Classes in System</h5>";
    $stmt = $pdo->query("SELECT id, name, section, status FROM classes ORDER BY name, section");
    $classes = $stmt->fetchAll();
    
    if (empty($classes)) {
        echo "<div class='alert alert-warning'>
                <i class='fas fa-exclamation-triangle'></i> 
                No classes found in the system
              </div>";
    } else {
        echo "<div class='alert alert-info'>
                <i class='fas fa-info-circle'></i> 
                Found " . count($classes) . " classes in total
              </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>Status</th>
                            <th>Direct Links</th>
                        </tr>
                    </thead>
                    <tbody>";
        
        foreach ($classes as $class) {
            // Highlight the common class names
            $highlight_names = ['SIX', 'SEVEN', 'EIGHT', 'NINE', 'TEN', 'ELEVEN'];
            $row_class = '';
            foreach ($highlight_names as $name) {
                if (stripos($class['name'], $name) !== false) {
                    $row_class = 'table-success';
                    break;
                }
            }
            
            // Check if this is ID 14
            if ($class['id'] == 14) {
                $row_class = 'table-warning';
                echo "<tr class='$row_class'>
                        <td><strong>{$class['id']}</strong> (This is the one you're seeing)</td>
                        <td>{$class['name']}</td>
                        <td>{$class['section']}</td>
                        <td>{$class['status']}</td>
                        <td>
                            <a href='subject_selection.php?class_id={$class['id']}' class='btn btn-sm btn-primary mb-1'>
                                <i class='fas fa-tasks'></i> Subject Selection
                            </a><br>
                            <a href='subject_enrollment_tools.php?session=2025-2026&class_id={$class['id']}&action=analyze' class='btn btn-sm btn-info'>
                                <i class='fas fa-chart-pie'></i> Enrollment Analysis
                            </a>
                        </td>
                      </tr>";
            } else {
                echo "<tr class='$row_class'>
                        <td>{$class['id']}</td>
                        <td>{$class['name']}</td>
                        <td>{$class['section']}</td>
                        <td>{$class['status']}</td>
                        <td>
                            <a href='subject_selection.php?class_id={$class['id']}' class='btn btn-sm btn-primary mb-1'>
                                <i class='fas fa-tasks'></i> Subject Selection
                            </a><br>
                            <a href='subject_enrollment_tools.php?session=2025-2026&class_id={$class['id']}&action=analyze' class='btn btn-sm btn-info'>
                                <i class='fas fa-chart-pie'></i> Enrollment Analysis
                            </a>
                        </td>
                      </tr>";
            }
        }
        
        echo "      </tbody>
                </table>
              </div>";
    }
    
    // Show academic sessions
    echo "<h5 class='mt-4'>Available Academic Sessions</h5>";
    $stmt = $pdo->query("SELECT session_name, status FROM academic_sessions ORDER BY session_name DESC");
    $sessions = $stmt->fetchAll();
    
    if (!empty($sessions)) {
        echo "<div class='table-responsive'>
                <table class='table table-striped table-bordered'>
                    <thead>
                        <tr>
                            <th>Session Name</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>";
        
        foreach ($sessions as $session) {
            echo "<tr>
                    <td>{$session['session_name']}</td>
                    <td>{$session['status']}</td>
                  </tr>";
        }
        
        echo "      </tbody>
                </table>
              </div>";
    }
    
} catch (Exception $e) {
    echo "<div class='alert alert-danger'>
            <i class='fas fa-exclamation-triangle'></i> 
            Error: " . $e->getMessage() . "
          </div>";
}

echo "<div class='mt-4'>
        <a href='../students/list.php' class='btn btn-secondary'>
            <i class='fas fa-users'></i> Students List
        </a>
        <a href='subject_selection.php' class='btn btn-primary'>
            <i class='fas fa-tasks'></i> Subject Selection
        </a>
      </div>";

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