<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $this->renderSection('title') ?> - SIKAD Universitas Gemilang</title>
    <!-- CSS Terpusat -->
    <link rel="stylesheet" href="<?= base_url('assets/css/style.css') ?>">
</head>
<body>

    <div class="dashboard-layout">
        <!-- Sidebar -->
        <aside class="sidebar">
            <div class="sidebar-brand">
                <div class="sidebar-brand-icon">
                    <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                        <path d="M12 3L1 9L12 15L21 10.09V17H23V9M5 13.18V17.18L12 21L19 17.18V13.18L12 17L5 13.18Z"/>
                    </svg>
                </div>
                <div class="sidebar-brand-text">
                    <h2>SIKAD</h2>
                    <span>Sistem Akademik</span>
                </div>
            </div>
            
            <?php
                // Logika pemetaan nama Role ke nama file sidebar
                $role = session()->get('role');
                $sidebarFile = 'default';
                
                switch($role) {
                    case 'Admin': $sidebarFile = 'admin'; break;
                    case 'Mahasiswa': $sidebarFile = 'mahasiswa'; break;
                    case 'Dosen': $sidebarFile = 'dosen'; break;
                    case 'TU': $sidebarFile = 'tu'; break;
                    case 'Keuangan': $sidebarFile = 'keuangan'; break;
                    case 'Administrasi Keuangan': $sidebarFile = 'admin_keuangan'; break;
                    case 'Administrator PMB': $sidebarFile = 'admin_pmb'; break;
                    case 'Administrator Prodi': $sidebarFile = 'admin_prodi'; break;
                    case 'Dosen Belum-NIDN': $sidebarFile = 'dosen_non_nidn'; break;
                    case 'Pimpinan': $sidebarFile = 'pimpinan'; break;
                }
            ?>
            <?= $this->include('layouts/sidebars/' . $sidebarFile) ?>
        </aside>

        <!-- Main Content -->
        <main class="main-content">
            <!-- Topbar -->
            <header class="topbar">
                <div class="topbar-left">
                    <h3><?= $this->renderSection('title') ?></h3>
                </div>
                
                <div class="topbar-right">
                    <div style="position: relative; margin-right: 1.5rem; display: flex; align-items: center; color: var(--text-muted);">
                        <svg style="width: 24px; height: 24px;" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"></path></svg>
                        <span style="position:absolute; top:-3px; right:-3px; background:var(--danger); color:white; border-radius:50%; width:16px; height:16px; font-size:10px; display:flex; align-items:center; justify-content:center; font-weight:bold; border: 2px solid white;">3</span>
                    </div>
                    
                    <div class="user-profile" onclick="toggleDropdown()">
                        <div class="user-info-text" style="text-align: right;">
                            <span class="user-name"><?= session()->get('nama') ?></span>
                            <span class="user-role"><?= session()->get('role') ?></span>
                        </div>
                        <div class="avatar">
                            <?= strtoupper(substr(session()->get('nama') ?? 'U', 0, 1)) ?>
                        </div>
                        
                        <!-- Dropdown Menu -->
                        <div class="dropdown-menu" id="profileDropdown">
                            <a href="#" class="dropdown-item">
                                <svg fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path></svg>
                                Profil Saya
                            </a>
                            <a href="#" class="dropdown-item">
                                <svg fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
                                Pengaturan Akun
                            </a>
                            <a href="<?= base_url('logout') ?>" class="dropdown-item danger">
                                <svg fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"></path></svg>
                                Keluar (Logout)
                            </a>
                        </div>
                    </div>
                </div>
            </header>

            <!-- Dynamic Content Area -->
            <section class="content-area">
                <?= $this->renderSection('content') ?>
            </section>
        </main>
    </div>

    <!-- Script Dropdown & Sidebar Accordion -->
    <script>
        // Profile Dropdown Toggle
        function toggleDropdown() {
            document.getElementById('profileDropdown').classList.toggle('show');
        }

        window.onclick = function(event) {
            if (!event.target.closest('.user-profile')) {
                var dropdowns = document.getElementsByClassName("dropdown-menu");
                for (var i = 0; i < dropdowns.length; i++) {
                    var openDropdown = dropdowns[i];
                    if (openDropdown.classList.contains('show')) {
                        openDropdown.classList.remove('show');
                    }
                }
            }
        }

        // Sidebar Accordion Logic
        document.addEventListener('DOMContentLoaded', function() {
            const toggles = document.querySelectorAll('.submenu-toggle');
            toggles.forEach(toggle => {
                toggle.addEventListener('click', function(e) {
                    e.preventDefault();
                    
                    // Toggle 'open' class on the clicked toggle (rotates chevron)
                    this.classList.toggle('open');
                    
                    // Toggle 'open' class on the next sibling element (the ul.submenu)
                    const submenu = this.nextElementSibling;
                    if(submenu && submenu.classList.contains('submenu')) {
                        submenu.classList.toggle('open');
                    }
                });
            });
        });
    </script>
</body>
</html>
