<?php
global $gdb;

$menus = is_array($this->menus ?? null) ? $this->menus : [];
$modal = !empty($this->modal);
$cookiePages = is_array($this->cookie_pages ?? null) ? $this->cookie_pages : [];
$cookieMenus = is_array($this->cookie_menus ?? null) ? $this->cookie_menus : [];

ob_start();
foreach ($menus as $menu):
    $menuId = (int)($menu['id'] ?? 0);
    $menuName = (string)($menu['name'] ?? '');
    $menuStatus = (int)($menu['status'] ?? 0);
?>
  <div class="menu_sortable">
    <ol class="sortable" id="sortable_<?= $menuId; ?>" menu-id="<?= $menuId; ?>">
      <li id="list_<?= $menuId; ?>" class="menu_<?= $menuId; ?>" menu="<?= $menuId; ?>">
        <div class="context_0 content content_menu">
          <span class="disclose"><span></span></span>
          <?php if ($menuStatus !== 1): ?><i class="fa fa-ban fa-lg text-danger" title="Menu nieaktywne"></i><?php endif; ?>
          Menu: <b><?= htmlspecialchars($menuName, ENT_QUOTES, 'UTF-8'); ?></b>
        </div>
        <?= \Shared\Tpl\Tpl::view('pages/subpages-browse-list', [
          'pages' => $menu['pages'] ?? [],
          'step' => 1,
        ]); ?>
      </li>
    </ol>
  </div>
<?php
endforeach;
$out = ob_get_clean();

$gridId = 'pages-list';
$gridTitle = 'Lista stron';
$gridSaveUrl = '';
$gridBackUrl = '';
$gridHidden = [];
$gridContent = $out;
$gridPersist = false;
include __DIR__ . '/../components/grid-edit-replacement.php';
?>
<style type="text/css">
  <?php if ($modal): ?>.mfp-container<?php endif; ?> body.sb-top.sb-top-sm .navbar.navbar-fixed-top + #sidebar_left + #content_wrapper {
    padding-top: 0;
  }
  <?php if ($modal): ?>.mfp-container<?php endif; ?> #content {
    padding: 27px 0 0;
  }
  <?php if ($modal): ?>.mfp-container<?php endif; ?> #content > .row {
    margin: 0;
  }
  <?php if ($modal): ?>.mfp-container<?php endif; ?> #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">
  var cookiePages = <?= json_encode($cookiePages, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;
  var cookieMenus = <?= json_encode($cookieMenus, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;

  function getUrlParam(paramName) {
    var reParam = new RegExp('(?:[\\?&]|&amp;)' + paramName + '=([^&]+)', 'i');
    var match = window.location.search.match(reParam);

    return (match && match.length > 1) ? match[1] : '';
  }

  $(document).ready(function() {
    <?php if (!$modal): ?>
    $('#sidebar_left, .navbar-fixed-top').hide();
    <?php endif; ?>

    var funcNum = getUrlParam('CKEditorFuncNum');

    $('body').on('click', '.button', function() {
      if (funcNum) {
        window.opener.CKEDITOR.tools.callFunction(funcNum, $(this).attr('url'));
        window.close();
      }
    });

    $('ol.sortable').nestedSortable({
      forcePlaceholderSize: true,
      handle: 'div',
      helper: 'clone',
      items: 'li',
      opacity: 0.9,
      placeholder: 'placeholder',
      revert: 250,
      tabSize: 45,
      tolerance: 'pointer',
      toleranceElement: '> div',
      maxLevels: 3,
      isTree: true,
      expandOnHover: 700,
      isAllowed: function() {
        return false;
      }
    });

    $('.disclose').on('click', function() {
      $(this).closest('li').toggleClass('sort-collapsed').toggleClass('sort-expanded');
    });

    $('.disclose').mousedown(function(e) {
      if (e.which !== 1) {
        return;
      }

      if ($(this).parent('div').hasClass('content_menu')) {
        var menuId = $(this).parent('div').parent('li').attr('menu');
        $.ajax({
          type: 'POST',
          cache: false,
          url: '/admin/pages/cookieMenus/',
          data: { menu_id: menuId }
        });
      } else {
        var pageId = $(this).parent('div').parent('li').attr('id').replace('list_', '');
        $.ajax({
          type: 'POST',
          cache: false,
          url: '/admin/pages/cookiePages/',
          data: { page_id: pageId }
        });
      }
    });

    Object.keys(cookiePages || {}).forEach(function(key) {
      if (String(cookiePages[key]) === '1') {
        $('.list_' + key).children('div').children('span.disclose').click();
      }
    });

    Object.keys(cookieMenus || {}).forEach(function(key) {
      if (String(cookieMenus[key]) === '1') {
        $('.menu_' + key).children('div').children('span.disclose').click();
      }
    });
  });
</script>

