<?php
header('Content-Type: application/json');
include('last_db_config.php');

$response = [
    'evaluated'    => 0,
    'notEvaluated' => 0,
    'options'      => []
];

if (isset($_POST['SchoolID'], $_POST['Class']) && $_POST['SchoolID'] !== '' && $_POST['Class'] !== '') {
    $school_id = $_POST['SchoolID'];
    $class     = $_POST['Class'];

    // 1) عدد المقيمين
    $res1 = mysqli_query($conn, "
      SELECT COUNT(DISTINCT t.StudentID) AS cnt
      FROM testresult t
      JOIN student s ON s.StudentID = t.StudentID
      WHERE s.SchoolID='$school_id' AND s.Class='$class'
    ");
    $row1 = mysqli_fetch_assoc($res1) ?: ['cnt'=>0];
    $response['evaluated'] = intval($row1['cnt']);

    // 2) الطلاب غير المقيمين + جمعهم
    $res2 = mysqli_query($conn, "
      SELECT s.StudentID, s.Name
      FROM student s
      LEFT JOIN testresult t ON s.StudentID = t.StudentID
      WHERE s.SchoolID='$school_id' AND s.Class='$class' AND t.StudentID IS NULL
      ORDER BY s.Name
    ");
    $not = [];
    while ($r = mysqli_fetch_assoc($res2)) {
        $not[] = ['StudentID'=>$r['StudentID'],'Name'=>$r['Name']];
    }
    $response['notEvaluated'] = count($not);
    $response['options']      = $not;
}

echo json_encode($response);
?>