
$("#expire").mask("99/9999");
$('#creditcard_ccno').payment('formatCardNumber');
$('#creditcard_cccvd').payment('formatCardCVC');

var dataInfo = $('#cielo_transparente_container').data('info');

$('#creditcard_ccno').blur(function(e) {
  var cardType = $.payment.cardType($(this).val());

  if (cardType) {
    $('.ftr-js-flag-card[value=' + cardType + ']').prop('disabled', false);
    if (cardType == 'amex') {
      $('#creditcard_cccvd').attr("maxlength", 4);
    } else {
      if ($("#creditcard_cccvd").val().length == 4) {
        $("#creditcard_cccvd").val("");
      }
      $('#creditcard_cccvd').attr("maxlength",3);
    }

    $.ajax({
      url: 'index.php?route=payment/cielo_transparente/parcelamento&bandeira=' + cardType + '&parcelas=' + dataInfo.parcelas,
      type: 'GET',
      cache: false,
      dataType: 'html',
      beforeSend: function() {
        $('.ftr-loading-bar').removeClass('ftr-none');
      },
      complete: function() {
        $('.ftr-loading-bar').addClass('ftr-none');
      },
      success: function(data) {
        $('#parcelas').html(data);
      },
      error: function(json) {
        console.log(json);
      }
    });
  } else {
    $(".ftr-js-flag-card:not(:disabled)").prop( 'disabled', true );
  }
});

$('#checkout-confirm').on('submit', function(e) {
  e.preventDefault();
  var
  $this = $(this).find('.ftr-steps__button'),
  mesAno = $("#expire").val().split("/"),
  mes = mesAno[0],
  ano = mesAno[1],
  cardType = $.payment.cardType($('#creditcard_ccno').val());

  if (($.payment.validateCardNumber($('#creditcard_ccno').val())) && ($.payment.validateCardCVC($('#creditcard_cccvd').val(), cardType))&& ($.payment.validateCardExpiry(mes, ano))) {
    $.ajax({
      type: 'POST',
      url: 'index.php?route=payment/cielo_transparente/callback&order_id=' + dataInfo.idboleto,
      data: {
        creditcard_cctype : $(".radioimg-label").find("input:checked").val(),
        creditcard_name : $("#creditcard_name").val(),
        creditcard_ccno : $("#creditcard_ccno").val().replace(/ /g, ""),
        creditcard_cccvd : $("#creditcard_cccvd").val(),
        expire : $("#expire").val(),
        formaPagamento : $("#formaPagamento").val()
      },
      dataType : 'json',
      beforeSend: function() {
        $this.prop('disabled', true);
        $('.ftr-loading-bar').removeClass('ftr-none');
      },
      complete: function() {
        $this.prop('disabled', false);
        $('.ftr-loading-bar').addClass('ftr-none');
      },
      success: function(json) {
        if (!json['error']) {
            //Comentado para que posteriormente seja estudado uma forma de retentativa
            if (json['autorizacao'] == 'retentativa') {
              notificationMsg(dataInfo.text_erro_retentativa, 'warning');
            } else {
              location = dataInfo.continue;
            }
        } else {
          $('.ftr-field').removeClass('error');
          $('.ftr-field__error').remove();
          if (json['error']['bandeira']) {
            $('#box-bandeiras').after('<span class="ftr-field__error">' + json['error']['bandeira'] + '</span>');
          }
          if (json['error']['nomecartao']) {
            $('#creditcard_name').closest('.ftr-field').addClass('error');
            $('#creditcard_name + .ftr-field__text').after('<span class="ftr-field__error">' + json['error']['nomecartao'] + '</span>');
          }
          if (json['error']['numerocartao']) {
            $('#creditcard_ccno').closest('.ftr-field').addClass('error');
            $('#creditcard_ccno + .ftr-field__text').after('<span class="ftr-field__error">' + json['error']['numerocartao'] + '</span>');
          }
          if (json['error']['csv']) {
            $('#creditcard_cccvd').closest('.ftr-field').addClass('error');
            $('#creditcard_cccvd + .ftr-field__text').after('<span class="ftr-field__error">' + json['error']['csv'] + '</span>');
          }
          if (json['error']['validade']) {
            $('#expire').closest('.ftr-field').addClass('error');
            $('#expire + .ftr-field__text').after('<span class="ftr-field__error">' + json['error']['validade'] + '</span>');
          }
        }
      }
    });
  } else {
    $('.ftr-field').removeClass('error');
    $('.ftr-field__error').remove();
    if (!$.payment.validateCardNumber($('#creditcard_ccno').val())) {
      $('#creditcard_ccno').closest('.ftr-field').addClass('error');
      $('#creditcard_ccno + .ftr-field__text').after('<span class="ftr-field__error">Numero de Cartão Inválido</span>');
    }
    if (!$.payment.validateCardCVC($('#creditcard_cccvd').val(), cardType)) {
      $('#creditcard_cccvd').closest('.ftr-field').addClass('error');
      $('#creditcard_cccvd + .ftr-field__text').after('<span class="ftr-field__error">CSV Inválido</span>');
    }
    if (!$.payment.validateCardExpiry(mes, ano)) {
      $('#expire').closest('.ftr-field').addClass('error');
      $('#expire + .ftr-field__text').after('<span class="ftr-field__error">Data de Vencimento Inválida</span>');
    }
  }
});
