<?php
$current = $_GET['sayfa'] ?? 'dashboard';

if (!function_exists('isActive')) {
  function isActive($p, $c) { return $p === $c ? 'is-active' : ''; }
}

$lang = $_SESSION['lang'] ?? ($_COOKIE['lang'] ?? 'tr');
$lang = in_array($lang, ['tr','en'], true) ? $lang : 'tr';

$TXT = [
  'tr' => [
    'dashboard' => 'Genel Bakış (Dashboard)',

    'grp_patient' => 'Hasta İşlemleri',
    'hasta_ekle' => 'Yeni Hasta Kaydı',
    'hasta_liste' => 'Hasta Listesi',
    'hasta_onam' => 'Onam & Arşiv',
    'dislab_gonder' => 'Dışlab Hasta Gönderimi',
    'dislab_izle' => 'Dışlab Hasta İzle',

    'grp_seans' => 'Seans & Randevu',
    'seans_baslat' => 'Seans Başlat',
    'aktif_seans' => 'Aktif Seans Takibi',
    'seans_takvimi' => 'Haftalık Planlama',
    'bugunku_seanslar' => 'Bugünkü Akış',
    'yatak_doluluk' => 'Cihaz/Yatak Durumu',

    'grp_klinik' => 'Klinik & Tıbbi',
    'vizit_notlari' => 'Doktor Vizit Notları',
    'hemsire_notlari' => 'Hemşire Gözlem',
    'lab_sonuclari' => 'Laboratuvar Sonuçları',

    'grp_loji' => 'Lojistik & Depo',
    'stok_takip' => 'Sarf Malzeme Stok',
    'stok_liste' => 'Malzeme Listesi',
    'stok_hareket' => 'Depo Hareketleri',

    'grp_system' => 'Sistem',
    'test_tanimlari' => 'Test Tanımları',
	'dislab-sync' => 'Dışlab Test Senk.',
    'tanimlar' => 'Sistem Tanımları',
	'dislab-tanimlari' => 'Dış Laboratuvar Tanımları',
    'cihazlar' => 'Cihaz Tanımları',
    'cihaz_bakimlari' => 'Cihaz Bakımları',
    'ayarlar' => 'Sistem Ayarları',
    'log_listesi' => 'Sistem Logları',

    'logout' => 'Güvenli Çıkış',
  ],
  'en' => [
    'dashboard' => 'Overview (Dashboard)',

    'grp_patient' => 'Patient Operations',
    'hasta_ekle' => 'New Patient',
    'hasta_liste' => 'Patient List',
    'hasta_onam' => 'Consents & Archive',
    'dislab_gonder' => 'External Lab Transfer',
    'dislab_izle' => 'External Lab Tracking',

    'grp_seans' => 'Sessions & Appointments',
    'seans_baslat' => 'Start Session',
    'aktif_seans' => 'Active Session Tracking',
    'seans_takvimi' => 'Weekly Planning',
    'bugunku_seanslar' => 'Today’s Flow',
    'yatak_doluluk' => 'Device/Bed Status',

    'grp_klinik' => 'Clinical',
    'vizit_notlari' => 'Doctor Rounds Notes',
    'hemsire_notlari' => 'Nursing Observation',
    'lab_sonuclari' => 'Lab Results',

    'grp_loji' => 'Logistics & Inventory',
    'stok_takip' => 'Consumables Stock',
    'stok_liste' => 'Materials List',
    'stok_hareket' => 'Warehouse Movements',

    'grp_system' => 'System',
    'test_tanimlari' => 'Test Definitions',
	 'dislab-sync' => 'External Laboratory Test Syn.',
    'tanimlar' => 'System Definitions',
	'dislab-tanimlari' => 'External Lab Definitions',
    'cihazlar' => 'Device Definitions',
    'cihaz_bakimlari' => 'Device Maintenance',
    'ayarlar' => 'System Settings',
    'log_listesi' => 'System Logs',

    'logout' => 'Sign Out',
  ]
];

function __t(array $TXT, string $lang, string $key): string {
  return htmlspecialchars($TXT[$lang][$key] ?? $key, ENT_QUOTES, 'UTF-8');
}

$full = $_SESSION['full_name'] ?? 'User';
$role = $_SESSION['role'] ?? 'staff';

function initials2(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;
}

