<?php
header('Content-Type: application/json; charset=utf-8');
include_once '../db_connect.php';

$tid = $conn->real_escape_string($_GET['tid'] ?? '');

if (empty($tid)) {
    echo json_encode(['status' => 'error', 'message' => 'معرف المدرس مفقود']);
    exit;
}

$students = [];
// جلب الطلاب المرتبطين بالرقم البرمجي للمدرس
$sql = "SELECT student_id, student_name FROM students WHERE teacher_name LIKE '%$tid%' ORDER BY student_name ASC";
$result = $conn->query($sql);

if ($result && $result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $students[] = $row;
    }
}
echo json_encode(['status' => 'success', 'students' => $students]);
?>