<?php
include('db_connection.php');

$class_id = isset($_GET['class_id']) ? (int)$_GET['class_id'] : null;

if ($class_id === null) {
    echo json_encode(['success' => false, 'message' => 'Class ID is required.']);
    exit();
}

try {
    $stmt = $pdo->prepare("SELECT id, username FROM emails_v WHERE class_id = :class_id ORDER BY username ASC");
    $stmt->bindParam(':class_id', $class_id, PDO::PARAM_INT);
    $stmt->execute();
    $students = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode(['success' => true, 'students' => $students]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>
