<?php
require_once 'includes/db.php'; 

if (isset($_POST['query'])) {
    $query = trim($_POST['query']);

    $sql = "SELECT * FROM student 
            WHERE adminNo LIKE ? 
            OR surname LIKE ? 
            OR otherNames LIKE ?
            LIMIT 10";

    $stmt = $pdo->prepare($sql);
    $like = "%{$query}%";
    $stmt->execute([$like, $like, $like]);

    if ($stmt->rowCount() > 0) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $fullName = $row['surname'] . ' ' . $row['otherNames'];
            $class = $row['class'];
            $adminNo = $row['adminNo'];

            echo "<div class='suggestion-item' 
                  onclick=\"selectStudent('{$adminNo}')\">
                    <strong>{$adminNo}</strong> - {$fullName} ({$class})
                  </div>";
        }
    } else {
        echo "<div class='suggestion-item text-muted'>No matching student found</div>";
    }
}
?>
