// Handle Form Submission
document.getElementById('academicYearForm').addEventListener('submit', function(e) {
    e.preventDefault();
    const formData = new FormData(this);

    fetch('api/academic/add_year.php', {
        method: 'POST',
        body: formData
    })
    .then(res => res.json())
    .then(data => {
        if (data.status === 'success') {
            Swal.fire('Success', data.message, 'success');
            $('#academicYearModal').modal('hide');
            location.reload(); // Refresh to update global year settings
        } else {
            Swal.fire('Error', data.message, 'error');
        }
    });
});

// Function to change current year from a table list
function setCurrentYear(yearId) {
    fetch('api/academic/set_current_year.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: `id=${yearId}`
    })
    .then(res => res.json())
    .then(data => {
        if (data.status === 'success') {
            Swal.fire('Updated', 'Current academic year changed.', 'success')
                .then(() => location.reload());
        }
    });
}