// =============================================================================
// PAINEL DO ANUNCIANTE — [imz_meus_anuncios]
// Pausar/retomar anúncios via AJAX com feedback por toast.
// =============================================================================

(function () {
    'use strict';

    function imzAdsToast(message, type) {
        type = type || 'success';
        var container = document.getElementById('imz-toast-container');
        if (!container) {
            container = document.createElement('div');
            container.id = 'imz-toast-container';
            container.setAttribute('role', 'status');
            container.setAttribute('aria-live', 'polite');
            document.body.appendChild(container);
        }
        var toast = document.createElement('div');
        toast.className = 'imz-toast imz-toast-' + type;
        toast.textContent = message;
        container.appendChild(toast);
        requestAnimationFrame(function () { toast.classList.add('visible'); });
        setTimeout(function () {
            toast.classList.remove('visible');
            setTimeout(function () { toast.remove(); }, 300);
        }, 4000);
    }

    document.addEventListener('DOMContentLoaded', function () {
        var panel = document.getElementById('imz-my-ads');
        if (!panel || typeof imz_myads_data === 'undefined') return;

        panel.addEventListener('click', function (e) {
            var btn = e.target.closest('.imz-ad-toggle-btn');
            if (!btn) return;

            var adId = btn.getAttribute('data-ad-id');
            var newStatus = btn.getAttribute('data-new-status');
            btn.disabled = true;

            var fd = new FormData();
            fd.append('action', 'imz_ad_toggle_status');
            fd.append('ad_id', adId);
            fd.append('new_status', newStatus);
            fd.append('nonce', imz_myads_data.nonce_toggle);

            fetch(imz_myads_data.ajax_url, { method: 'POST', body: fd })
                .then(function (r) { return r.json(); })
                .then(function (result) {
                    if (!result.success) {
                        throw new Error((result.data && result.data.message) || 'Erro desconhecido.');
                    }
                    imzAdsToast(result.data.message, 'success');

                    // Atualizar badge e botão no cartão sem reload
                    var card = btn.closest('.imz-my-ad-card');
                    if (card) {
                        var badge = card.querySelector('.imz-ad-status-badge');
                        if (badge) {
                            badge.className = 'imz-ad-status-badge imz-ad-status-' + result.data.new_status;
                            badge.textContent = result.data.new_label;
                        }
                        var isPaused = result.data.new_status === 'pausado';
                        btn.setAttribute('data-new-status', isPaused ? 'ativo' : 'pausado');
                        btn.textContent = isPaused ? '▶ Retomar' : '⏸ Pausar';
                        btn.className = isPaused
                            ? 'imz-botao-principal imz-mt0 imz-ad-toggle-btn'
                            : 'imz-botao-secundario imz-ad-toggle-btn';
                    }
                })
                .catch(function (error) {
                    imzAdsToast(error.message, 'error');
                })
                .finally(function () {
                    btn.disabled = false;
                });
        });
    });
})();
