<?php
$pages = is_array($this->pages ?? null) ? $this->pages : [];
$step = (int)($this->step ?? 1);

if (empty($pages)) {
    return;
}
?>
<ol>
  <?php foreach ($pages as $page): ?>
    <?php
      $pageId = (int)($page['id'] ?? 0);
      $menuId = (int)($page['menu_id'] ?? 0);
      $title = (string)($page['title'] ?? '');
      $status = (int)($page['status'] ?? 0);
      $start = (int)($page['start'] ?? 0);
      $languages = is_array($page['languages'] ?? null) ? $page['languages'] : [];
    ?>
    <li id="list_<?= $pageId; ?>" class="list_<?= $pageId; ?>" menu="<?= $menuId; ?>">
      <div class="content">
        <div class="menu-box-title">
          <button type="button" class="disclose layout-tree-toggle" aria-expanded="false" title="Rozwin / zwin">
            <i class="fa fa-caret-right"></i>
          </button>
          <?php if ($status !== 1): ?><i class="fa fa-ban fa-lg text-danger" title="Strona nieaktywna"></i><?php endif; ?>
          <?php if ($start === 1): ?><i class="fa fa-star fa-lg text-system" title="Strona startowa"></i><?php endif; ?>
          <?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8'); ?>

          <?php if (!empty($languages)): ?>
            <div class="btn-group flags" style="display: inline-block;">
              <?php foreach ($languages as $language): ?>
                <?php $langId = (string)($language['lang_id'] ?? ''); ?>
                <?php if ($langId !== ''): ?>
                  <img src="/admin/layout/lang-<?= htmlspecialchars($langId, ENT_QUOTES, 'UTF-8'); ?>.jpg" />
                <?php endif; ?>
              <?php endforeach; ?>
            </div>
          <?php endif; ?>
        </div>

        <?php if ($step < 3): ?>
          <div class="btn-group ml20 pull-right">
            <a href="/admin/pages/edit/id=<?= $pageId; ?>&menu_id=<?= $menuId; ?>" title="Edytuj stron&#281;" class="btn btn-sm btn-system">
              <i class="fa fa-file-text"></i> <span class="hidden-xs">Edytuj stron&#281;</span>
            </a>
            <a href="#" title="Usu&#324; stron&#281;" class="btn btn-sm btn-danger page-delete" page-id="<?= $pageId; ?>">
              <i class="fa fa-trash"></i> <span class="hidden-xs">Usu&#324; stron&#281;</span>
            </a>
            <a href="/admin/pages/pageArticles/id=<?= $pageId; ?>" title="Lista artyku&#322;&#243;w" class="btn btn-sm btn-info" page-id="<?= $pageId; ?>">
              <i class="fa fa-bars"></i> <span class="hidden-xs">Lista artyku&#322;&#243;w</span>
            </a>
            <a href="/admin/pages/edit/pid=<?= $pageId; ?>&menu_id=<?= $menuId; ?>" title="Dodaj podstron&#281;" class="btn btn-sm btn-success">
              <i class="fa fa-plus-circle"></i> <span class="hidden-xs">Dodaj podstron&#281;</span>
            </a>
          </div>
        <?php else: ?>
          <div class="btn-group ml20 pull-right">
            <a href="/admin/pages/edit/id=<?= $pageId; ?>&menu_id=<?= $menuId; ?>" title="Edytuj stron&#281;" class="btn btn-sm btn-system">
              <i class="fa fa-file-text"></i> <span class="hidden-xs">Edytuj stron&#281;</span>
            </a>
            <a href="#" title="Usu&#324; stron&#281;" class="btn btn-sm btn-danger page-delete" page-id="<?= $pageId; ?>">
              <i class="fa fa-trash"></i> <span class="hidden-xs">Usu&#324; stron&#281;</span>
            </a>
            <a href="/admin/pages/pageArticles/id=<?= $pageId; ?>" title="Lista artyku&#322;&#243;w" class="btn btn-sm btn-info" page-id="<?= $pageId; ?>">
              <i class="fa fa-bars"></i> <span class="hidden-xs">Lista artyku&#322;&#243;w</span>
            </a>
          </div>
        <?php endif; ?>
      </div>
      <?= \Shared\Tpl\Tpl::view('pages/subpages-list', [
        'pages' => $page['subpages'] ?? [],
        'step' => $step + 1,
      ]); ?>
    </li>
  <?php endforeach; ?>
</ol>

