<?php
#-- get_subjects_marksenter.php --#
include("includes/db.php");

if (isset($_POST['studentId'], $_POST['classId'], $_POST['examId'])) {
    $studentId = $_POST['studentId'];
    $examId = $_POST['examId'];

    // Fetch assigned subjects for the selected student
    $query = "SELECT sub1, sub2, sub3, sub4, sub5, sub6, sub7
              FROM studentsub
              WHERE student_id = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $studentId);
    $stmt->execute();
    $result = $stmt->get_result();

    // Check if any subjects are assigned to the student
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $subjectCodes = array_filter([$row['sub1'], $row['sub2'], $row['sub3'], $row['sub4'], $row['sub5'], $row['sub6'], $row['sub7']]);

        // Prepare an IN clause with the retrieved subject codes
        $placeholders = implode(',', array_fill(0, count($subjectCodes), '?'));
        $subjectQuery = "SELECT subject_code, subject_name, theory_full_marks
                         FROM subjects
                         WHERE subject_code IN ($placeholders)";
        $stmtSubjects = $conn->prepare($subjectQuery);

        // Bind parameters dynamically
        $stmtSubjects->bind_param(str_repeat("s", count($subjectCodes)), ...$subjectCodes);
        $stmtSubjects->execute();
        $subjectResult = $stmtSubjects->get_result();

        // Output the HTML for each subject's input field (Theory marks only)
        while ($subject = $subjectResult->fetch_assoc()) {
            $subjectCode = $subject['subject_code'];
            $subjectName = $subject['subject_name'];
            $theoryFullMarks = $subject['theory_full_marks'];

            // Fetch obtained marks for the subject if any exist for the student in the selected exam
            $marksQuery = "SELECT marks FROM marks 
                           WHERE student_id = ? AND exam_type = ? AND subject_id = ?";
            $marksStmt = $conn->prepare($marksQuery);
            $marksStmt->bind_param("iis", $studentId, $examId, $subjectCode);
            $marksStmt->execute();
            $marksResult = $marksStmt->get_result();
            $obtainedMarks = 0;
            if ($marksResult->num_rows > 0) {
                $marksRow = $marksResult->fetch_assoc();
                $obtainedMarks = $marksRow['marks'];
            }

            // Output the input field for marks with pre-filled obtained marks
            echo "<div class='form-group col-md-6'>
                    <label for='subject_$subjectCode'>$subjectName [Sub Code: $subjectCode] (Theory: $theoryFullMarks)</label>
                    <input type='number' name='marks[$subjectCode][theory]' class='form-control marks-input' 
                           id='subject_$subjectCode'
                           data-max-marks='$theoryFullMarks' value='$obtainedMarks' 
                           placeholder='Enter theory marks for $subjectName' />
                    <small class='hidden' >Full Marks: $theoryFullMarks</small>
                  </div>";
            $marksStmt->close();
        }

        $stmtSubjects->close();
    } else {
        echo "<p>No subjects assigned to the selected student.</p>";
    }

    $stmt->close();
} else {
    echo "<p>Invalid data received.</p>";
}

$conn->close();
?>
