$(document).ready(function() {
  var token = $('body').data('token');

  $('.js-btn-filter').on('click', function() {
    var url = 'index.php?route=sale/return&token=' + token;

    var filter_return_id = $('input[name="filter_return_id"]').attr('value');

    if (filter_return_id) {
      url += '&filter_return_id=' + encodeURIComponent(filter_return_id);
    }

    var filter_order_id = $('input[name="filter_order_id"]').attr('value');

    if (filter_order_id) {
      url += '&filter_order_id=' + encodeURIComponent(filter_order_id);
    }

    var filter_customer = $('input[name="filter_customer"]').attr('value');

    if (filter_customer) {
      url += '&filter_customer=' + encodeURIComponent(filter_customer);
    }

    var filter_product = $('input[name="filter_product"]').attr('value');

    if (filter_product) {
      url += '&filter_product=' + encodeURIComponent(filter_product);
    }

    var filter_model = $('input[name="filter_model"]').attr('value');

    if (filter_model) {
      url += '&filter_model=' + encodeURIComponent(filter_model);
    }

    var filter_return_status_id = $('select[name="filter_return_status_id"]').attr('value');

    if (filter_return_status_id != '*') {
      url += '&filter_return_status_id=' + encodeURIComponent(filter_return_status_id);
    }

    var filter_date_added = $('input[name="filter_date_added"]').attr('value');

    if (filter_date_added) {
      url += '&filter_date_added=' + encodeURIComponent(filter_date_added);
    }

    var filter_date_modified = $('input[name="filter_date_modified"]').attr('value');

    if (filter_date_modified) {
      url += '&filter_date_modified=' + encodeURIComponent(filter_date_modified);
    }

    location = url;
  });

  $.widget('custom.catcomplete', $.ui.autocomplete, {
    _renderMenu: function(ul, items) {
      var self = this,
        currentCategory = '';

      $.each(items, function(index, item) {
        if (item.category != currentCategory) {
          ul.append('<li class="ui-autocomplete-category">' + item.category + '</li>');

          currentCategory = item.category;
        }

        self._renderItem(ul, item);
      });
    },
  });

  $("input[name='filter_customer']").catcomplete({
    delay: 500,
    source: function(request, response) {
      $.ajax({
        url:
          'index.php?route=sale/customer/autocomplete&token=' +
          token +
          '&filter_name=' +
          encodeURIComponent(request.term),
        dataType: 'json',
        success: function(json) {
          response(
            $.map(json, function(item) {
              return {
                category: item.customer_group,
                label: item.name,
                value: item.customer_id,
              };
            })
          );
        },
      });
    },
    select: function(event, ui) {
      $("input[name='filter_customer']").val(ui.item.label);

      return false;
    },
    focus: function() {
      return false;
    },
  });

  $(document).ready(function() {
    $('.date').datepicker({
      changeMonth: true,
      changeYear: true,

      /* COMECO - Tradução DataPicker - por Henrique R. Pohl */

      closeText: 'Fechar',
      prevText: '&#x3c;Anterior',
      nextText: 'Pr&oacute;ximo&#x3e;',
      currentText: 'Hoje',
      monthNames: [
        'Janeiro',
        'Fevereiro',
        'Mar&ccedil;o',
        'Abril',
        'Maio',
        'Junho',
        'Julho',
        'Agosto',
        'Setembro',
        'Outubro',
        'Novembro',
        'Dezembro',
      ],
      monthNamesShort: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
      dayNames: [
        'Domingo',
        'Segunda-feira',
        'Ter&ccedil;a-feira',
        'Quarta-feira',
        'Quinta-feira',
        'Sexta-feira',
        'Sabado',
      ],
      dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
      dayNamesMin: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
      weekHeader: 'Sm',
      firstDay: 0,
      isRTL: false,
      showMonthAfterYear: false,
      yearSuffix: '',

      /* FIM - Tradução DataPicker - por Henrique R. Pohl */

      dateFormat: 'dd/mm/yy',
      onSelect: function() {
        $('#validade').datepicker({
          changeMonth: true,
          changeYear: true,

          /* COMECO - Tradução DataPicker - por Henrique R. Pohl */

          closeText: 'Fechar',
          prevText: '&#x3c;Anterior',
          nextText: 'Pr&oacute;ximo&#x3e;',
          currentText: 'Hoje',
          monthNames: [
            'Janeiro',
            'Fevereiro',
            'Mar&ccedil;o',
            'Abril',
            'Maio',
            'Junho',
            'Julho',
            'Agosto',
            'Setembro',
            'Outubro',
            'Novembro',
            'Dezembro',
          ],
          monthNamesShort: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
          dayNames: [
            'Domingo',
            'Segunda-feira',
            'Ter&ccedil;a-feira',
            'Quarta-feira',
            'Quinta-feira',
            'Sexta-feira',
            'Sabado',
          ],
          dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
          dayNamesMin: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
          weekHeader: 'Sm',
          firstDay: 0,
          isRTL: false,
          showMonthAfterYear: false,
          yearSuffix: '',

          /* FIM - Tradução DataPicker - por Henrique R. Pohl */

          dateFormat: 'dd/mm/yy',
        });
      },
    });
  });
});
