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

$query = "SELECT * FROM academic_years ORDER BY start_date DESC";
$result = $conn->query($query);

if ($result->num_rows > 0) {
    echo '<div class="list-group">';
    while ($row = $result->fetch_assoc()) {
        $activeClass = $row['status'] == 'active' ? 'list-group-item-success' : '';
        echo '<div class="list-group-item ' . $activeClass . '">';
        echo '<div class="d-flex justify-content-between align-items-center">';
        echo '<div>';
        echo '<h6 class="mb-1">' . $row['ay_ad'] . '</h6>';
        echo '<small>' . $row['ay_bs'] . '</small><br>';
        echo '<small>' . $row['start_date'] . ' to ' . $row['end_date'] . '</small>';
        echo '</div>';
        echo '<div>';
        echo '<span class="badge badge-' . ($row['status'] == 'active' ? 'success' : 'secondary') . '">';
        echo ucfirst($row['status']);
        echo '</span>';
        if ($row['status'] != 'active') {
            echo '<button class="btn btn-sm btn-outline-success ml-2 set-active-year" 
                    data-year-id="' . $row['year_id'] . '">Set Active</button>';
        }
        echo '</div>';
        echo '</div>';
        echo '</div>';
    }
    echo '</div>';
} else {
    echo '<div class="alert alert-info">No academic years found</div>';
}
?>