<?php
/**
 * DEBUG VERSION of admin_students.php
 * This will help identify exactly what's causing the blank page
 */

// Enable error reporting to see all errors
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>ADMIN STUDENTS DEBUG - STEP 1</h1>";
echo "<p>Starting debug process...</p>";

// Start session
echo "<h2>Step 1: Starting Session</h2>";
session_start();
echo "✓ Session started successfully<br>";

// Check if config.php exists and works
echo "<h2>Step 2: Testing Config.php</h2>";
if (!file_exists('config.php')) {
    die("❌ config.php file not found in current directory: " . __DIR__);
}
echo "✓ config.php file exists<br>";

try {
    require_once 'config.php';
    echo "✓ config.php loaded successfully<br>";
} catch (Exception $e) {
    die("❌ Error loading config.php: " . $e->getMessage());
}

// Check if $conn exists and works
echo "<h2>Step 3: Testing Database Connection</h2>";
if (!isset($conn) || !$conn instanceof mysqli) {
    die("❌ Database connection (\$conn) is not available or not a mysqli object");
}
echo "✓ Database connection available<br>";

// Test the connection
if ($conn->connect_error) {
    die("❌ Database connection failed: " . $conn->connect_error);
}
echo "✓ Database connection working<br>";

// Check session variables
echo "<h2>Step 4: Testing Session Variables</h2>";
$required_session_vars = ['loggedin', 'id', 'username', 'role', 'school_id'];
foreach ($required_session_vars as $var) {
    if (!isset($_SESSION[$var])) {
        die("❌ Required session variable '$var' not set");
    }
    echo "✓ Session variable '$var' = " . (is_scalar($_SESSION[$var]) ? $_SESSION[$var] : gettype($_SESSION[$var])) . "<br>";
}

// Test basic database queries
echo "<h2>Step 5: Testing Basic Database Queries</h2>";

try {
    // Test school data
    $sql = "SELECT school_name FROM schools WHERE school_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $_SESSION["school_id"]);
    $stmt->execute();
    $result = $stmt->get_result();
    $school = $result->fetch_assoc();
    echo "✓ School query successful: " . ($school['school_name'] ?? 'Unknown') . "<br>";
    $stmt->close();
    
    // Test classes
    $sql = "SELECT COUNT(*) as count FROM classes WHERE school_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $_SESSION["school_id"]);
    $stmt->execute();
    $result = $stmt->get_result();
    $classes_count = $result->fetch_assoc()['count'];
    echo "✓ Classes query successful: $classes_count classes found<br>";
    $stmt->close();
    
    // Test subjects table
    $sql = "SELECT COUNT(*) as count FROM subjects";
    $result = $conn->query($sql);
    if ($result) {
        $subjects_count = $result->fetch_assoc()['count'];
        echo "✓ Subjects query successful: $subjects_count subjects found<br>";
    } else {
        echo "❌ Subjects table query failed<br>";
    }
    
    // Test combinations table
    $sql = "SELECT COUNT(*) as count FROM subject_combination_templates WHERE school_id = ? AND is_active = 1";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $_SESSION["school_id"]);
    $stmt->execute();
    $result = $stmt->get_result();
    $combinations_count = $result->fetch_assoc()['count'];
    echo "✓ Subject combinations query successful: $combinations_count combinations found<br>";
    $stmt->close();
    
} catch (Exception $e) {
    die("❌ Database query error: " . $e->getMessage());
}

// Check if subject_management_class.php exists (if it's supposed to be included)
echo "<h2>Step 6: Checking Subject Management Class</h2>";
if (file_exists('subject_management_class.php')) {
    echo "✓ subject_management_class.php exists<br>";
    try {
        require_once 'subject_management_class.php';
        echo "✓ subject_management_class.php loaded successfully<br>";
        
        if (class_exists('SubjectManager')) {
            echo "✓ SubjectManager class found<br>";
            
            try {
                $subjectManager = new SubjectManager($conn, $_SESSION["school_id"]);
                echo "✓ SubjectManager instantiated successfully<br>";
                
                // Test a simple method
                $subjects = $subjectManager->getSubjects();
                echo "✓ getSubjects() method works - returned " . count($subjects) . " subjects<br>";
                
            } catch (Exception $e) {
                echo "❌ SubjectManager method error: " . $e->getMessage() . "<br>";
            }
        } else {
            echo "❌ SubjectManager class not found<br>";
        }
    } catch (Exception $e) {
        echo "❌ Error loading subject_management_class.php: " . $e->getMessage() . "<br>";
    }
} else {
    echo "⚠️ subject_management_class.php not found (this may be normal)<br>";
}

echo "<h2>DEBUG COMPLETE</h2>";
echo "<p>If you reached this point, the basic setup is working. The issue might be in the HTML rendering or JavaScript.</p>";
echo "<p><strong>Next steps:</strong></p>";
echo "<ul>";
echo "<li>If all steps passed, the issue is likely in the HTML output or JavaScript</li>";
echo "<li>If any step failed, that's the root cause of your blank page</li>";
echo "<li>Check the specific error message for the failed step</li>";
echo "</ul>";
?>
