$(document).ready(function() {
  var dataSearchs = $('.js-search'),
    dataSearchsContent = $('.js-search-content');

  $('body').on('click', '.js-tab', function() {
    $('.js-filter-module').val('');
    $('.js-container-module').removeClass('hidden');
  });

  $('.js-filter-module').on('input', function() {
    var value = trocaCaracter(
      $(this)
        .val()
        .trim()
        .toLowerCase()
    );

    $('.js-tab.active').removeClass('active');

    if (value.length === 0) {
      var hash = window.location.hash ? window.location.hash : '#tab-info=0';
      var hashNumber = hash.split('=')[1];

      $('.js-search-content').addClass('hidden');
      $('.js-container-module').removeClass('hidden');
      $('.js-tab[data-tab-control=tab-info-' + hashNumber + ']').addClass('active');
      $('.js-search-content[data-tab=tab-info-' + hashNumber + ']').removeClass('hidden');
      return;
    }

    dataSearchs.toArray().map(function(search) {
      if (
        $(search)
          .data('search')
          .toLowerCase()
          .indexOf(value) > -1
      ) {
        $(search).removeClass('hidden');
      } else {
        $(search).addClass('hidden');
      }
    });

    dataSearchsContent.toArray().map(function(search) {
      if (
        $(search)
          .find('.js-search')
          .filter(':not(.hidden)').length <= 0
      ) {
        $(search).addClass('hidden');
      } else {
        $(search).removeClass('hidden');
      }
    });
  });

  $('.js-addRemove').on('click', function(event) {
    event.preventDefault();

    var $this = $(this),
      url = $this.data('href');

    if (url.indexOf('uninstall') >= 0) {
      var confirmContinue = confirm('Quer mesmo excluir este módulo?');
      if (!confirmContinue) {
        return;
      }
    }

    $this.find('i').attr('class', 'fas fa-sync-alt fa-spin');

    $.ajax({
      url: url,
      type: 'get',
      dataType: 'json',
    })
      .done(function(json) {
        if (json['error']) {
          $('.js-contant-message').before(
            '<div class="text-small text-error background-error padding-medium rounded-x-small full-width"><span>' +
              json['error'] +
              '</span></div>'
          );
        } else {
          var container = $this.prev();

          if (url.indexOf('uninstall') >= 0) {
            $this.data('href', url.replace('uninstall', 'install'));
            $this.attr('data-tooltip', 'Desinstalado!');
            $this.find('i').attr('class', 'fas fa-check-circle');
            setTimeout(function() {
              $this.attr('data-tooltip', 'Instalar');
              $this.find('i').attr('class', 'fas fa-plus');
            }, 3000);

            container.replaceWith(
              $(
                '<div data-href="' +
                  container.attr('href') +
                  '" class="flex nowrap-xs middle-xs text-decoration-none" style="opacity:.32;">' +
                  container.html() +
                  '</div>'
              )
            );
          } else {
            $this.data('href', url.replace('install', 'uninstall'));
            $this.attr('data-tooltip', 'Instalado!');
            $this.find('i').attr('class', 'fas fa-check-circle');
            setTimeout(function() {
              $this.attr('data-tooltip', 'Desinstalar');
              $this.find('i').attr('class', 'fas fa-trash');
            }, 3000);

            container.replaceWith(
              $(
                '<a href="' +
                  container.data('href') +
                  '" class="flex nowrap-xs middle-xs text-decoration-none">' +
                  container.html() +
                  '</a>'
              )
            );
          }
        }
      })
      .fail(function(json) {
        console.log(json);
      });
  });
});
