<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $title ?? 'Dashboard' ?> - Monitoring OPD</title>
    
    <link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin>
    <link rel="preconnect" href="https://cdnjs.cloudflare.com" crossorigin>
    <link rel="preload" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
    <noscript><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"></noscript>
    <link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
    <noscript><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet"></noscript>
    <?php if (!empty($loadCharts)): ?>
    <script src="https://cdn.jsdelivr.net/npm/chart.js" defer></script>
    <?php endif; ?>

    <style>
        :root {
            --sidebar-width: 260px;
            --sidebar-bg: #1a237e;
            --sidebar-bg-hover: rgba(255,255,255,0.1);
            --header-height: 70px;
            --primary-color: #1a237e;
            --text-color: #333;
            --bg-light: #f4f6f9;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: var(--bg-light);
            overflow-x: hidden;
        }
        
        /* Sidebar Styling */
        .sidebar {
            height: 100vh;
            position: fixed;
            top: 0;
            left: 0;
            width: var(--sidebar-width);
            background: linear-gradient(180deg, var(--sidebar-bg) 0%, #0d1245 100%);
            color: white;
            transition: all 0.3s ease-in-out;
            z-index: 1000;
            overflow-y: auto;
            box-shadow: 4px 0 10px rgba(0,0,0,0.1);
        }

        .sidebar.collapsed {
            margin-left: calc(var(--sidebar-width) * -1);
        }

        .sidebar-header {
            height: auto;
            min-height: var(--header-height);
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            padding: 20px;
            background-color: rgba(0,0,0,0.1);
            border-bottom: 1px solid rgba(255,255,255,0.05);
            text-align: center;
        }

        .sidebar-brand {
            font-weight: 700;
            font-size: 1.1rem;
            color: white;
            text-decoration: none;
            margin-left: 0;
            margin-top: 10px;
            letter-spacing: 0.5px;
            white-space: normal;
        }
        
        .sidebar-menu {
            padding: 20px 0;
            list-style: none;
            margin: 0;
        }

        .sidebar-menu li {
            margin-bottom: 5px;
        }

        .sidebar-menu li a {
            display: flex;
            align-items: center;
            padding: 12px 25px;
            color: rgba(255,255,255,0.75);
            text-decoration: none;
            transition: all 0.2s;
            border-left: 4px solid transparent;
            font-weight: 500;
            font-size: 0.95rem;
        }

        .sidebar-menu li a:hover {
            background-color: var(--sidebar-bg-hover);
            color: white;
            padding-left: 30px; /* Slide effect */
        }

        .sidebar-menu li a.active {
            background-color: rgba(255,255,255,0.15);
            color: white;
            border-left-color: #4facfe;
            box-shadow: inset 5px 0 10px rgba(0,0,0,0.1);
        }

        .sidebar-menu li a i {
            margin-right: 15px;
            width: 20px;
            text-align: center;
            font-size: 1.1rem;
        }

        .sidebar-divider {
            height: 1px;
            background-color: rgba(255,255,255,0.1);
            margin: 15px 25px;
        }

        .sidebar-heading {
            padding: 10px 25px;
            font-size: 0.7rem;
            text-transform: uppercase;
            color: rgba(255,255,255,0.4);
            font-weight: 700;
            letter-spacing: 1px;
        }

        /* Main Content Styling */
        .main-content {
            margin-left: var(--sidebar-width);
            padding: 20px 30px;
            transition: all 0.3s ease-in-out;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .main-content.expanded {
            margin-left: 0;
        }

        /* Navbar Styling */
        .top-navbar {
            background-color: white;
            box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05);
            padding: 0 25px;
            height: var(--header-height);
            border-radius: 15px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            position: sticky;
            top: 20px;
            z-index: 99;
        }

        .toggle-btn {
            background: none;
            border: none;
            font-size: 1.4rem;
            color: var(--primary-color);
            cursor: pointer;
            padding: 5px;
            transition: transform 0.2s;
            border-radius: 5px;
        }

        .toggle-btn:hover {
            background-color: rgba(26, 35, 126, 0.05);
            transform: scale(1.1);
        }

        .navbar-title {
            font-weight: 600;
            color: #555;
            font-size: 1.1rem;
        }

        .user-profile {
            display: flex;
            align-items: center;
            gap: 15px;
            cursor: pointer;
            padding: 5px 10px;
            border-radius: 10px;
            transition: background-color 0.2s;
        }

        .user-profile:hover {
            background-color: rgba(0,0,0,0.02);
        }

        .user-info {
            text-align: right;
            line-height: 1.2;
        }

        .user-name {
            font-weight: 700;
            color: var(--text-color);
            margin: 0;
            font-size: 0.9rem;
        }

        .user-role {
            font-size: 0.75rem;
            color: #888;
            margin: 0;
            text-transform: capitalize;
        }

        .user-avatar {
            width: 42px;
            height: 42px;
            background: linear-gradient(45deg, #1a237e, #534bae);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 1.1rem;
            box-shadow: 0 3px 6px rgba(0,0,0,0.15);
        }

        /* Content Card Styling */
        .card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.03);
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .card:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(0,0,0,0.05);
        }

        .card-header {
            background-color: white;
            border-bottom: 1px solid #f0f0f0;
            padding: 20px 25px;
            border-radius: 15px 15px 0 0 !important;
        }

        /* Responsive */
        @media (max-width: 992px) {
            .sidebar {
                margin-left: calc(var(--sidebar-width) * -1);
            }
            
            .sidebar.mobile-active {
                margin-left: 0;
            }

            .main-content {
                margin-left: 0;
            }

            .main-content.expanded {
                margin-left: 0; /* No change on mobile */
            }

            .navbar-title {
                display: none;
            }

            .overlay {
                display: none;
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: rgba(0,0,0,0.5);
                z-index: 999;
                backdrop-filter: blur(2px);
            }
            
            .overlay.active {
                display: block;
                animation: fadeIn 0.3s;
            }
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
    </style>
</head>
<body>

    <!-- Sidebar -->
    <nav class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <img src="<?= base_url('img/LOGO PEMDA.jpg.jpeg') ?>" alt="Logo" width="50" class="d-inline-block align-text-top rounded-circle shadow-sm" loading="lazy">
            <a href="<?= base_url('dashboard') ?>" class="sidebar-brand">Monitoring OPD</a>
        </div>
        
        <ul class="sidebar-menu">
            <li>
                <a href="<?= base_url('dashboard') ?>" class="<?= strpos(uri_string(), 'dashboard') !== false ? 'active' : '' ?>">
                    <i class="fas fa-tachometer-alt"></i> Dashboard
                </a>
            </li>

            <!-- Menu Super Admin -->
            <?php if (session()->get('role') == 'superadmin'): ?>
                <div class="sidebar-divider"></div>
                <div class="sidebar-heading">Master Data</div>
                <li><a href="<?= base_url('users') ?>" class="<?= strpos(uri_string(), 'users') !== false ? 'active' : '' ?>"><i class="fas fa-users"></i> Kelola User</a></li>
                <li><a href="<?= base_url('bidang') ?>" class="<?= strpos(uri_string(), 'bidang') !== false ? 'active' : '' ?>"><i class="fas fa-layer-group"></i> Daftar Bidang</a></li>
                <li><a href="<?= base_url('opd') ?>" class="<?= strpos(uri_string(), 'opd') !== false ? 'active' : '' ?>"><i class="fas fa-building"></i> Daftar OPD</a></li>
                <li><a href="<?= base_url('program') ?>" class="<?= strpos(uri_string(), 'program') !== false && strpos(uri_string(), 'program/import') === false ? 'active' : '' ?>"><i class="fas fa-list"></i> Data RENJA</a></li>
                <li><a href="<?= base_url('program/import') ?>" class="<?= strpos(uri_string(), 'program/import') !== false ? 'active' : '' ?>"><i class="fas fa-file-import"></i> Import RENJA</a></li>
                <li><a href="<?= base_url('jadwal') ?>" class="<?= strpos(uri_string(), 'jadwal') !== false ? 'active' : '' ?>"><i class="fas fa-calendar-alt"></i> Kelola Jadwal</a></li>
                
                <div class="sidebar-divider"></div>
                <div class="sidebar-heading">Laporan</div>
                <li><a href="<?= base_url('laporan/progres') ?>" class="<?= strpos(uri_string(), 'laporan/progres') !== false ? 'active' : '' ?>"><i class="fas fa-chart-line"></i> Progres Evaluasi</a></li>
                <li><a href="<?= base_url('laporan/ekspor') ?>" class="<?= strpos(uri_string(), 'laporan/ekspor') !== false ? 'active' : '' ?>"><i class="fas fa-file-export"></i> Ekspor Laporan</a></li>
            <?php endif; ?>

            <!-- Menu Admin Bidang -->
            <?php if (session()->get('role') == 'admin_bidang'): ?>
                <div class="sidebar-divider"></div>
                <div class="sidebar-heading">Manajemen Bidang</div>
                <li><a href="<?= base_url('admin_bidang/renja') ?>" class="<?= strpos(uri_string(), 'admin_bidang/renja') !== false ? 'active' : '' ?>"><i class="fas fa-calendar-check"></i> Rencana Kerja OPD</a></li>
                <li><a href="<?= base_url('admin_bidang/input_progres') ?>" class="<?= strpos(uri_string(), 'admin_bidang/input_progres') !== false ? 'active' : '' ?>"><i class="fas fa-clipboard-list"></i> Input Progres OPD</a></li>
                <li><a href="<?= base_url('admin_bidang/verifikasi') ?>" class="<?= strpos(uri_string(), 'admin_bidang/verifikasi') !== false ? 'active' : '' ?>"><i class="fas fa-check-double"></i> Verifikasi Laporan</a></li>
            <?php endif; ?>

            <!-- Menu OPD -->
            <?php if (session()->get('role') == 'opd'): ?>
                <div class="sidebar-divider"></div>
                <div class="sidebar-heading">Kegiatan OPD</div>
                <li><a href="<?= base_url('opd_kinerja/input') ?>" class="<?= strpos(uri_string(), 'opd_kinerja/input') !== false ? 'active' : '' ?>"><i class="fas fa-edit"></i> Input Laporan</a></li>
                <li><a href="<?= base_url('opd_kinerja/riwayat') ?>" class="<?= strpos(uri_string(), 'opd_kinerja/riwayat') !== false ? 'active' : '' ?>"><i class="fas fa-history"></i> Riwayat Laporan</a></li>
            <?php endif; ?>

            <div class="sidebar-divider"></div>
            <li>
                <a href="<?= base_url('login/logout') ?>" class="text-danger">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a>
            </li>
        </ul>
    </nav>

    <!-- Overlay for mobile -->
    <div class="overlay" id="overlay"></div>

    <!-- Main Content -->
    <div class="main-content" id="mainContent">
        <!-- Top Navbar -->
        <nav class="top-navbar">
            <div class="d-flex align-items-center">
                <button class="toggle-btn me-3" id="sidebarToggle">
                    <i class="fas fa-bars"></i>
                </button>
                <h5 class="m-0 navbar-title">Sistem Informasi Evaluasi RENJA Triwulan</h5>
            </div>
            
            <div class="d-flex align-items-center">
                <!-- Notification Bell -->
                <ul class="navbar-nav me-3">
                    <?= view_cell('App\Cells\NotificationCell::render') ?>
                </ul>

                <div class="user-profile">
                    <div class="user-info d-none d-sm-block">
                        <p class="user-name"><?= session()->get('username') ?></p>
                        <p class="user-role"><?= str_replace('_', ' ', session()->get('role')) ?></p>
                    </div>
                    <div class="user-avatar ms-2">
                        <?= strtoupper(substr(session()->get('username'), 0, 1)) ?>
                    </div>
                </div>
            </div>
        </nav>

        <!-- Content Body -->
        <div class="content-body flex-grow-1">
            <?= $this->renderSection('content') ?>
        </div>

        <footer class="mt-5 text-center text-muted small py-3">
            &copy; 2026 Pemerintah Daerah. All rights reserved.
        </footer>
    </div>

    <!-- Scripts -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" defer></script>
    <script>
        // Sidebar Toggle Script
        const sidebar = document.getElementById('sidebar');
        const mainContent = document.getElementById('mainContent');
        const overlay = document.getElementById('overlay');
        const sidebarToggle = document.getElementById('sidebarToggle');

        sidebarToggle.addEventListener('click', function() {
            if (window.innerWidth > 992) {
                // Desktop behavior: Toggle collapsed class
                sidebar.classList.toggle('collapsed');
                mainContent.classList.toggle('expanded');
            } else {
                // Mobile behavior: Toggle mobile-active class and overlay
                sidebar.classList.toggle('mobile-active');
                overlay.classList.toggle('active');
            }
        });

        // Close sidebar when clicking overlay (mobile only)
        overlay.addEventListener('click', function() {
            sidebar.classList.remove('mobile-active');
            overlay.classList.remove('active');
        });
        
        // Handle window resize to reset states if needed
        window.addEventListener('resize', function() {
            if (window.innerWidth > 992) {
                overlay.classList.remove('active');
                sidebar.classList.remove('mobile-active');
            }
        });
    </script>
</body>
</html>
