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

if (isset($_GET['year_id'])) {
    $yearId = (int)$_GET['year_id'];
    
    // Get academic year info
    $yearQuery = $conn->query("SELECT * FROM academic_years WHERE year_id = $yearId");
    $yearData = $yearQuery->fetch_assoc();
    
    // Fetch subject assignments for this year
    $query = "
        SELECT c.class_id, c.class_name, c.class_numeric,
               s.section_id, s.section_name,
               ay.ay_ad, ay.ay_bs,
               GROUP_CONCAT(DISTINCT sub.subject_name ORDER BY sub.subject_name SEPARATOR ', ') as subjects,
               GROUP_CONCAT(DISTINCT sub.subject_code ORDER BY sub.subject_name SEPARATOR ', ') as subject_codes,
               COUNT(DISTINCT csm.subject_id) as subject_count,
               SUM(COALESCE(sub.theory_full_marks, 0) + COALESCE(sub.internal_full_marks, 0)) as total_marks,
               SUM(COALESCE(sub.theory_credit_hour, 0) + COALESCE(sub.internal_credit_hour, 0)) as total_credits
        FROM classes c
        JOIN class_section cs ON c.class_id = cs.class_id
        JOIN sections s ON cs.section_id = s.section_id
        JOIN academic_years ay ON ay.year_id = ?
        LEFT JOIN class_subject_mapping csm ON c.class_id = csm.class_id 
            AND csm.subject_id IS NOT NULL
        LEFT JOIN subjects sub ON csm.subject_id = sub.id
        WHERE cs.status = 1 AND c.status = 1 AND s.status = 1
        GROUP BY c.class_id, s.section_id
        ORDER BY c.class_numeric, s.section_name
    ";
    
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $yearId);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $counter = 1;
        while ($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>{$counter}</td>";
            echo "<td>" . htmlspecialchars($row['class_name']) . " (" . $row['class_numeric'] . ")</td>";
            echo "<td>" . htmlspecialchars($row['section_name']) . "</td>";
            echo "<td>{$row['ay_ad']}<br><small>{$row['ay_bs']}</small></td>";
            echo "<td>";
            if ($row['subjects']) {
                $subjects = explode(', ', $row['subjects']);
                $codes = explode(', ', $row['subject_codes']);
                foreach ($subjects as $index => $subject) {
                    $code = isset($codes[$index]) ? $codes[$index] : '';
                    echo "<span class='subject-badge' title='{$code}'>" . htmlspecialchars($subject) . "</span>";
                }
                echo "<small class='text-muted d-block mt-1'>" . $row['subject_count'] . " subject(s)</small>";
            } else {
                echo "<span class='text-muted'>No subjects assigned</span>";
            }
            echo "</td>";
            echo "<td>" . ($row['total_marks'] ?: '0') . "</td>";
            echo "<td>" . ($row['total_credits'] ?: '0.0') . "</td>";
            echo "<td>";
            echo "<button class='btn btn-xs btn-info view-subjects-btn' 
                    data-class-id='{$row['class_id']}' 
                    data-section-id='{$row['section_id']}'>
                    <i class='fa fa-eye'></i> View
                  </button>";
            echo "<button class='btn btn-xs btn-danger remove-all-btn ml-1'
                    data-class-id='{$row['class_id']}'
                    data-section-id='{$row['section_id']}'>
                    <i class='fa fa-trash'></i> Remove All
                  </button>";
            echo "</td>";
            echo "</tr>";
            $counter++;
        }
    } else {
        echo "<tr><td colspan='8' class='text-center'>No subject combinations found for this academic year</td></tr>";
    }
} else {
    echo "<tr><td colspan='8' class='text-center'>Please select an academic year</td></tr>";
}
?>