// hangi sayfa hangi accordion grubu?
$groupOf = [
  'hasta-ekle' => 'grpPatient',
  'hasta-liste' => 'grpPatient',
  'hasta-onam' => 'grpPatient',
  'dislab-gonder' => 'grpPatient',
  'dislab-izle' => 'grpPatient',

  'seans-baslat' => 'grpSeans',
  'aktif-seans-takip' => 'grpSeans',
  'seans-takvimi' => 'grpSeans',
  'bugunku-seanslar' => 'grpSeans',
  'yatak-doluluk' => 'grpSeans',

  'vizit-notlari' => 'grpClinic',
  'hemsire-notlari' => 'grpClinic',
  'lab-sonuclari' => 'grpClinic',

  'stok-takip' => 'grpStock',
  'stok-liste' => 'grpStock',
  'stok-hareket' => 'grpStock',

  'test-tanimlari' => 'grpSystem',
  'tanimlar' => 'grpSystem',
  'dislab-tanimlari' => 'grpSystem',
  'cihazlar' => 'grpSystem',
  'cihaz-bakimlari' => 'grpSystem',
  'ayarlar' => 'grpSystem',
  'log-listesi' => 'grpSystem',
];

$autoOpen = $groupOf[$current] ?? '';
?>

<div class="sidebar-overlay" onclick="toggleSidebar()"></div>

