import toggleClass from '@assets/js/toggleClass.js';

window.toggleClass = toggleClass;

var reloadPayment = document.querySelector('.js-reloadPayment');

if(reloadPayment){
  reloadPayment.addEventListener('click', function (){
    $.ajax({
      url: 'index.php?route=payment/getnet/reloadPix',
      type: 'get',
      dataType: 'json',
      timeout: 3000,
      contentType: 'application/json; charset=utf-8',
      error: function(xhr, ajaxOptions, thrownError) {
        console.log(thrownError + '\r\n' + xhr.statusText + '\r\n' + xhr.responseText);
      },
      success: function(json) {
        if(json){
          var imgQR = document.querySelector('.js-imgQr');
          var spanQr = document.querySelector('.js-copy-content');
          imgQR.setAttribute('src','https://chart.googleapis.com/chart?chs=400x400&cht=qr&chl='+json.additional_data.qr_code);
          spanQr.textContent = json.additional_data.qr_code;
        }
      },
    });
  });
}

setInterval( paymentStatus, 5000);

function paymentStatus(){
  var imgQR = document.querySelector('.js-imgQr');
  if(imgQR && !(imgQR.getAttribute('src') == 'image/qrcode_pago.png')){
    $.ajax({
      url: 'index.php?route=checkout/success/verifyPaymentPix',
      type: 'get',
      dataType: 'json',
      timeout: 3000,
      contentType: 'application/json; charset=utf-8',
      error: function(xhr, ajaxOptions, thrownError) {
        console.log(thrownError + '\r\n' + xhr.statusText + '\r\n' + xhr.responseText);
      },
      success: function(json) {
        if(json){
          imgQR.setAttribute('src','image/qrcode_pago.png');
        }
      },
    });
  }
}