document.addEventListener('DOMContentLoaded', () => {
    document.querySelectorAll('.linkfiliate-deactivate-plugin').forEach(button => {
        button.addEventListener('click', async e => {
            e.preventDefault();

            const plugin = button.dataset.plugin;
            const notice = button.closest('.notice'); // parent banner

            // if (!confirm(`Are you sure you want to deactivate ${plugin}?`)) return;

            // Show loading state
            button.disabled = true;
            button.textContent = 'Deactivating...';
            button.classList.add('updating-message');

            const formData = new FormData();
            formData.append('action', 'linkfiliate_deactivate_plugin');
            formData.append('plugin', plugin);
            formData.append('nonce', linkfiliateData.nonce);

            try {
                const response = await fetch(linkfiliateData.ajaxUrl, {
                    method: 'POST',
                    body: formData
                });

                const result = await response.json();

                if (result.success) {
                    // Show success feedback
                    button.textContent = 'Deactivated ✅';
                    button.classList.remove('updating-message');
                    button.classList.add('button-success');

                    // Smooth fade-out animation for the notice
                    if (notice) {
                        notice.style.transition = 'opacity 0.6s ease, transform 0.4s ease';
                        notice.style.opacity = '0';
                        notice.style.transform = 'translateY(-10px)';
                        setTimeout(() => notice.remove(), 700);
                    }

                    // Optional: reload after short delay
                    setTimeout(() => location.reload(), 1000);
                } else {
                    alert(result.data.message || 'Error deactivating plugin');
                    button.textContent = 'Deactivate Plugin';
                    button.disabled = false;
                    button.classList.remove('updating-message');
                }
            } catch (error) {
                console.error('Request failed:', error);
                alert('Failed to send request.');
                button.textContent = 'Deactivate Plugin';
                button.disabled = false;
                button.classList.remove('updating-message');
            }
        });
    });
});
