{extends "frame.tpl"}

{block body_class}class="body-menu"{/block}

{block functions append}
    {function name="inversion" field=""}
       <label class="input-group-addon" title="{'inversionInfo'|translate:'filter'}">
          <input class="input" type="checkbox" name="{$field}_invert" value="1">
          <span class="bi bi-dash-circle-fill"></span>
       </label>
    {/function}
{/block}

{block name="title"}
   {translate_type type=$type}
{/block}

{block name="content"}
   <ul class="nav nav-pills nav-stacked">
      {block name="menu-items"}
         <li class="nav-header"><i class="glyphicon {block list_icon}glyphicon-tags{/block}"></i><span>{translate_type type=$type}</span></li>
         <li><a href="javascript:nf('', 'launch.php?s=list.php&amp;type={$type}');"><i class="glyphicon glyphicon-list"></i> <span>{'toolbar_list'|translate}</span></a></li>
         <li><a href="javascript:nw('{$type}', '0');"><i class="glyphicon glyphicon-plus"></i> <span>{'toolbar_add'|translate}</span></a></li>
      {/block}
   </ul>

{/block}

<script type="text/javascript">
   {block name="onready" append}
   // Opening / Closing parts
   function getOpenedPills(name)
   {
      try{
         return JSON.parse(localStorage.getItem("menu-"+name+"-opened")) || {};
      }
      catch (e)
      {
         return {};
      }
   }

   function setOpenedPills(name, index, value)
   {
      var items = getOpenedPills(name);
      if (value)
         items[index] = value;
      else
         delete items[index];

      localStorage.setItem("menu-"+name+"-opened", JSON.stringify(items));
   }

   $(document).on('click', '.opener', function(){
      var index = $(".opener").index(this);
      var $pill = $(".pill-content").eq(index);
      $pill.toggleClass("hidden");

      if (!$pill.is('.opener-no-remember')) {
         setOpenedPills('{$type}', index, !$pill.is('.hidden'));
      }

      return false;
   });

   // Reset btn - chosen
   $('input[type="reset"], button[type="reset"]').click(function() {
       $(".selecter").val('').trigger("chosen:updated");
       setTimeout(() => {
          $(this).closest('form').submit();
       }, 10);
   });


   var opened = getOpenedPills('{$type}');
   if (Object.keys(opened).length > 0)
   {
      $pills = $(".pill-content:not(.always-visible)");
      for (index in opened)
         $pills.eq(index).toggleClass("hidden");
   }

   // Search form
   var $form = $('[data-search=form]');
   if ($form.length)
   {
      function isMassEditActive() {
         const mainSearchParams = new URL($("[name='mainFrame']", window.parent.document).contents().get(0).location.href).searchParams;
         return mainSearchParams.get('s') === 'listEdit.php';
      }

      function submitSingle($item)
      {
         var $always = $form.find('[data-search=always]'),
            $itemInputs = $item.find(':input');

         var queryString = $.merge($always, $itemInputs).serialize();

         if (isMassEditActive()) {
            const searchParams = new URLSearchParams(queryString);
            searchParams.set('s', 'listEdit.php');
            queryString = searchParams.toString();
         }

         const url = 'launch.php?' + queryString;

         nf(null, url);

         return false;
      }

      $form.on('keypress', '[data-search=item] :input', function(e) {
         if (e.key == "Enter" && e.ctrlKey == false && e.shiftKey == false)
         {
            return submitSingle($(e.target).closest('[data-search=item]'));
         }
      }).on('submit', function(e) {
         var $btn = $(e.target).find(':input[type=submit]:focus'),
            $item = $btn.closest('[data-search=item]');
         if ($item.length) {
            return submitSingle($item);
         } else if (isMassEditActive()) {
            $(this).find("[name='s']").val('listEdit.php');
            return true;
         }

      });
   }

   if (window.top.showLeftPane) {
      window.top.showLeftPane(true);
   }
   {/block}
</script>