<aside class="main-sidebar">
  <div class="sidebar-brand">
    <a href="dashboard.php" style="text-decoration:none; display:flex; align-items:center; gap:12px;">
      <div class="brand-logo">
        <img src="includes/logo.png" alt="Nephro Link" style="width:22px;height:22px;object-fit:contain;">
      </div>
      <div class="brand-name">Nephro <span>Link</span></div>
    </a>
  </div>

  <nav class="sidebar-nav">
    <ul class="menu-list" style="list-style:none; padding:0; margin:0;">

      <!-- Dashboard -->
      <li class="menu-item">
        <a href="dashboard.php?sayfa=dashboard" class="menu-link <?php echo ($current=='dashboard' || $current=='') ? 'is-active' : ''; ?>">
          <i class="bi bi-bar-chart-line"></i> <span><?= __t($TXT, $lang, 'dashboard') ?></span>
        </a>
      </li>

      <!-- Hasta İşlemleri -->
      <li class="acc-wrap" data-acc="grpPatient">
        <button class="acc-head" type="button" onclick="accToggle('grpPatient')">
          <span class="acc-title"><?= __t($TXT, $lang, 'grp_patient') ?></span>
          <i class="bi bi-chevron-down acc-ico"></i>
        </button>

        <div class="acc-body">
          <a href="dashboard.php?sayfa=hasta-ekle" class="menu-link <?php echo isActive('hasta-ekle', $current); ?>">
            <i class="bi bi-person-plus"></i> <span><?= __t($TXT, $lang, 'hasta_ekle') ?></span>
          </a>

          <a href="dashboard.php?sayfa=hasta-liste" class="menu-link <?php echo isActive('hasta-liste', $current); ?>">
            <i class="bi bi-people"></i> <span><?= __t($TXT, $lang, 'hasta_liste') ?></span>
          </a>

          <a href="dashboard.php?sayfa=hasta-onam" class="menu-link <?php echo isActive('hasta-onam', $current); ?>">
            <i class="bi bi-file-earmark-text"></i> <span><?= __t($TXT, $lang, 'hasta_onam') ?></span>
          </a>

          <a href="dashboard.php?sayfa=dislab-gonder" class="menu-link <?php echo isActive('dislab-gonder', $current); ?>">
            <i class="bi bi-file-arrow-up"></i> <span><?= __t($TXT, $lang, 'dislab_gonder') ?></span>
          </a>

          <a href="dashboard.php?sayfa=dislab-izle" class="menu-link <?php echo isActive('dislab-izle', $current); ?>">
            <i class="bi bi-eye"></i> <span><?= __t($TXT, $lang, 'dislab_izle') ?></span>
          </a>
        </div>
      </li>

      <!-- Seans & Randevu -->
      <li class="acc-wrap" data-acc="grpSeans">
        <button class="acc-head" type="button" onclick="accToggle('grpSeans')">
          <span class="acc-title"><?= __t($TXT, $lang, 'grp_seans') ?></span>
          <i class="bi bi-chevron-down acc-ico"></i>
        </button>

        <div class="acc-body">
          <a href="dashboard.php?sayfa=seans-baslat" class="menu-link <?php echo isActive('seans-baslat', $current); ?>">
            <i class="bi bi-stopwatch"></i> <span><?= __t($TXT, $lang, 'seans_baslat') ?></span>
          </a>

          <a href="dashboard.php?sayfa=aktif-seans-takip" class="menu-link <?php echo isActive('aktif-seans-takip', $current); ?>">
            <i class="bi bi-broadcast"></i> <span><?= __t($TXT, $lang, 'aktif_seans') ?></span>
          </a>

          <a href="dashboard.php?sayfa=seans-takvimi" class="menu-link <?php echo isActive('seans-takvimi', $current); ?>">
            <i class="bi bi-calendar-week"></i> <span><?= __t($TXT, $lang, 'seans_takvimi') ?></span>
          </a>

          <a href="dashboard.php?sayfa=bugunku-seanslar" class="menu-link <?php echo isActive('bugunku-seanslar', $current); ?>">
            <i class="bi bi-clock-history"></i> <span><?= __t($TXT, $lang, 'bugunku_seanslar') ?></span>
          </a>

          <a href="dashboard.php?sayfa=yatak-doluluk" class="menu-link <?php echo isActive('yatak-doluluk', $current); ?>">
            <i class="bi bi-hdd-network"></i> <span><?= __t($TXT, $lang, 'yatak_doluluk') ?></span>
          </a>
        </div>
      </li>

      <!-- Klinik & Tıbbi -->
      <li class="acc-wrap" data-acc="grpClinic">
        <button class="acc-head" type="button" onclick="accToggle('grpClinic')">
          <span class="acc-title"><?= __t($TXT, $lang, 'grp_klinik') ?></span>
          <i class="bi bi-chevron-down acc-ico"></i>
        </button>

        <div class="acc-body">
          <a href="dashboard.php?sayfa=vizit-notlari" class="menu-link <?php echo isActive('vizit-notlari', $current); ?>">
            <i class="bi bi-person-badge"></i> <span><?= __t($TXT, $lang, 'vizit_notlari') ?></span>
          </a>

          <a href="dashboard.php?sayfa=hemsire-notlari" class="menu-link <?php echo isActive('hemsire-notlari', $current); ?>">
            <i class="bi bi-clipboard2-pulse"></i> <span><?= __t($TXT, $lang, 'hemsire_notlari') ?></span>
          </a>

          <a href="dashboard.php?sayfa=lab-sonuclari" class="menu-link <?php echo isActive('lab-sonuclari', $current); ?>">
            <i class="bi bi-graph-up"></i> <span><?= __t($TXT, $lang, 'lab_sonuclari') ?></span>
          </a>
        </div>
      </li>

      <!-- Lojistik & Depo -->
      <li class="acc-wrap" data-acc="grpStock">
        <button class="acc-head" type="button" onclick="accToggle('grpStock')">
          <span class="acc-title"><?= __t($TXT, $lang, 'grp_loji') ?></span>
          <i class="bi bi-chevron-down acc-ico"></i>
        </button>

        <div class="acc-body">
          <a href="dashboard.php?sayfa=stok-takip" class="menu-link <?php echo isActive('stok-takip', $current); ?>">
            <i class="bi bi-box-seam"></i> <span><?= __t($TXT, $lang, 'stok_takip') ?></span>
          </a>

          <a href="dashboard.php?sayfa=stok-liste" class="menu-link <?php echo isActive('stok-liste', $current); ?>">
            <i class="bi bi-boxes"></i> <span><?= __t($TXT, $lang, 'stok_liste') ?></span>
          </a>

          <a href="dashboard.php?sayfa=stok-hareket" class="menu-link <?php echo isActive('stok-hareket', $current); ?>">
            <i class="bi bi-arrow-left-right"></i> <span><?= __t($TXT, $lang, 'stok_hareket') ?></span>
          </a>
        </div>
      </li>

      <!-- Sistem -->
      <li class="acc-wrap" data-acc="grpSystem">
        <button class="acc-head" type="button" onclick="accToggle('grpSystem')">
          <span class="acc-title"><?= __t($TXT, $lang, 'grp_system') ?></span>
          <i class="bi bi-chevron-down acc-ico"></i>
        </button>

        <div class="acc-body">
          <a href="dashboard.php?sayfa=test-tanimlari" class="menu-link <?php echo isActive('test-tanimlari', $current); ?>">
            <i class="bi bi-eyedropper"></i> <span><?= __t($TXT, $lang, 'test_tanimlari') ?></span>
          </a>
			
			<div class="acc-body">
          <a href="dashboard.php?sayfa=dislab-sync" class="menu-link <?php echo isActive('dislab-sync', $current); ?>">
            <i class="bi bi-cloud-arrow-up"></i> <span><?= __t($TXT, $lang, 'dislab-sync') ?></span>
          </a>

          <a href="dashboard.php?sayfa=tanimlar" class="menu-link <?php echo isActive('tanimlar', $current); ?>">
            <i class="bi bi-sliders"></i> <span><?= __t($TXT, $lang, 'tanimlar') ?></span>
          </a>
			
		<a href="dashboard.php?sayfa=dislab-tanimlari" class="menu-link <?php echo isActive('dislab-tanimlari', $current); ?>">
    <i class="bi bi-building-gear"></i> 
    <span><?= __t($TXT, $lang, 'dislab-tanimlari') ?></span>
