<script>
  "use strict";
  (function($, $module){

    var i18n = {
      category: "{intl l="Category"}",
      product: "{intl l="Product"}",
      brand: "{intl l="Brand"}",
      folder: "{intl l="Folder"}",
      content: "{intl l="content"}",
      modalTitleReassign: "{intl l="Reassign the url" d="rewriteurl.bo.default"}",
      modalTitleReassignAll: "{intl l="Reassign all urls" d="rewriteurl.bo.default"}"
    };

    $module.find('.js-popover').popover();


    /*****************************************/
    /*********** Action reassign *************/
    /*****************************************/
    var $modalSearch = $module.find('.js-modal-search');
    var $modalSearchLoader = $modalSearch.find('.js-loader');

    $module.on('click', '.js-action-reassign', function(event){
      event.preventDefault();
      $modalSearch.find('.js-input-url-id').val($(this).data('id'));
      $modalSearch.find('.js-target-url').text('/' + $(this).data('url'));
      $modalSearch.find('.js-input-all').val(0);
      $modalSearch.find('.modal-header h3').text(i18n.modalTitleReassign);
      $modalSearch.find('.js-info-redirected').show();
      $modalSearch.modal('show');
    });

    $modalSearch.on('submit', '.js-form-search', function(event){
      event.preventDefault();
    });

    var searchTimer = null;
    $modalSearch.on('keyup' ,'.js-input-search', function(){
      var val = $(this).val();

      if (searchTimer !== null) {
        clearTimeout(searchTimer)
      }

      $modalSearch.find('.js-alert-no-result').addClass('hide');

      if (val.length > 3) {
        $modalSearchLoader.removeClass('hide');

        searchTimer = setTimeout(function() {
          $.ajax({
            url: '{url path="/admin/module/rewriteurl/search"}',
            data: {
              q: val
            },
            dataType: 'json',
            method: 'GET',
            success: function(data) {
              $modalSearchLoader.addClass('hide');
              var options = [];
              for (var type in data) {
                options.push('<optgroup label="' + i18n[type] + '">');
                for (var id in data[type]) {
                  options.push('<option value="' + id + '::' + type +'">' + data[type][id] + '</option>');
                }
                options.push('</optgroup>');
              }

              if (options.length > 0) {
                $modalSearch.find(".js-block-search-result select").html(options.join(''));
                $modalSearch.find('.js-block-search-result').removeClass('hide');
              } else {
                this.error();
              }
            },
            error: function() {
              $modalSearchLoader.addClass('hide');

              $modalSearch.find('.js-block-search-result').addClass('hide');
              $modalSearch.find('.js-alert-no-result').removeClass('hide');
            }
          });
        }, 350, this);
      } else {
        $modalSearchLoader.addClass('hide');
        $modalSearch.find('.js-block-search-result').addClass('hide');
      }
    });

    $modalSearch.on('change' ,".js-block-search-result select", function(){
      $modalSearch.find('.js-input-reassign').val($(this).val());
    });

    /*****************************************/
    /************ Action delete **************/
    /*****************************************/

    $module.on('click', '.js-action-delete', function(){
      $module.find('.js-input-delete').val($(this).data('id'));
      $module.find('.js-target-url').text('/' + $(this).data('url'));
    });

    /*****************************************/
    /************ Action set to default **************/
    /*****************************************/

    $module.on('click', '.js-action-default', function(){
      $module.find('.js-input-default').val($(this).data('id'));
      $module.find('.js-target-url').text('/' + $(this).data('url'));
    });

    /*****************************************/
    /************ Action add *************/
    /*****************************************/

    $module.on('click', '.js-action-add', function(event){
      event.preventDefault();
      $module.find('.js-alert-error').addClass('hide');

      var val = $module.find('.js-input-url-add').val();
      if (val !== "") {
        $.get('{url path="/admin/module/rewriteurl/exist"}', {
          q : val
        }, function(data) {
          if (data == false){
            $module.find('.js-form-add').submit();
          } else {
            var reassignLink = "<a href='"+data.reassignUrl+"&current_tab=modules'>{intl l='link'}</a>";
            $module.find('.js-alert-error')
                    .removeClass('hide')
                    .html("{intl l='Error this url already exist you can reassign by follow this '}"+reassignLink);
          }
        });
      }
    });

    /*****************************************/
    /******** Action redirect all ************/
    /*****************************************/

    $module.on('click', '.js-action-redirect-all', function(event) {
      event.preventDefault();
      $modalSearch.find('.js-input-url-id').val($(this).data('id'));
      $modalSearch.find('.js-input-all').val(1);
      $modalSearch.find('.modal-header h3').text(i18n.modalTitleReassignAll);
      $modalSearch.find('.js-info-redirected').hide();
      $modalSearch.modal('show');
    });

    $module.on('mouseover', '.js-action-view-redirected', function(event){
      $module.find('tr[data-id="' + $(this).text() + '"]').addClass('info');
      $(this).addClass('info');
    });
    $module.on('mouseleave', '.js-action-view-redirected', function(event){
      $module.find('tr[data-id="' + $(this).text() + '"]').removeClass('info');
    });


      /******************************************************************************/
      /*********** Action "Change redirect type" (Temporary or permanent) *************/
      /******************************************************************************/

      var $loader = $('<div class="loader"></div>');
      $('body').append($loader);


      $module.find('.js-select-redirect-type').on("change",function () {
          $loader.show();
          $.ajax({
              url: '{url path="/admin/module/rewriteurl/change-redirect-type"}',
              data: {
                  id_url: $(this).data('idurl'),
                  httpcode: $(this).val()
              },
              dataType: 'json',
              method: 'POST',
              success: function(){
                  $loader.hide();
              },
              error: function(){
                  $loader.hide();
              }
          });
      });

  }(jQuery, jQuery("#module-RewriteUrl")));
</script>