<?php
declare(strict_types=1);
/**
 * 대메뉴 호버 시 소메뉴 드롭다운 (gov-portal-strip)
 *
 * @var list<array> $govNavItems
 * @var int $govActiveParentIdx
 * @var string $govCurrentPath
 * @var list<string> $govDashboardAliases
 */
helper('admin');
?>
<nav class="portal-top-nav" aria-label="대분류 메뉴">
  <?php foreach ($govNavItems as $pIdx => $item): ?>
  <?php
  $isActive = ($pIdx === $govActiveParentIdx);
  $parentHref = $item['href'] ?? '';
  $hasChildren = ! empty($item['hasChildren']);
  $activeChild = null;
  if ($hasChildren && ! empty($siteNavTree[$pIdx])) {
      $activeChild = menu_active_child_for_parent($siteNavTree[$pIdx], $govCurrentPath, $govDashboardAliases);
  }
  ?>
  <div class="portal-nav-item">
    <?php if ($hasChildren): ?>
    <?php if ($parentHref !== ''): ?>
    <a href="<?= esc($item['url']) ?>" class="portal-nav-link <?= $isActive ? 'is-active' : '' ?>">
      <?= esc($item['name']) ?>
    </a>
    <?php else: ?>
    <span class="portal-nav-trigger <?= $isActive ? 'is-active' : '' ?>" tabindex="0"><?= esc($item['name']) ?></span>
    <?php endif; ?>
    <div class="portal-nav-dropdown">
      <div class="portal-nav-dropdown-panel">
        <?php foreach ($item['children'] as $child): ?>
        <?php if ($child['href'] !== ''): ?>
        <?php
          $childActive = $activeChild !== null
              && (int) ($activeChild->mm_idx ?? 0) === (int) ($child['idx'] ?? 0);
        ?>
        <a href="<?= esc($child['url']) ?>" class="<?= $childActive ? 'is-active' : '' ?>">
          <?= esc($child['name']) ?>
        </a>
        <?php else: ?>
        <span class="no-link" title="메뉴 링크 미설정"><?= esc($child['name']) ?></span>
        <?php endif; ?>
        <?php endforeach; ?>
      </div>
    </div>
    <?php elseif ($parentHref !== ''): ?>
    <a href="<?= esc($item['url']) ?>" class="portal-nav-link <?= $isActive ? 'is-active' : '' ?>">
      <?= esc($item['name']) ?>
    </a>
    <?php else: ?>
    <span class="portal-nav-trigger" style="opacity:.5;"><?= esc($item['name']) ?></span>
    <?php endif; ?>
  </div>
  <?php endforeach; ?>
</nav>
