<?php
global $pdo;
$unitList = $pdo->query("SELECT * FROM unit WHERE status=1 ORDER BY urutan ASC, id ASC")->fetchAll();
$unitIcons = [1=>'fas fa-star',2=>'fas fa-users',3=>'fas fa-trophy'];
$unitColors = [1=>'#0d6efd',2=>'#dc3545',3=>'#198754'];
$cp = basename($_SERVER['PHP_SELF']);
$cpDir = basename(dirname($_SERVER['PHP_SELF']));
function sideActive($pages,$cp,$cpDir=''){
  if(is_array($pages)){foreach($pages as $p){if(strpos($p,'/')!==false){[$d,$f]=explode('/',$p);if($cpDir==$d&&($f=='*'||$cp==$f))return 'active';}elseif($cp==$p)return 'active';}return '';}
  return $cp==$pages?'active':'';
}
?>
<div class="sidebar" id="sidebar">
  <div class="sidebar-brand" style="display:flex;align-items:center;justify-content:center;flex-direction:column;gap:.25rem">
    <a href="<?=BASE_URL?>" style="text-decoration:none;display:flex;align-items:center;justify-content:center">
      <?=getSektorLogo('normal')?>
    </a>
    <div style="font-size:.58rem;color:#444;letter-spacing:2px;text-transform:uppercase;margin-top:.1rem"><?=htmlspecialchars(getSektorData()['nama_sektor']??'Sektor Pembangunan Murid')?></div>
  </div>
  <?php if(isLoggedIn()): ?>
  <div style="padding:.5rem .75rem;border-bottom:1px solid rgba(255,255,255,0.05);flex-shrink:0">
    <div style="background:rgba(204,0,1,0.08);border:1px solid rgba(204,0,1,0.15);border-radius:10px;padding:.5rem .75rem;display:flex;align-items:center;gap:.6rem">
      <div style="width:32px;height:32px;border-radius:50%;background:rgba(204,0,1,0.2);display:flex;align-items:center;justify-content:center;font-size:.9rem;flex-shrink:0"><i class="fas fa-user" style="color:#cc0001"></i></div>
      <div style="min-width:0">
        <div style="font-size:.78rem;font-weight:600;color:#fff;white-space:nowrap;overflow:hidden;text-overflow:ellipsis"><?= htmlspecialchars(currentUserName()) ?></div>
        <div style="font-size:.65rem;color:#555;text-transform:uppercase;letter-spacing:1px"><?= currentUserRole() ?></div>
      </div>
    </div>
  </div>
  <?php endif; ?>
  <div class="sidebar-menu">
    <div class="sidebar-section">Menu Utama</div>
    <a href="<?= BASE_URL ?>" class="sidebar-item <?= sideActive(['index.php'],$cp) ?>"><i class="fas fa-home"></i> Utama</a>
    <a href="<?= BASE_URL ?>modules/informasi/" class="sidebar-item <?= sideActive(['informasi/index.php'],$cp,$cpDir) ?>"><i class="fas fa-info-circle"></i> Informasi</a>
    <a href="<?= BASE_URL ?>modules/takwim/" class="sidebar-item <?= sideActive(['takwim/index.php'],$cp,$cpDir) ?>"><i class="fas fa-calendar-alt"></i> Takwim</a>
    <a href="<?= BASE_URL ?>modules/success/" class="sidebar-item <?= sideActive(['success/index.php'],$cp,$cpDir) ?>"><i class="fas fa-award"></i> Success Stories</a>
    <div class="sidebar-section">Unit</div>
    <?php foreach($unitList as $u): ?>
    <a href="<?= BASE_URL ?>modules/pegawai/?unit=<?= $u['id'] ?>" class="sidebar-item <?= ($cpDir=='pegawai'&&($_GET['unit']??'')==$u['id'])?'active':'' ?>">
      <i class="<?= $unitIcons[$u['id']]??'fas fa-building' ?>" style="color:<?= $unitColors[$u['id']]??'#888' ?>"></i>
      <span style="font-size:.8rem;line-height:1.2"><?= htmlspecialchars($u['nama_unit']) ?></span>
    </a>
    <?php endforeach; ?>
    <?php if(isAdmin()): ?>
    <div class="sidebar-section">Pentadbiran</div>
    <a href="<?= BASE_URL ?>modules/pegawai/" class="sidebar-item <?= ($cpDir=='pegawai'&&!isset($_GET['unit']))?'active':'' ?>"><i class="fas fa-users-cog"></i> Urus Pegawai</a>
    <a href="<?= BASE_URL ?>modules/laporan/" class="sidebar-item <?= sideActive(['laporan/index.php'],$cp,$cpDir) ?>"><i class="fas fa-chart-bar"></i> Laporan</a>
    <?php if(isSuperAdmin()): ?>
    <a href="<?= BASE_URL ?>modules/admin/" class="sidebar-item <?= sideActive(['admin/index.php'],$cp,$cpDir) ?>"><i class="fas fa-cog"></i> Tetapan Sistem</a>
    <?php endif; ?>
    <?php endif; ?>
    <div class="sidebar-section">Akaun</div>
    <a href="<?= BASE_URL ?>modules/auth/profile.php" class="sidebar-item <?= sideActive(['profile.php'],$cp) ?>"><i class="fas fa-user-circle"></i> Profil Saya</a>
    <a href="<?= BASE_URL ?>modules/auth/logout.php" class="sidebar-item" style="color:#dc3545!important"><i class="fas fa-sign-out-alt"></i> Log Keluar</a>
  </div>
</div>
