<?php
// PHP to dynamically generate navigation menu
function getNavigationMenu() {
    $menuItems = [];
    $basePath = $_SERVER['DOCUMENT_ROOT']; // Use document root for consistency
    $directories = glob($basePath . '/*', GLOB_ONLYDIR);

    foreach ($directories as $dir) {
        $menuFile = $dir . '/menu.json';
        if (file_exists($menuFile)) {
            $json = file_get_contents($menuFile);
            $data = json_decode($json, true);
            if ($data && isset($data['id']) && isset($data['name'])) {
                $data['path'] = '/' . basename($dir);
                $menuItems[] = $data;
            }
        }
    }

    // Sort menu items by ID
    usort($menuItems, function($a, $b) {
        return $a['id'] <=> $b['id'];
    });

    return $menuItems;
}

$navigationItems = getNavigationMenu();

// Get current request URI to determine active page
$currentUri = $_SERVER['REQUEST_URI'];

// Function to check if a menu item is active
function isMenuItemActive($itemPath, $currentUri) {
    // Exact match or if the current URI starts with the item's path (for subpages)
    if ($currentUri === $itemPath || ($itemPath !== '/' && strpos($currentUri, $itemPath) === 0)) {
        return true;
    }
    return false;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title><?= isset($pageTitle) ? htmlspecialchars($pageTitle) . ' | ' : '' ?>Aegis Infrastructure</title>
  <meta name="description" content="Aegis Infrastructure: 通过基础设施即代码 (IaC)、不可变基础设施和云原生技术，构建安全、可扩展且高度可用的现代 IT 解决方案。" />
  
  <!-- **关键新增**: 添加了SVG格式的Favicon -->
  <link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect width='100' height='100' rx='20' fill='%2302030A'/%3E%3Crect x='10' y='10' width='80' height='80' rx='10' fill='none' stroke='%2300BFFF' stroke-width='8'/%3E%3Ctext x='50' y='68' font-size='50' font-weight='bold' fill='%23E0E8FF' text-anchor='middle' font-family='sans-serif'%3EN%3C/text%3E%3C/svg%3E">

  <!-- CSS 变量，用于动态调整头部厚度 -->
  <style>
    :root {
      --header-height: 84px;      /* 默认头部高度 */
      --header-height-scrolled: 76px; /* 滚动后头部高度 */
      --nav-padding-fix: 2px;      /* 用于微调菜单垂直对齐的 Padding */
    }
    .site-header {
      height: var(--header-height);
    }
    .site-header.is-scrolled {
      height: var(--header-height-scrolled);
    }
    .nav {
      padding-top: var(--nav-padding-fix);
    }
  </style>
  
  <link rel="stylesheet" href="/index.css">
</head>
<body>
  <!-- 进度条 -->
  <div class="progress" id="progressBar" aria-hidden="true"></div>

  <!-- ============== 页眉 ============== -->
  <header class="site-header" id="siteHeader" role="banner">
    <div class="nav-inner">
      <a href="/" class="brand" aria-label="Aegis Infrastructure首页">
        <svg class="logo-crest" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
          <path d="M50 2 L95 25 L95 75 L50 98 L5 75 L5 25 Z" fill="var(--brand)" opacity="0.1"/>
          <path d="M50 2 L95 25 L95 75 L50 98 L5 75 L5 25 Z" stroke="var(--brand)" stroke-width="3" fill="none"/>
          <path d="M25 37 L50 25 L75 37 L75 63 L50 75 L25 63 Z" stroke="var(--text)" stroke-width="3" fill="none" opacity="0.6"/>
        </svg>
        <span class="brand-title">Aegis Infrastructure</span>
      </a>
      
      <nav class="nav" aria-label="主导航">
        <div class="nav-item">
            <a href="/" class="<?= ($currentUri === '/') ? 'is-active' : '' ?>">首页</a>
        </div>
        <?php foreach ($navigationItems as $item): ?>
            <?php $hasSubmenu = isset($item['submenu']); ?>
            <div class="nav-item <?= $hasSubmenu ? 'has-submenu' : '' ?>">
                <a href="<?= $hasSubmenu ? 'javascript:;' : htmlspecialchars($item['path']) ?>" class="<?= isMenuItemActive($item['path'], $currentUri) ? 'is-active' : '' ?>">
                    <?= htmlspecialchars($item['name']) ?>
                </a>
                <?php if ($hasSubmenu): ?>
                    <div class="submenu">
                        <?php foreach ($item['submenu'] as $sub): ?>
                            <a href="<?= htmlspecialchars($sub['href']) ?>"><?= htmlspecialchars($sub['name']) ?></a>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
      </nav>

      <button class="menu-toggle" id="menuToggle" aria-label="打开导航菜单" aria-expanded="false"><span></span><span></span><span></span></button>
    </div>
  </header>

  <!-- 移动端抽屉导航 -->
  <aside class="drawer" id="drawer" aria-hidden="true">
      <div class="drawer-item">
          <a href="/">首页</a>
      </div>
      <?php foreach ($navigationItems as $item): ?>
          <div class="drawer-item <?= isset($item['submenu']) ? 'has-submenu' : '' ?>">
              <a href="<?= isset($item['submenu']) ? 'javascript:;' : htmlspecialchars($item['path']) ?>"><?= htmlspecialchars($item['name']) ?></a>
              <?php if (isset($item['submenu'])): ?>
                  <div class="drawer-submenu">
                      <?php foreach ($item['submenu'] as $sub): ?>
                          <a href="<?= htmlspecialchars($sub['href']) ?>"><?= htmlspecialchars($sub['name']) ?></a>
                      <?php endforeach; ?>
                  </div>
              <?php endif; ?>
          </div>
      <?php endforeach; ?>
  </aside>

  <div class="scrim" id="scrim" aria-hidden="true"></div>

