<?php
$current = $_GET['sayfa'] ?? 'dashboard';
$lang = $_SESSION['lang'] ?? ($_COOKIE['lang'] ?? 'tr');
$full = $_SESSION['full_name'] ?? 'User';
$role = $_SESSION['role'] ?? 'staff';

function initials(string $name): string {
  $parts = preg_split('/\s+/', trim($name));
  $a = strtoupper(mb_substr($parts[0] ?? 'N', 0, 1));
  $b = strtoupper(mb_substr($parts[count($parts)-1] ?? 'L', 0, 1));
  return $a . $b;
}
?>
<header class="nl-topbar">
  <button class="nl-icon-btn" type="button" onclick="toggleSidebar()" aria-label="Menu">
    <i class="bi bi-list"></i>
  </button>

  <div class="nl-topbar-title">
    <div class="nl-app">Nephro Link</div>
    <div class="nl-sub"><?= htmlspecialchars($current) ?></div>
  </div>

  <div class="nl-topbar-actions">
    <form method="post" action="" class="d-flex align-items-center gap-2">
      <input type="hidden" name="action" value="set_lang">
      <input type="hidden" name="back" value="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? 'dashboard.php') ?>">
      <select name="lang" class="nl-select" onchange="this.form.submit()">
        <option value="tr" <?= $lang==='tr'?'selected':'' ?>>TR</option>
        <option value="en" <?= $lang==='en'?'selected':'' ?>>EN</option>
      </select>
    </form>

    <div class="nl-user">
      <div class="nl-avatar"><?= initials($full) ?></div>
      <div class="nl-user-meta">
        <strong><?= htmlspecialchars($full) ?></strong>
        <span><?= htmlspecialchars($role) ?></span>
      </div>
    </div>
  </div>
</header>

<style>
.nl-topbar{
  position: sticky; top: 0; z-index: 900;
  height: 70px;
  display:flex; align-items:center; justify-content:space-between;
  gap:14px;
  padding: 0 18px;
  background:#fff;
  border-bottom:1px solid var(--border, #e2e8f0);
}
.nl-icon-btn{
  width:44px; height:44px;
  border-radius:12px;
  border:1px solid var(--border, #e2e8f0);
  background:#fff;
  display:flex; align-items:center; justify-content:center;
  color:#0f172a;
}
.nl-icon-btn:hover{ background:#f8fafc; }
.nl-topbar-title{ display:flex; flex-direction:column; gap:2px; min-width:0; }
.nl-app{
  font-weight:800; letter-spacing:-0.3px;
  color:#0f172a; font-size:1.05rem;
}
.nl-sub{
  color:#64748b; font-size:0.8rem;
  white-space:nowrap; overflow:hidden; text-overflow:ellipsis;
}
.nl-topbar-actions{ display:flex; align-items:center; gap:12px; }
.nl-select{
  border:1px solid var(--border, #e2e8f0);
  border-radius:12px;
  padding:8px 10px;
  background:#fff;
  font-weight:700;
  color:#0f172a;
}
.nl-user{
  display:flex; align-items:center; gap:10px;
  padding:8px 10px;
  border:1px solid var(--border, #e2e8f0);
  border-radius:14px;
  background:#fff;
}
.nl-avatar{
  width:38px; height:38px; border-radius:12px;
  display:flex; align-items:center; justify-content:center;
  font-weight:900; color:#475569;
  background:#e2e8f0;
}
.nl-user-meta strong{ display:block; font-size:0.85rem; color:#0f172a; }
.nl-user-meta span{ display:block; font-size:0.72rem; color:#94a3b8; text-transform:capitalize; }
</style>
