
$.extend($.validator.messages, {
  required: "Campo requerido.",
  email: "Formato de email inválido.",
  customDate: "Ingrese la fecha en formato dd/mm/yyyy.",
  equalTo: "Las contraseñas no coinciden.",
  remote: "Email ya registrado en el sitio.",
  minlength: "Cantidad mínima de caracteres: 6"
});

$('[name="company_area"]').on('change', function(){
  let current = $(this).val();
  if(current == 'otros'){
    $('.other-area-container').show();
  }else{
    $('.other-area-container').hide();
  }
});

let form_subscription = $('#form-subscription');

if( form_subscription.length ){

  /*Form Validation*/
  form_subscription.validate({
    rules:{
      "first_name": {
        required: true
      },
      "last_name": {
        required: true
      },
      "profession": {
        required: true
      },
      "company": {
        required: true
      },
      "position": {
        required: true
      },
      "phone": {
        required: true
      },
      "email": {
        required: true,
        email: true
      },
      "password": {
        required: true,
        minlength : 6
      },
      "repeat_password": {
        required: true,
        equalTo : "[name='password']"
      },
      "address": {
        required: true
      },
      "postal_code": {
        required: true
      },
      "city": {
        required: true
      },
      "region": {
        required: true
      },
      "country": {
        required: true
      },

    },
    errorClass: 'error-field',
    errorElement: 'p',
    errorPlacement: function( label, element ) {
      label.insertAfter( element );
    },
    ignore: ":hidden",
    submitHandler: function(form) {
      let suscription_data = getFormData(form);
      handleSuscription(form, suscription_data);
    }
  });

}

function handleSuscription(form, suscription_data){
  let data = handle_subscription_controller_object;

  data['user_data'] = suscription_data;

  $.ajax({
    type: 'POST',
    dataType: 'json',
    url: handle_subscription_controller_object.url,
    data: data,
    beforeSend: function(){
      $(form).find('.form-message').hide();
      $(form).find('.sending-message').fadeIn();
    },
    success: function(data){
      $(form).find('.form-message').hide();
      if( data.hasOwnProperty('success') ){
        $(form).find('.success-message').fadeIn();
        if( data.hasOwnProperty('redirect') ){
          window.location.href = data.redirect;
        }
      }
      if( data.hasOwnProperty('error') ){
        $(form).find('.error-message').find('p').html(data.message);
        $(form).find('.error-message').fadeIn();
      }
    },
    error: function (request, status, error) {
      console.log(status);
      console.log(error);
      console.log(request.responseText);
    }
  });
}


function getFormData(form){
  let fields = $(form).find('.data-field');
  let data = {};
  fields.each( function(){
    let name = $(this).attr('name');
    let value = $(this).val();
    let valid = true;

    if( $(this).is(':checkbox') ){
      if( $(this).hasClass('true-false') ){
        if( $(this).is(':checked') ){
          value = true;
        }else{
          value = false;
        }
      }
    }

    if( $(this).is(':radio') ){
      valid = false;
      if( $(this).is(':checked') ){
        valid = true;
        value = $(this).val();
        if($(this).hasClass('is-boolean')){
          value = (value === "1");
        }
      }
    }
    if(valid){
      data[name] = value;
    }
  });

  return data;
}
