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

  $.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="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,
                firstname: item.firstname,
                lastname: item.lastname,
                email: item.email,
                telephone: item.telephone,
              };
            })
          );
        },
      });
    },
    select: function(event, ui) {
      $('input[name="customer"]').attr('value', ui.item.label);
      $('input[name="customer_id"]').attr('value', ui.item.value);
      $('input[name="firstname"]').attr('value', ui.item.firstname);
      $('input[name="lastname"]').attr('value', ui.item.lastname);
      $('input[name="email"]').attr('value', ui.item.email);
      $('input[name="telephone"]').attr('value', ui.item.telephone);

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

  $('input[name="product"]').autocomplete({
    delay: 500,
    source: function(request, response) {
      $.ajax({
        url:
          'index.php?route=catalog/product/autocomplete&token=' +
          token +
          '&filter_name=' +
          encodeURIComponent(request.term),
        dataType: 'json',
        success: function(json) {
          response(
            $.map(json, function(item) {
              return {
                label: item.name,
                value: item.product_id,
                model: item.model,
              };
            })
          );
        },
      });
    },
    select: function(event, ui) {
      $('input[name="product_id"]').attr('value', ui.item.value);
      $('input[name="product"]').attr('value', ui.item.label);
      $('input[name="model"]').attr('value', ui.item.model);

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

  $(document).ready(function() {
    $('#date').datepicker({
      /* 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',
        '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',
    });
  });
});
