<?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'] : [];
      $previewUrls = is_array($page['preview_urls'] ?? null) ? $page['preview_urls'] : [];
    ?>
    <li id="list_<?= $pageId; ?>" class="list_<?= $pageId; ?>" menu="<?= $menuId; ?>">
      <div class="content">
        <span class="disclose"><span></span></span>
        <?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; ?>

        <?php foreach ($languages as $language): ?>
          <?php
            $langId = (string)($language['lang_id'] ?? '');
            if ($langId === '') {
                continue;
            }
            $url = (string)($previewUrls[$langId] ?? '#');
          ?>
          <div class="btn-group ml20 pull-right">
            <a href="#" title="Wybierz stronę" class="btn btn-sm btn-system button url-select" url="<?= htmlspecialchars($url, ENT_QUOTES, 'UTF-8'); ?>">
              <i class="fa fa-check"></i> <span class="hidden-xs"><?= strtoupper(htmlspecialchars($langId, ENT_QUOTES, 'UTF-8')); ?></span>
            </a>
          </div>
        <?php endforeach; ?>
      </div>

      <?= \Shared\Tpl\Tpl::view('pages/subpages-browse-list', [
        'pages' => $page['subpages'] ?? [],
        'step' => $step + 1,
      ]); ?>
    </li>
  <?php endforeach; ?>
</ol>
