<?
global $gdb;
ob_start();
  ?>
    <div class="menu_sortable">
      <ol class="sortable" id="sortable">
        <? if ( is_array( $this -> categories ) ): foreach ( $this -> categories as $category ):?>
          <li id="list_<?= $category['id'];?>" class="category_<?= $category['id'];?>" category="<?= $category['id'];?>">
            <div class="context_0 content content_menu">
              <span class="disclose"><span></span></span>
              <? if ( !$category['status'] ) echo '<i class="fa fa-ban fa-lg text-danger" title="Kategoria nieaktywna"></i>';?> 
              <b><?= $category['languages'][$this -> dlang]['title'];?></b>              
              <div class="btn-group ml20 pull-right">
                <a href="#" title="Wybierz kategorię" class="btn btn-sm btn-system button category-select" category-title="<?= $category['languages'][$this -> dlang]['title'];?>" category-id="<?= $category['id'];?>">
                  <i class="fa fa-check"></i> <span class="hidden-xs">wybierz</span>
                </a>
              </div>
            </div>
            <?= \Tpl::view( 'shop-category/subcategory-browse-list', [
                    'categories' => \admin\factory\ShopCategory::subcategories( $category['id'] ),
                    'level' => $this -> level + 1,
                    'dlang' => $this -> dlang
                  ] );?>
          </li>
        <? endforeach; endif;?>
      </ol>
    </div>
  <?
$out = ob_get_clean();

$grid = new \gridEdit;
$grid -> id               = 'pages-list';
$grid -> gdb_opt          = $gdb;
$grid -> include_plugins  = false;
$grid -> title            = 'Lista kategorii';
$grid -> default_buttons  = false;
$grid -> external_code    = $out;

echo $grid -> draw();
?>
<style type="text/css">
  .mfp-container body.sb-top.sb-top-sm .navbar.navbar-fixed-top + #sidebar_left + #content_wrapper {
    padding-top: 0;
  }
  .mfp-container #content {
    padding: 27px 0 0;
  }
  .mfp-container #content > .row {
    margin: 0;
  }
  .mfp-container #content > .row > .col-lg-9 {
    padding: 0;
    width: 100%;
    float: none;
  }
</style>
<script type="text/javascript" src="/libraries/jquery-nested-sortable/jquery.mjs.nestedSortable.js"></script>
<script type="text/javascript">
  $( document ).ready( function() {
    $( 'ol.sortable' ).nestedSortable({
			forcePlaceholderSize: true,
			handle: 'div',
			helper:	'clone',
			items: 'li',
			opacity: .9,
			placeholder: 'placeholder',
			revert: 250,
			tabSize: 45,
			tolerance: 'pointer',
			toleranceElement: '> div',
			maxLevels: 4,
			isTree: true,
			expandOnHover: 700,
      protectRoot: false
		});

		$( '.disclose' ).on( 'click', function() {
			$( this ).closest( 'li' ).toggleClass( 'sort-collapsed' ).toggleClass( 'sort-expanded' );
		});
            
    $( '.disclose' ).mousedown( function(e) {
      if ( e.which === 1 ) {
        var category_id = $( this ).parent( 'div' ).parent( 'li' ).attr( 'id' );

        $.ajax({
          type: 'POST',
          cache: false,
          url: '/admin/ajax.php',
          data: {
            a: 'cookie_categories',
            category_id:  category_id
          }
        });
      }
    });
    
    <?php
      $array = unserialize( $_COOKIE[ 'cookie_categories' ] );
      if ( is_array( $array ) ): foreach ( $array as $key => $val ):
        if ( $val ):
          ?>$( '#<?= $key;?>' ).children( 'div' ).children( 'span.disclose' ).click();<?
        endif;
      endforeach; endif;
    ?>
  });
</script>