<?php 
  $db = \Config\Database::connect();
  $role = session()->get('role');
  
  // Ambil menu yang hanya boleh diakses oleh role user saat ini
  $userMenus = $db->query("SELECT * FROM menus WHERE FIND_IN_SET('$role', role_akses) ORDER BY urutan ASC")->getResultArray();
  $namaAplikasi = $db->table('konfig_sistem')->where('meta_key', 'nama_aplikasi')->get()->getRowArray();
?>

<aside class="w-64 bg-slate-950 text-slate-200 min-h-screen flex flex-col justify-between border-r border-slate-800 shadow-xl">
    <div>
        <div class="p-5 border-b border-slate-800 bg-slate-900/50 flex items-center gap-3">
            <div class="h-8 w-8 rounded-lg bg-indigo-600 flex items-center justify-center font-bold text-white shadow-lg shadow-indigo-500/30">A</div>
            <div>
                <h1 class="text-sm font-bold text-white truncate w-44"><?= $namaAplikasi['meta_value'] ?? 'Portal Apresiasi' ?></h1>
                <span class="text-xs text-indigo-400 font-medium capitalize"><?= str_replace('_', ' ', $role) ?></span>
            </div>
        </div>

        <nav class="p-4 space-y-1.5">
            <p class="text-[10px] font-bold text-slate-500 uppercase tracking-wider px-3 mb-2">Menu Navigasi</p>
            <?php foreach($userMenus as $m): ?>
                <a href="<?= base_url($m['url_rute']) ?>" class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium transition-all group hover:bg-slate-900 hover:text-white">
                    <i class="fas <?= $m['ikon'] ?> text-slate-400 group-hover:text-indigo-400 transition-colors"></i>
                    <span><?= $m['nama_menu'] ?></span>
                </a>
            <?php endforeach; ?>
        </nav>
    </div>

    <div class="p-4 border-t border-slate-800 bg-slate-900/30">
        <div class="flex items-center gap-3 mb-3">
            <div class="h-9 w-9 rounded-full bg-slate-800 flex items-center justify-center text-sm font-semibold uppercase text-slate-300 border border-slate-700"><?= substr(session()->get('username'), 0, 2) ?></div>
            <div class="truncate w-36">
                <p class="text-xs font-semibold text-white truncate"><?= session()->get('username') ?></p>
                <p class="text-[10px] text-slate-500 truncate"><?= session()->get('instansi') ?></p>
            </div>
        </div>
        <a href="/logout" class="block w-full text-center py-2 text-xs font-semibold text-red-400 bg-red-500/10 rounded-lg hover:bg-red-500 hover:text-white transition-all">Keluar Sistem</a>
    </div>
</aside>