 (function(){
        var accs = document.querySelectorAll('#wscraper-faq .question');
        accs.forEach(function(q){
            q.addEventListener('click', function(){
                var ans = this.nextElementSibling;
                var isActive = this.classList.contains('active');
                // Cerrar todos
                accs.forEach(function(other){
                    other.classList.remove('active');
                    other.nextElementSibling.style.display = 'none';
                });
                // Abrir el seleccionado
                if (!isActive) {
                    this.classList.add('active');
                    ans.style.display = 'block';
                }
            });
        });
    })();



jQuery(document).ready(function ($) {
  $('#wscraper-run-cron').on('click', function () {
    if (!confirm('¿Estás seguro de ejecutar el monitoreo de precios ahora?, esto consumirá de tus tokens.')) return;

    $.post(ALERTAPRECIO.ajaxurl, {
      action: 'alertaprecio_run_cron_now',
      _ajax_nonce: ALERTAPRECIO.nonce
    }, function (res) {
      if (res.success) {
        alert('✅ Cron ejecutado correctamente. Productos procesados.');
      } else {
        alert('❌ Error al ejecutar el cron.');
      }
    }).fail(function () {
      alert('❌ Falló la llamada AJAX.');
    });
  });

  $('.wscraper-plan-link').on('click', function (e) {
    e.preventDefault();
    $('#wscraper-modal').fadeIn();
  });

  $(document).on('click', '.wscraper-modal-close, #wscraper-modal', function (e) {
    if (e.target.id === 'wscraper-modal' || $(e.target).hasClass('wscraper-modal-close')) {
      $('#wscraper-modal').fadeOut();
    }
  });
});

jQuery(document).ready(function ($) {
  $('.wscraper-plan-link').on('click', function (e) {
    e.preventDefault();
    $('#wscraper-modal').fadeIn();
  });

  $(document).on('click', '.wscraper-modal-button, #wscraper-modal', function (e) {
    if (e.target.id === 'wscraper-modal' || $(e.target).hasClass('wscraper-modal-button')) {
      $('#wscraper-modal').fadeOut();
    }
  });
});


document.getElementById('wscraper-login-google').addEventListener('click', () => {
  const origin = window.location.origin;

  const callbackBase = `${origin}/?rest_route=/alertaprecio/v1/google-callback`;
  const callbackUrl = encodeURIComponent(callbackBase);

  const loginUrl = `https://api.alertaprecio.online/google-wp-login?callback=${callbackUrl}`;

  window.open(loginUrl, '_self');
});

const STORAGE_KEYS = {
  token: [ 'alertaprecio_token'],
  email: ['alertaprecio_email'],
  alerts: ['alertaprecio_alerts'],
  webtoken: ['alertaprecio_webToken']
};

function getUserData() {
  try {
    const token = STORAGE_KEYS.token.map(k => localStorage.getItem(k)).find(v => v);
    const email = STORAGE_KEYS.email.map(k => localStorage.getItem(k)).find(v => v);
    const numberAlerts = STORAGE_KEYS.alerts.map(k => localStorage.getItem(k)).find(v => v);
    return token && email ? { email, numberAlerts } : null;
  } catch (e) {
    console.error('Error al obtener datos de usuario de localStorage:', e);
    return null;
  }
}

const user = getUserData();
const btnWarning  = document.getElementById('btnWarning');
const loginBtn    = document.getElementById('wscraper-login-google');
const navBtns     = document.getElementById('wscraper-nav-buttons');
const emailSpan   = document.getElementById('user-email');
const alertsSpan  = document.getElementById('user-alerts');

if (user) {
  emailSpan.textContent  = user.email;
  alertsSpan.textContent = user.numberAlerts;
  loginBtn.style.display   = 'none';
  navBtns.style.display    = 'flex';
  btnWarning.style.display = 'none';
} else {
  loginBtn.style.display   = 'inline-block';
  navBtns.style.display    = 'none';
  btnWarning.style.display = 'block';
}

  jQuery(document).ready(function ($) {
  $('.wscraper-plan-link').on('click', function (e) {
    e.preventDefault();
    $('#wscraper-modal').fadeIn();
  });

  $(document).on('click', '.wscraper-modal-button, #wscraper-modal', function (e) {
    if (e.target.id === 'wscraper-modal' || $(e.target).hasClass('wscraper-modal-button')) {
      $('#wscraper-modal').fadeOut();
    }
  });
});

jQuery(document).ready(function ($) {
 
  // Cron manual
  $('#wscraper-run-cron').on('click', function () {
    if (!confirm('¿Seguro? Esto consumirá tokens.')) return;
    $.post(ALERTAPRECIO.ajaxurl, {
      action: 'alertaprecio_run_cron_now',
      _ajax_nonce: ALERTAPRECIO.nonce
    }, res => {
      alert(res.success ? '✅ Ejecutado' : '❌ Error');
    }).fail(() => alert('❌ Falló AJAX'));
  });

  // Modal planes
  $('.wscraper-plan-link').on('click', e => {
    e.preventDefault();
    $('#wscraper-modal').fadeIn();
  });
  $(document).on('click', '.wscraper-modal-close, .wscraper-modal-button, #wscraper-modal', function(e) {
    if (e.target.id === 'wscraper-modal' || $(e.target).is('.wscraper-modal-close, .wscraper-modal-button')) {
      $('#wscraper-modal').fadeOut();
    }
  });

  // Login Google
  $('#wscraper-login-google').on('click', () => {
    const cb = encodeURIComponent(`${window.location.origin}/?rest_route=/alertaprecio/v1/google-callback`);
    window.open(`https://api.alertaprecio.online/google-wp-login?callback=${cb}`, '_self');
  });

});