var type_payment_phone = $('#type_payment_phone').data('type-payment-phones'),
  Type_PaymentPhone = new window.Type(type_payment_phone),
  Type_CadastroPessoa = new Type($('[data-tipo-pessoa]').data('tipo-pessoa'));

$('.js-checkbox-holder').on('click', function() {
  if ($(this).is(':checked')) {
    $('.js-holder-content').addClass('ftr-hidden');
    $('.js-customer-content').removeClass('ftr-hidden');
  } else {
    $('.js-holder-content').removeClass('ftr-hidden');
    $('.js-customer-content').addClass('ftr-hidden');
  }
});

$('.ftr-js-select-telefone').on('click', function() {
  setMaskPhone($(this).data('value'), $('input[name="numero_telefone"]'));
});

$('.ftr-js-tipo-telefone-customer').on('click', function() {
  setMaskPhone($(this).data('value'), $('input[name="customer_tipo_telefone"]'));
});

function setMaskPhone(value, $inputfone) {
  $inputfone.removeClass('mask-celular, mask-phone');

  switch (parseInt(value)) {
    case Type_PaymentPhone.Tp_Celular:
      $inputfone.addClass('mask-celular');
      break;
    case Type_PaymentPhone.Tp_Telefone:
    case Type_PaymentPhone.Tp_Fax:
      $inputfone.addClass('mask-phone');
      break;
    default:
      $inputfone.addClass('mask-celular');
  }

  window.ftrMask();
}

setMaskPhone($('.ftr-js-telefone').val(), $('input[name="numero_telefone"]'));

setMaskPhone($('.ftr-js-tipo-telefone-customer').val(), $('input[name="customer_tipo_telefone"]'));
function setTypePerson(value) {
  let labelData = document.querySelector('.js-label-fisica-juridica');
  if (value == Type_CadastroPessoa.Tp_Fisico) {
    $('.js-content-fisica').removeClass('ftr-hidden');
    $('.js-content-juridica').addClass('ftr-hidden');

    if(labelData){
      labelData.textContent = language.getText('payment_address', 'text_date');
    }
  } else {
    $('.js-content-juridica').removeClass('ftr-hidden');
    $('.js-content-fisica').addClass('ftr-hidden');

    if(labelData){
      labelData.textContent = language.getText('payment_address','text_date_company');
    }
  }
}

var pessoa = $('.js-holder-content input[name="pessoa"]');
pessoa.on('click', function() {
  setTypePerson($(this).val());
});

setTypePerson(pessoa.filter(':checked').val());

$('#form-yapay, #form-wirecard').validate({
  onfocusout: function(element, event) {
    $(event.currentTarget)
      .next('.form-error')
      .remove();
    $(event.currentTarget).valid();
  },
  onsubmit: true,
  errorElement: 'span',
  errorClass: 'form-error',
  validClass: 'form-success',
  rules: {
    celular: {
      celular: true,
    },
    cpf: {
      cpfBR: true,
    },
    cnpj: {
      cnpj: true,
    },
    postcode: {
      postalcodeBR: true,
    },
    email: {
      email: true,
    },
  },
  messages: {
    confirm: {
      equalTo: 'As senhas não conferem',
    },
  },
});