</a>
          <a href="dashboard.php?sayfa=cihazlar" class="menu-link <?php echo isActive('cihazlar', $current); ?>">
            <i class="bi bi-cpu"></i> <span><?= __t($TXT, $lang, 'cihazlar') ?></span>
          </a>

          <a href="dashboard.php?sayfa=cihaz-bakimlari" class="menu-link <?php echo isActive('cihaz-bakimlari', $current); ?>">
            <i class="bi bi-tools"></i> <span><?= __t($TXT, $lang, 'cihaz_bakimlari') ?></span>
          </a>

          <a href="dashboard.php?sayfa=ayarlar" class="menu-link <?php echo isActive('ayarlar', $current); ?>">
            <i class="bi bi-gear"></i> <span><?= __t($TXT, $lang, 'ayarlar') ?></span>
          </a>

          <a href="dashboard.php?sayfa=log-listesi" class="menu-link <?php echo isActive('log-listesi', $current); ?>">
            <i class="bi bi-journal-text"></i> <span><?= __t($TXT, $lang, 'log_listesi') ?></span>
          </a>
        </div>
      </li>

    </ul>
  </nav>

  <!-- Güvenli Çıkış: en alt sabit -->
  <div class="sidebar-logout">
    <a href="logout.php" class="menu-link logout-link">
      <i class="bi bi-power"></i> <span><?= __t($TXT, $lang, 'logout') ?></span>
    </a>
  </div>

  <div class="sidebar-footer">
    <div class="user-pill">
      <div class="u-avatar"><?= initials2($full) ?></div>
      <div class="u-info">
        <strong><?= htmlspecialchars($full) ?></strong>
        <span><?= htmlspecialchars($role) ?></span>
      </div>
    </div>
  </div>
</aside>

