<?php

require_once '../../../config.php';

$conn = getDBConnection();



$class = $_POST['class'] ?? '';

$term = $_POST['term'] ?? '';

$academic_year = $_POST['academic_year'] ?? '';



// Get subjects configured for this class

$subjectsCount = 0;

$stmt = $conn->prepare("SELECT COUNT(*) as count FROM class_subjects WHERE class_name = ?");

$stmt->bind_param("s", $class);

$stmt->execute();

$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {

    $subjectsCount = $row['count'];

}

$stmt->close();



// Get students

$students = [];

$stmt = $conn->prepare("SELECT * FROM students WHERE class = ? AND status = 'active' ORDER BY first_name, last_name");

$stmt->bind_param("s", $class);

$stmt->execute();

$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {

    $students[] = $row;

}

$stmt->close();



// Get score counts for each student

$scoreStats = [];

if ($term && $academic_year) {

    $stmt = $conn->prepare("

        SELECT student_id, 

               COUNT(*) as subjects_entered,

               SUM(total_score) as total_marks

        FROM scores 

        WHERE student_id IN (SELECT id FROM students WHERE class = ?) 

          AND term = ? 

          AND academic_year = ?

        GROUP BY student_id

    ");

    $stmt->bind_param("sss", $class, $term, $academic_year);

    $stmt->execute();

    $result = $stmt->get_result();

    while ($row = $result->fetch_assoc()) {

        $scoreStats[$row['student_id']] = $row;

    }

    $stmt->close();

}



$conn->close();



// Generate table rows

$output = '';

$counter = 1;



if (empty($students)) {

    $output = '<tr><td colspan="8" class="text-center py-4">No students found in this class</td></tr>';

} else {

    foreach ($students as $student) {

        $stats = $scoreStats[$student['id']] ?? null;

        $subjectsEntered = $stats ? $stats['subjects_entered'] : 0;

        $totalMarks = $stats ? $stats['total_marks'] : 0;

        

        // Determine status

        if ($subjectsEntered == 0) {

            $statusClass = 'status-none';

            $statusText = 'No Scores';

        } elseif ($subjectsEntered < $subjectsCount) {

            $statusClass = 'status-incomplete';

            $statusText = 'Incomplete';

        } else {

            $statusClass = 'status-complete';

            $statusText = 'Complete';

        }

        

        $output .= '<tr>';

        $output .= '<td>' . $counter++ . '</td>';

        $output .= '<td>' . htmlspecialchars($student['student_id']) . '</td>';

        $output .= '<td><strong>' . htmlspecialchars($student['first_name'] . ' ' . $student['last_name']) . '</strong></td>';

        $output .= '<td>' . ($student['sex'] ?? 'N/A') . '</td>';

        $output .= '<td><span class="badge score-status-badge ' . $statusClass . '">' . $statusText . '</span></td>';

        $output .= '<td>' . $subjectsEntered . '/' . $subjectsCount . '</td>';

        $output .= '<td><strong>' . $totalMarks . '</strong></td>';

        $output .= '<td>';

        $output .= '<button class="btn btn-sm btn-primary" onclick="openScoreModal(' . $student['id'] . ', \'' . addslashes($student['first_name'] . ' ' . $student['last_name']) . '\', \'' . $student['student_id'] . '\')">';

        $output .= '<i class="fas fa-pen me-1"></i> Enter Scores';

        $output .= '</button>';

        $output .= '</td>';

        $output .= '</tr>';

    }

}



echo $output;