<?php
$cfg = app_config();
$base = rtrim((string)($cfg['base_url'] ?? ''), '/');
$assets = rtrim(asset_url(), '/');
$isAdmin = !empty($user) && (($user['role'] ?? '') === 'admin');

$requestPath = (string)parse_url((string)($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH);
$requestPath = '/' . ltrim($requestPath, '/');
$basePath = (string)parse_url($base === '' ? '/' : $base, PHP_URL_PATH);
$basePath = trim($basePath, '/');
if ($basePath !== '' && strncmp($requestPath, '/' . $basePath . '/', strlen('/' . $basePath . '/')) === 0) {
    $requestPath = substr($requestPath, strlen('/' . $basePath));
}
$route = trim($requestPath, '/');

$isActive = static function (array $routes) use ($route): bool {
    foreach ($routes as $item) {
        $item = trim((string)$item, '/');
        if ($item === '') {
            if ($route === '') {
                return true;
            }
            continue;
        }
        if ($route === $item || strncmp($route, $item . '/', strlen($item) + 1) === 0) {
            return true;
        }
    }
    return false;
};
?>
<aside class="lyear-layout-sidebar">
  <div id="logo" class="sidebar-header">
    <a href="<?= e_url(base_url('dashboard')) ?>"><img src="<?= e_url($assets . '/images/logo-sidebar.png') ?>" alt="logo"></a>
  </div>
  <div class="lyear-layout-sidebar-scroll">
    <nav class="sidebar-main">
      <ul class="nav nav-drawer">
        <li class="nav-item<?= $isActive(['', 'dashboard']) ? ' active' : '' ?>"><a href="<?= e_url(base_url('dashboard')) ?>"><i class="mdi mdi-home"></i> <?= app_zh('控制台') ?></a></li>
        <li class="nav-item<?= $isActive(['keys']) && !$isActive(['keys/generate']) ? ' active' : '' ?>"><a href="<?= e_url(base_url('keys')) ?>"><i class="mdi mdi-key"></i> <?= app_zh('密钥列表') ?></a></li>
        <li class="nav-item<?= $isActive(['keys/generate']) ? ' active' : '' ?>"><a href="<?= e_url(base_url('keys/generate')) ?>"><i class="mdi mdi-plus"></i> <?= app_zh('生成密钥') ?></a></li>
        <li class="nav-item<?= $isActive(['connect/demo']) ? ' active' : '' ?>"><a href="<?= e_url(base_url('connect/demo')) ?>"><i class="mdi mdi-layers"></i> <?= app_zh('对接示例') ?></a></li>
        <li class="nav-item<?= $isActive(['settings']) ? ' active' : '' ?>"><a href="<?= e_url(base_url('settings')) ?>"><i class="mdi mdi-account"></i> <?= app_zh('个人设置') ?></a></li>
        <?php if ($isAdmin): ?>
          <li class="nav-item<?= $isActive(['admin/users']) ? ' active' : '' ?>"><a href="<?= e_url(base_url('admin/users')) ?>"><i class="mdi mdi-account-multiple"></i> <?= app_zh('用户管理') ?></a></li>
          <li class="nav-item<?= $isActive(['admin/referrals']) ? ' active' : '' ?>"><a href="<?= e_url(base_url('admin/referrals')) ?>"><i class="mdi mdi-ticket"></i> <?= app_zh('邀请码管理') ?></a></li>
          <li class="nav-item<?= $isActive(['admin/stats']) ? ' active' : '' ?>"><a href="<?= e_url(base_url('admin/stats')) ?>"><i class="mdi mdi-chart-line"></i> <?= app_zh('数据统计') ?></a></li>
          <li class="nav-item<?= $isActive(['admin/system']) ? ' active' : '' ?>"><a href="<?= e_url(base_url('admin/system')) ?>"><i class="mdi mdi-server"></i> <?= app_zh('系统设置') ?></a></li>
        <?php endif; ?>
      </ul>
    </nav>
  </div>
</aside>