<style>
/* === ANA TASARIM (SENİN ÇİZGİN) === */
.main-sidebar{
  width: var(--sb-w, 290px);
  height: 100vh;
  background:#fff;
  border-right:1px solid var(--border, #e2e8f0);
  position:fixed; left:0; top:0;
  display:flex; flex-direction:column;
  z-index:1000;
  transition:all .3s cubic-bezier(.4,0,.2,1);
  font-family:'Inter', sans-serif;
}
body.sidebar-closed .main-sidebar{ left: calc(var(--sb-w, 290px) * -1); }

.sidebar-brand{
  height:70px; padding:0 25px;
  display:flex; align-items:center; gap:12px;
  border-bottom:1px solid var(--border, #e2e8f0);
}
.brand-logo{
  width:38px; height:38px;
  background: var(--accent, #4361ee);
  border-radius:12px;
  display:flex; align-items:center; justify-content:center;
  box-shadow:0 4px 10px rgba(67,97,238,.18);
}
.brand-name{ font-weight:800; font-size:1.1rem; color:#0f172a; letter-spacing:-.5px; }
.brand-name span{ color: var(--accent, #4361ee); font-weight:300; }

.sidebar-nav{
  flex:1;
  overflow-y:auto;
  padding:15px;
  scrollbar-width: thin;
  scrollbar-color:#e2e8f0 transparent;
}

/* === ACCORDION BAŞLIĞI: menu-label tipografisi BİREBİR === */
.acc-wrap{ margin-top: 10px; }

.acc-head{
  width:100%;
  display:flex; align-items:center; justify-content:space-between;

  /* menu-label ile aynı */
  padding: 25px 15px 12px;
  font-family:'Inter', sans-serif;
  font-size: 0.7rem;
  font-weight: 800;
  color: #334155;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  line-height: 1.1;

  background: transparent;
  border: 0;
  text-align: left;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;

  border-top: 1px solid #f8fafc;
}
.acc-title{
  font-family:'Inter', sans-serif;
  font-size: 0.7rem;
  font-weight: 800;
  color: #334155;
  text-transform: uppercase;
  letter-spacing: 1.5px;
}
.acc-ico{
  color:#94a3b8;
  font-size: 0.95rem;
  margin-top: -2px;
  transition:.2s;
}
.acc-wrap.open .acc-ico{ transform: rotate(180deg); color: var(--accent, #4361ee); }

.acc-body{ display:none; padding-top: 6px; }
.acc-wrap.open .acc-body{ display:block; }

/* Link stili aynı */
.menu-link{
  display:flex; align-items:center; gap:14px;
  padding:12px 16px;
  color:#64748b;
  text-decoration:none;
  font-weight:600;
  font-size:.9rem;
  border-radius:14px;
  transition:.2s;
  margin-bottom:4px;
}
.menu-link i{
  font-size:1.1rem;
  width:24px;
  text-align:center;
  color:#94a3b8;
  transition:.2s;
}
.menu-link:hover{ background:#f8fafc; color: var(--accent, #4361ee); }
.menu-link:hover i{ color: var(--accent, #4361ee); }
.menu-link.is-active{
  background:#eff6ff;
  color: var(--accent, #4361ee);
  border-right:3px solid var(--accent, #4361ee);
  border-radius:14px 4px 4px 14px;
}
.menu-link.is-active i{ color: var(--accent, #4361ee); }

/* Logout sabit */
.sidebar-logout{
  padding: 10px 15px 0;
  border-top: 1px solid var(--border, #e2e8f0);
  background:#fff;
}
.logout-link{
  color:#ef4444 !important;
}
.logout-link i{ color:#ef4444 !important; }
.logout-link:hover{
  background:#fff5f5;
  color:#ef4444 !important;
}

/* Footer */
.sidebar-footer{
  padding: 12px 15px 15px;
  background:#fff;
}
.user-pill{
  background:#f8fafc;
  padding:12px;
  border-radius:16px;
  display:flex; align-items:center; gap:12px;
  border:1px solid #f1f5f9;
}
.u-avatar{
  width:38px; height:38px;
  background:#e2e8f0;
  border-radius:10px;
  display:flex; align-items:center; justify-content:center;
  font-weight:800; color:#475569;
}
.u-info strong{ display:block; font-size:.85rem; color:#0f172a; }
.u-info span{ font-size:.7rem; color:#94a3b8; text-transform:capitalize; }

.sidebar-overlay{
  display:none;
  position:fixed; inset:0;
  background: rgba(0,0,0,.35);
  z-index:999;
  backdrop-filter: blur(2px);
}
@media (max-width: 992px){
  .main-sidebar{ left: calc(var(--sb-w, 290px) * -1); }
  body.sidebar-mobile-open .main-sidebar{ left:0; box-shadow: 20px 0 50px rgba(0,0,0,.1); }
  body.sidebar-mobile-open .sidebar-overlay{ display:block; }
}
</style>

<script>
(function(){
  const KEY = "nephrolink_sidebar_open_groups";

  function getSet(){
    try{
      const raw = localStorage.getItem(KEY);
      const arr = raw ? JSON.parse(raw) : [];
      return new Set(Array.isArray(arr) ? arr : []);
    }catch(e){
      return new Set();
    }
  }
  function saveSet(set){
    try{ localStorage.setItem(KEY, JSON.stringify(Array.from(set))); }catch(e){}
  }

  window.accToggle = function(id){
    const wrap = document.querySelector('.acc-wrap[data-acc="'+id+'"]');
    if(!wrap) return;

    const openSet = getSet();
    const willOpen = !wrap.classList.contains('open');

    wrap.classList.toggle('open', willOpen);

    if (willOpen) openSet.add(id);
    else openSet.delete(id);

    saveSet(openSet);
  };

  // init: localStorage + aktif sayfanın grubu açık (öncelik)
  const openSet = getSet();
  const autoOpen = "<?= htmlspecialchars($autoOpen) ?>";

  document.querySelectorAll('.acc-wrap[data-acc]').forEach(wrap => {
    const id = wrap.getAttribute('data-acc');
    const shouldOpen = (autoOpen && id === autoOpen) || openSet.has(id);
    wrap.classList.toggle('open', !!shouldOpen);
    if (autoOpen && id === autoOpen) openSet.add(id);
  });

  saveSet(openSet);
})();
</script>
