function toggleActive(table, id, btn) {
    btn.disabled = true;
    fetch('../ajax/handler.php', {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: `action=toggle_active&table=${table}&id=${id}`
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            const newStatus = data.new_status;
            if (newStatus == 1) {
                btn.innerHTML = '<i class="bi bi-toggle-on"></i> Active';
                btn.className = 'btn btn-sm btn-success';
            } else {
                btn.innerHTML = '<i class="bi bi-toggle-off"></i> Inactive';
                btn.className = 'btn btn-sm btn-secondary';
            }
        } else {
            alert('Error: ' + data.message);
        }
    })
    .catch(err => {
        alert('Network error');
        console.error(err);
    })
    .finally(() => { btn.disabled = false; });
}