<?php
// Simple test to check if the basic query works
require_once 'config/database.php';

echo "<h2>Query Test</h2>";

try {
    // Test basic student query
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM students");
    $total_students = $stmt->fetch()['count'];
    echo "Total students: " . $total_students . "<br>";
    
    // Test classes query
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM classes WHERE status = 'active'");
    $total_classes = $stmt->fetch()['count'];
    echo "Active classes: " . $total_classes . "<br>";
    
    // Test sample query with filters
    $sql = "SELECT id, student_id, first_name, last_name, class_id, group_name FROM students WHERE 1=1";
    $params = [];
    
    // Add a simple class filter test
    if (isset($_GET['class_id']) && (int)$_GET['class_id'] > 0) {
        $sql .= " AND class_id = ?";
        $params[] = (int)$_GET['class_id'];
        echo "Adding class filter: " . (int)$_GET['class_id'] . "<br>";
    }
    
    $sql .= " LIMIT 5";
    
    echo "Query: " . $sql . "<br>";
    echo "Params: " . json_encode($params) . "<br>";
    
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    $results = $stmt->fetchAll();
    
    echo "<h3>Results:</h3>";
    foreach ($results as $row) {
        echo "ID: " . $row['id'] . " | Name: " . $row['first_name'] . " " . $row['last_name'] . " | Class: " . $row['class_id'] . "<br>";
    }
    
    // Show available classes for testing
    echo "<h3>Available Classes:</h3>";
    $stmt = $pdo->query("SELECT id, name, section FROM classes WHERE status = 'active' ORDER BY name");
    $classes = $stmt->fetchAll();
    foreach ($classes as $class) {
        echo "<a href='?class_id=" . $class['id'] . "'>Test Class " . $class['id'] . ": " . $class['name'] . " (" . $class['section'] . ")</a><br>";
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "<br>";
    echo "SQL State: " . $e->getCode() . "<br>";
}
?>