<?php
// Include the database configuration file
include("includes/db.php");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Debugging statements
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    $classId = $conn->real_escape_string($_POST["classId"]);
    $subjectId = $conn->real_escape_string($_POST["subjectId"]);

    // // Debugging statements
    // echo "Class ID: $classId<br>";
    // echo "Subject ID: $subjectId<br>";


    // Fetch subject code based on subjectId from the subjects table
    $subjectCodeQuery = "SELECT subject_code , theory_full_marks FROM subjects WHERE id = '$subjectId'";
    $subjectCodeResult = $conn->query($subjectCodeQuery);

    if ($subjectCodeResult->num_rows > 0) {
        $subjectCodeRow = $subjectCodeResult->fetch_assoc();
        $subjectCode = $subjectCodeRow['subject_code'];
        $fullmarksth = $subjectCodeRow['theory_full_marks'];

          echo "Subject code: $subjectCode <br>";
          echo "Full Marks: $fullmarksth <br>";

        // Fetch students assigned the selected subject based on class
        $query = "SELECT s.student_id, s.symbol_no, s.regd_id, s.name, st.sub1, st.sub2, st.sub3, st.sub4, st.sub5, st.sub6, st.sub7
              FROM students s
              LEFT JOIN studentsub st ON s.student_id = st.student_id
              WHERE s.class_id = '$classId'
                AND (st.sub1 = '$subjectCode' OR st.sub2 = '$subjectCode' OR st.sub3 = '$subjectCode' OR st.sub4 = '$subjectCode' OR st.sub5 = '$subjectCode' OR st.sub6 = '$subjectCode' OR st.sub7 = '$subjectCode') ORDER BY symbol_no";

        $result = $conn->query($query);

        // Generate marks entry fields for each student
        $output = '';
        while ($row = $result->fetch_assoc()) {
            $studentId = $row['student_id'];

            // Fetch subject details from subjects table using subject code
            $subjectQuery = "SELECT subject_name, theory_full_marks, internal_full_marks, theory_credit_hour, internal_credit_hour
                             FROM subjects
                             WHERE subject_code = '$subjectCode'";

            $subjectResult = $conn->query($subjectQuery);
            $subjectRow = $subjectResult->fetch_assoc();

          $output .= '<div class="form-group">';
            // Output subject details
            $output .= '<p></p>';
$output .= '<b><label for="marks_' . $studentId . '" style="width:400px" > ' . $row['name'] .'<span style="color:red">' ." [Symbol No ".$row['symbol_no']."]".'</span>'.':</label></b>';
            $output .= '<input type="number" id="marks_' . $studentId . '" name="marks[' . $studentId . ']" class="form-control"  placeholder="Marks for ' . $subjectRow['subject_name'] .'" required>';
            // $output .= '<input type="number" id="theory_full_marks" name="theory_full_marks" value="'.$subjectRow['theory_full_marks'].'" class="form-control"  readonly>';

            
            // $output .= '<p>Theory Full Marks: ' . $subjectRow['theory_full_marks'] . '</p>';
            // $output .= '<p>Internal Full Marks: ' . $subjectRow['internal_full_marks'] . '</p>';
            // Include additional subject details as needed

            $output .= '</div>';
        }

        // Return the marks entry fields
        echo $output;
    } else {
        echo "Subject code not found for Subject ID: $subjectId";
    }

    // Close the database connection
    $conn->close();
}
?>
