$(document).ready(function() {

  var token = $('body').data('token');

  displayStatusStarted(window.processo_venda_status);

  $('.js-process-status').change(function() {
    displayStatusStarted($(this).val());
  });

  $('.js-submit-proccess-form').on('click', function() {
    var $this = $('.js-submit-proccess-form');

    $('.error, .success, .warning, .error-proccess').remove();
    $('.field-entry-error').removeClass('field-entry-error');
    $('.js-field-entry-group select').removeClass('background-error border-error');

    $.ajax({
      url: $('.js-proccess-form').attr('action'),
      type: 'POST',
      data: $('.js-proccess-form').serializeObject(),
      dataType: 'json',
      beforeSend: function() {
        $this.addClass('btn-loading');
        $this.attr('disabled', true);
      },
      complete: function() {
        $this.removeClass('btn-loading');
        $this.attr('disabled', false);
      },
      success: function(json) {

        $('.js-notify').remove();
        $('.text-error').remove();
        if (json.error) {
          if (json.error.warning) {
            $('.js-warning').before(
              '<div class="text-small full-width background-error text-error padding-medium bottom-margin-medium rounded-x-small">' +
              json.error.warning +
              '</div>'
            );
            delete json.error.warning;
          }

          if (json.error.proccess_count) {
            $.each(json.error.proccess_count, function(key, value) {
              var element = $('#' + key).next();
              element.removeClass('hidden');
              element
                .find('[data-tipo-status=' + value + ']')
                .after(
                  '<div class="text-small error-proccess full-width background-error text-error padding-small bottom-margin-medium rounded-x-small js-notify">' +
                  json['error']['proccess_count_msg'] +
                  '</div>'
                );
            });
            delete json.error.proccess_count;
          }

          $.each(json['error'], function(key, value) {
            if (key == 'proccess_status_started' || key == 'proccess_status_venda') {
              var processKey = key;
              $.each(value, function(key, value) {
                var element = $('#' + key).next();
                element.removeClass('hidden');
                $.each(value, function(column, input) {
                  var columnStatus = element.find('[data-tipo-status=' + column + ']');
                  columnStatus.after(
                    '<div class="text-small error-proccess full-width background-error text-error padding-small bottom-margin-medium rounded-x-small js-notify">' +
                    json['error'][processKey + '_msg'] +
                    '</div>'
                  );
                  $.each(input, function(index) {
                    var select = columnStatus
                      .parent()
                      .find('.field-group .js-field-entry-group')
                      .eq(index)
                      .find('select');
                    select.addClass('background-error border-error');
                  });
                });
              });
            } else {
              $('[name="' + key + '"]')
                .addClass('field-entry-error')
                .after('<span class="text-x-small text-error">' + value + '</span>');
            }
          });
        }

        if (json['success']) {
          $('.js-warning').before(
            '<div class="text-small full-width background-success text-success padding-medium bottom-margin-medium rounded-x-small js-notify">' +
            json['success'] +
            '</div>'
          );
        }
      },
      error: function(error) {
        console.log(error.responseText);
      },
    });
  });


});

function displayStatusStarted(value) {
    if (value == 1) {
    $('.js-status-started').removeClass('hidden');
    $('select[name="processo_venda_order_status_started"]').attr('disabled', false);
  } else {
    $('.js-status-started').addClass('hidden');
    $('select[name="processo_venda_order_status_started"]').attr('disabled', true);
  }
}