<?php
error_reporting(0); // إخفاء الأخطاء لمنع انهيار الـ JSON
header('Content-Type: application/json; charset=utf-8');
include_once '../db_connect.php';

$tid = $_GET['tid'] ?? '';

if (empty($tid)) {
    echo json_encode(['status' => 'error', 'students' => []]);
    exit;
}

// تنظيف الـ TID في حال كان يحتوي على كلمة teacher_
$clean_tid = str_replace('teacher_', '', $tid);

$sql = "SELECT * FROM students WHERE teacher_name LIKE '%$clean_tid%'";
$result = $conn->query($sql);

$students = [];
if ($result && $result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $students[] = $row;
    }
}

echo json_encode(['status' => 'success', 'students' => $students, 'data' => $students]);
?>