document.addEventListener('DOMContentLoaded', function(event) {
  window.blockSelectReadonly();

  window.info = document.querySelector('[data-info]').getAttribute('data-info');
  info = JSON.parse(info);

  language.add(info);
  language.load('sale/customer');

  var btnDesbloqueio = document.querySelector('.js-costumer-unlock');

  if(btnDesbloqueio){
    btnDesbloqueio.addEventListener('click', function unlockCustomer(e){
      e.preventDefault();
      var costumerLogin = this.getAttribute('data-customer-id');

      $.ajax({
        url: 'index.php?route=sale/customer/unlock&token='+ token +'&customer_id='+ costumerLogin,
        type: 'get',
        success: function(json) {
          window.location.reload();
        },error: function(json) {
          console.log(json);
        }
      });

    });
  }


});

$(document).ready(function($) {
  function exibeContent() {
    $(
      '.pessoa_' +
        $('#juridica, #fisica')
          .filter(':checked')
          .attr('id')
    )
      .prop('disable', false)
      .show();
    $(
      '.pessoa_' +
        $('#juridica, #fisica')
          .filter(':not(:checked)')
          .attr('id')
    )
      .hide()
      .find('#cpf, #rg, #cnpj, #razao_social, #inscricao_estadual')
      .prop('disable', true)
      .val('');
  }

  exibeContent();

  $('#juridica, #fisica').on('change', function() {
    exibeContent();
  });

  $('.btn-email-ml').on('click', function(event) {
    event.preventDefault();

    $.ajax({
      url: 'index.php?route=module/mercadolivre/mercadolivre/sendEmail&token=' + token + '',
      type: 'get',
      data: 'email_ml=' + $("input[name='email_ml']").val() + '&customer_id=' + costumerId + '',
      dataType: 'json',
      beforeSend: function() {
        $('.btn-email-ml').addClass('btn-loading');
        $('.btn-email-ml').attr('disabled', true);
      },
      complete: function() {
        $('.btn-email-ml').removeClass('btn-loading');
        $('.btn-email-ml').attr('disabled', false);
      },
      success: function(json) {
        $('.error, .success, .warning').remove();
        $('.field-entry-error').removeClass();

        if (json['error']) {
          $.each(json['error'], function(key, value) {
            $("input[name='" + key + "']")
              .addClass('field-entry-error')
              .after('<span class="error">' + value + '</span>');
          });

          if (json['error']['connect']) {
            $('.btn-email-ml').after('<span class="warning">' + json['error']['connect'] + '</span>');
          }

          $('.btn-email-ml')
            .removeClass('btn-primary')
            .addClass('btn-error');
        }

        if (json['success']) {
          $('.btn-email-ml')
            .removeClass('btn-primary btn-error')
            .addClass('btn-success');
          $('.btn-email-ml').after('<span class="success">' + json['success'] + '</span>');
        }
      },
    });
  });

  $('#history .pagination a').live('click', function() {
    $('#history').load(this.href);

    return false;
  });

  $('#history').load('index.php?route=sale/customer/history&token=' + token + '&customer_id=' + costumerId + '');

  $('#button-history').bind('click', function() {
    $.ajax({
      url: 'index.php?route=sale/customer/history&token=' + token + '&customer_id=' + costumerId + '',
      type: 'post',
      dataType: 'html',
      data: 'comment=' + encodeURIComponent($("#tab-history textarea[name='comment']").val()),
      beforeSend: function() {
        $('.success, .warning').remove();
        $('#button-history').attr('disabled', true);
        $('#button-history').addClass('btn-loading');
      },
      complete: function() {
        $('#button-history').attr('disabled', false);
        $('#button-history').removeClass('btn-loading');
        $("#tab-history textarea[name='comment']").val('');
      },
      success: function(html) {
        $('#history').html(html);

        $("#tab-history input[name='comment']").val('');
      },
    });
  });

  $('#transaction .pagination a').live('click', function() {
    $('#transaction').load(this.href);

    return false;
  });

  $('#transaction').load(
    'index.php?route=sale/customer/transaction&token=' + token + '&customer_id=' + costumerId + ''
  );

  $('#button-transaction').bind('click', function() {
    $.ajax({
      url: 'index.php?route=sale/customer/transaction&token=' + token + '&customer_id=' + costumerId + '',
      type: 'post',
      dataType: 'html',
      data:
        'description=' +
        encodeURIComponent($("#tab-transaction input[name='description']").val()) +
        '&amount=' +
        encodeURIComponent($("#tab-transaction input[name='amount']").val()),
      beforeSend: function() {
        $('.success, .warning').remove();
        $('#button-transaction').attr('disabled', true);
        $('#transaction').before(
          '<div class="attention"><img src="../assets/img/loading-black.svg" alt="" /> ' + textWait + '</div>'
        );
      },
      complete: function() {
        $('#button-transaction').attr('disabled', false);
        $('.attention').remove();
      },
      success: function(html) {
        $('#transaction').html(html);

        $("#tab-transaction input[name='amount']").val('');
        $("#tab-transaction input[name='description']").val('');
      },
    });
  });

  $('#reward .pagination a').live('click', function() {
    $('#reward').load(this.href);

    return false;
  });

  $('.js-main-form').on('click', '.js-add-ban-ip', function() {
    var $this = $(this);
    var ip = $this.data('ip');
    console.log(token);
    console.log(ip);

    $.ajax({
      url: 'index.php?route=sale/customer/addbanip&token=' + token + '',
      type: 'post',
      dataType: 'json',
      data: 'ip=' + encodeURIComponent(ip),
      beforeSend: function() {
        $('.success, .warning').remove();

        $('.box').before(
          '<div class="attention"><img src="../assets/img/loading-black.svg" alt="" /> ' + textWait + '</div>'
        );
      },
      complete: function() {},
      success: function(json) {
        console.log(json);

        $('.attention').remove();

        if (json['error']) {
          $('.box').before('<div class="warning" style="display: none;">' + json['error'] + '</div>');

          $('.warning').fadeIn('slow');
        }

        if (json['success']) {
          $('.box').before('<div class="success" style="display: none;">' + json['success'] + '</div>');

          $('.success').fadeIn('slow');

          $this.replaceWith(
            '<button type="button" data-ip="' +
              ip +
              '" class="btn-default btn-outline btn-medium js-remove-ban-ip"><span>' +
              language.get('text_remove_ban_ip') +
              '</span></button>'
          );
        }
      },
    });
  });

  $('.js-main-form').on('click', '.js-remove-ban-ip', function() {
    var $this = $(this);
    var ip = $this.data('ip');
    console.log(token);
    console.log(ip);

    $.ajax({
      url: 'index.php?route=sale/customer/removebanip&token=' + token + '',
      type: 'post',
      dataType: 'json',
      data: 'ip=' + encodeURIComponent(ip),
      beforeSend: function() {
        $('.success, .warning').remove();

        $('.box').before(
          '<div class="attention"><img src="../assets/img/loading-black.svg" alt="" /> ' + textWait + '</div>'
        );
      },
      success: function(json) {
        $('.attention').remove();

        if (json['error']) {
          $('.box').before('<div class="warning" style="display: none;">' + json['error'] + '</div>');

          $('.warning').fadeIn('slow');
        }

        if (json['success']) {
          $('.box').before('<div class="success" style="display: none;">' + json['success'] + '</div>');

          $('.success').fadeIn('slow');

          $this.replaceWith(
            '<button type="button" data-ip="' +
              ip +
              '" class="btn-default btn-outline btn-medium js-add-ban-ip"><span>' +
              language.get('text_add_ban_ip') +
              '</span></button>'
          );
        }
      },
    });
  });

  $('.colorbox').fancybox({
    prevEffect: 'none',
    nextEffect: 'none',
    closeBtn: true,
    nextClick: true,
  });

  $('.htabs a').tabs();
  $('.vtabs a').tabs();
});
