<?php
include('../includes/db.php');

if (isset($_POST['class_id']) && isset($_POST['section_id']) && isset($_POST['subject_code'])) {
    $classId = (int)$_POST['class_id'];
    $sectionId = (int)$_POST['section_id'];
    $subjectCode = $conn->real_escape_string($_POST['subject_code']);
    $academicYear = $conn->real_escape_string($_POST['academic_year']);
    $subjectName = $conn->real_escape_string($_POST['subject_name']);
    
    // Get students without marks for this subject
    $query = "SELECT 
                s.student_id,
                s.regd_id,
                s.name,
                s.gender,
                s.email,
                s.phone,
                s.parent_name,
                s.parent_phone
            FROM studentsub ss
            INNER JOIN students s ON ss.student_id = s.student_id
            LEFT JOIN marks m ON s.student_id = m.student_id 
                AND m.class_id = ss.class_id 
                AND m.section_id = ss.section_id
                AND m.subject_id = (SELECT id FROM subjects WHERE subject_code = '$subjectCode' LIMIT 1)
            WHERE ss.class_id = $classId 
            AND ss.section_id = $sectionId
            AND ss.academic_year = '$academicYear'
            AND (
                ss.sub1 = '$subjectCode' OR 
                ss.sub2 = '$subjectCode' OR 
                ss.sub3 = '$subjectCode' OR 
                ss.sub4 = '$subjectCode' OR 
                ss.sub5 = '$subjectCode' OR 
                ss.sub6 = '$subjectCode' OR 
                ss.sub7 = '$subjectCode'
            )
            AND s.status = 'Active'
            AND (m.marks = 0 OR m.marks IS NULL)
            ORDER BY s.name";
    
    $result = $conn->query($query);
    
    echo '<div class="alert alert-danger">';
    echo '<h5><i class="fa fa-exclamation-triangle"></i> Missing Marks Report</h5>';
    echo '<p><strong>Subject:</strong> ' . $subjectName . ' (' . $subjectCode . ')</p>';
    echo '<p><strong>Total Students Missing Marks:</strong> ' . $result->num_rows . '</p>';
    echo '</div>';
    
    if ($result->num_rows > 0) {
        echo '<div class="table-responsive">';
        echo '<table class="table table-bordered table-hover">';
        echo '<thead class="thead-light">';
        echo '<tr>
                <th>#</th>
                <th>Registration ID</th>
                <th>Student Name</th>
                <th>Gender</th>
                <th>Contact</th>
                <th>Parent Contact</th>
                <th>Status</th>
              </tr>';
        echo '</thead>';
        echo '<tbody>';
        
        $counter = 1;
        while ($row = $result->fetch_assoc()) {
            echo '<tr>';
            echo '<td>' . $counter . '</td>';
            echo '<td><strong>' . htmlspecialchars($row['regd_id']) . '</strong></td>';
            echo '<td>' . htmlspecialchars($row['name']) . '</td>';
            echo '<td>' . htmlspecialchars($row['gender']) . '</td>';
            echo '<td>' . htmlspecialchars($row['phone']) . '<br>' . htmlspecialchars($row['email']) . '</td>';
            echo '<td>' . htmlspecialchars($row['parent_name']) . '<br>' . htmlspecialchars($row['parent_phone']) . '</td>';
            echo '<td><span class="badge badge-danger">Missing Marks</span></td>';
            echo '</tr>';
            $counter++;
        }
        
        echo '</tbody>';
        echo '</table>';
        echo '</div>';
        
        // Quick actions
        echo '<div class="mt-3">';
        echo '<a href="enter_marks.php?class_id=' . $classId . '&section_id=' . $sectionId . '&subject_code=' . $subjectCode . '&academic_year=' . $academicYear . '" class="btn btn-success">';
        echo '<i class="fa fa-edit"></i> Enter Marks Now';
        echo '</a>';
        echo '</div>';
    } else {
        echo '<div class="alert alert-success">';
        echo '<i class="fa fa-check-circle"></i> No missing marks found for this subject.';
        echo '</div>';
    }
} else {
    echo '<div class="alert alert-danger">Invalid parameters.</div>';
}
?>