<?php
$activeMenu = $activeMenu ?? '';
$userName = $userName ?? session()->get('userName');
$userEmail = $userEmail ?? session()->get('userEmail');
$userRole = $userRole ?? session()->get('userRole');
$appSetting = function_exists('app_setting') ? app_setting() : [];
$appName = trim((string) ($appSetting['nama_aplikasi'] ?? 'Aplikasi Admin')) ?: 'Aplikasi Admin';
$appTagline = trim((string) ($appSetting['tagline'] ?? '')) ?: 'Admin Panel';
$appLogo = $appSetting['logo'] ?? null;
$roleLabel = match ($userRole ?? '') {
    'administrator' => 'Administrator',
    'kasir' => 'Kasir',
    default => ucfirst((string) ($userRole ?? '-')),
};
$isAdministrator = ($userRole ?? '') === 'administrator';
$isKasir = ($userRole ?? '') === 'kasir';
$isOperationalRole = $isAdministrator || $isKasir;
$userPhoto = session()->get('userPhoto');
$initial = strtoupper(substr((string) ($userName ?? '-'), 0, 1));
?>

<aside class="sidebar" id="appSidebar">
    <div class="brand-box">
        <div class="d-flex align-items-center gap-3">
            <?php if (! empty($appLogo)) : ?>
                <img src="<?= base_url('uploads/settings/' . $appLogo) ?>" alt="Logo <?= esc($appName) ?>" class="brand-logo">
            <?php else : ?>
                <div class="brand-mark">
                    <?= esc(strtoupper(substr($appName, 0, 1))) ?>
                </div>
            <?php endif; ?>

            <div class="min-w-0 brand-text">
                <div class="fw-bold text-truncate"><?= esc($appName) ?></div>
                <div class="small text-white-50 text-truncate">Admin Panel</div>
            </div>
        </div>

        <button class="btn btn-sm btn-light mobile-menu-button" type="button" id="sidebarToggle">
            <i class="bi bi-list"></i>
        </button>
    </div>

      <div class="sidebar-footer p-2 d-none d-lg-block">
            <div class="d-flex align-items-center gap-2 min-w-0">
                <?php if (! empty($userPhoto)) : ?>
                    <img src="<?= base_url('uploads/users/' . $userPhoto) ?>" alt="Foto <?= esc($userName ?? '-') ?>" class="sidebar-user-photo" style="width: 34px; height: 34px;">
                <?php else : ?>
                    <div class="sidebar-user-photo-placeholder avatar" style="width: 34px; height: 34px; font-size: .8rem;"><?= esc($initial) ?></div>
                <?php endif; ?>
                <div class="min-w-0">
                    <div class="fw-semibold small text-white text-truncate"><?= esc($userName ?? '-') ?></div>
                    <div class="small text-white-50 text-truncate" style="font-size: .72rem;"><?= esc($roleLabel) ?></div>
                </div>
            </div>
        </div>
    
    <div class="sidebar-menu-wrap" id="sidebarMenu">


        <div class="menu-label">Menu Utama</div>
        <nav class="sidebar-menu">
            <a href="<?= site_url('admin/dashboard') ?>" class="sidebar-link <?= $activeMenu === 'dashboard' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Dashboard">
                <i class="bi bi-grid-1x2-fill"></i>
                <span>Dashboard</span>
            </a>
        </nav>

        <?php if ($isOperationalRole) : ?>
            <div class="menu-label">Master Data</div>
            <nav class="sidebar-menu">
                <?php if ($isAdministrator) : ?>
                    <a href="<?= site_url('admin/kategori') ?>" class="sidebar-link <?= $activeMenu === 'kategori' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Data Kategori">
                        <i class="bi bi-tags-fill"></i>
                        <span>Data Kategori</span>
                    </a>
                <?php endif; ?>
                <a href="<?= site_url('admin/barang') ?>" class="sidebar-link <?= $activeMenu === 'barang' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Data Barang">
                    <i class="bi bi-box-seam-fill"></i>
                    <span>Data Barang</span>
                </a>
                <?php if ($isAdministrator) : ?>
                    <a href="<?= site_url('admin/jasa') ?>" class="sidebar-link <?= $activeMenu === 'jasa' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Data Jasa">
                        <i class="bi bi-tools"></i>
                        <span>Data Jasa</span>
                    </a>
                    <a href="<?= site_url('admin/suplier') ?>" class="sidebar-link <?= $activeMenu === 'suplier' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Data Suplier">
                        <i class="bi bi-truck"></i>
                        <span>Data Suplier</span>
                    </a>
                    <a href="<?= site_url('admin/teknisi') ?>" class="sidebar-link <?= $activeMenu === 'teknisi' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Data Teknisi">
                        <i class="bi bi-person-gear"></i>
                        <span>Data Teknisi</span>
                    </a>
                    <a href="<?= site_url('admin/pelanggan') ?>" class="sidebar-link <?= $activeMenu === 'pelanggan' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Data Pelanggan">
                        <i class="bi bi-person-vcard"></i>
                        <span>Data Pelanggan</span>
                    </a>
                <?php endif; ?>
            </nav>
        <?php endif; ?>

        <?php if ($isOperationalRole) : ?>
            <div class="menu-label">Transaksi</div>
            <nav class="sidebar-menu">
                <?php if ($isAdministrator) : ?>
                    <a href="<?= site_url('admin/pembelian') ?>" class="sidebar-link <?= $activeMenu === 'pembelian' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Pembelian">
                        <i class="bi bi-box-arrow-in-down"></i>
                        <span>Pembelian</span>
                    </a>
                <?php endif; ?>
                <a href="<?= site_url('admin/penjualan') ?>" class="sidebar-link <?= $activeMenu === 'penjualan' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Penjualan">
                    <i class="bi bi-cart-check"></i>
                    <span>Penjualan</span>
                </a>
                <a href="<?= site_url('admin/service') ?>" class="sidebar-link <?= $activeMenu === 'service' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Service">
                    <i class="bi bi-wrench-adjustable"></i>
                    <span>Service</span>
                </a>
                <?php if ($isAdministrator) : ?>
                    <a href="<?= site_url('admin/stok-penyesuaian') ?>" class="sidebar-link <?= $activeMenu === 'stok-penyesuaian' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Penyesuaian Stok">
                        <i class="bi bi-sliders"></i>
                        <span>Penyesuaian Stok</span>
                    </a>
                <?php endif; ?>
            </nav>
        <?php endif; ?>

        <?php if ($isAdministrator) : ?>
            <div class="menu-label">Laporan</div>
            <nav class="sidebar-menu">
                <a href="<?= site_url('admin/laporan/stok') ?>" class="sidebar-link <?= $activeMenu === 'laporan-stok' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Laporan Stok Barang">
                    <i class="bi bi-clipboard-data-fill"></i>
                    <span>Laporan Stok</span>
                </a>
                <a href="<?= site_url('admin/laporan/pembelian') ?>" class="sidebar-link <?= $activeMenu === 'laporan-pembelian' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Laporan Pembelian">
                    <i class="bi bi-journal-arrow-down"></i>
                    <span>Lap. Pembelian</span>
                </a>
                <a href="<?= site_url('admin/laporan/penjualan') ?>" class="sidebar-link <?= $activeMenu === 'laporan-penjualan' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Laporan Penjualan">
                    <i class="bi bi-journal-arrow-up"></i>
                    <span>Lap. Penjualan</span>
                </a>
                <a href="<?= site_url('admin/laporan/service') ?>" class="sidebar-link <?= $activeMenu === 'laporan-service' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Laporan Service">
                    <i class="bi bi-journal-medical"></i>
                    <span>Lap. Service</span>
                </a>
                <a href="<?= site_url('admin/laporan/penyesuaian-stok') ?>" class="sidebar-link <?= $activeMenu === 'laporan-penyesuaian-stok' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Laporan Penyesuaian Stok">
                    <i class="bi bi-journal-check"></i>
                    <span>Lap. Penyesuaian</span>
                </a>
            </nav>
        <?php endif; ?>
      

        <div class="menu-label">Akun</div>
        <nav class="sidebar-menu">
            <?php if ($isAdministrator) : ?>
                <a href="<?= site_url('admin/users') ?>" class="sidebar-link <?= $activeMenu === 'users' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Data User">
                    <i class="bi bi-people-fill"></i>
                    <span>Data User</span>
                </a>
            <?php endif; ?>
            <a href="<?= site_url('admin/profile') ?>" class="sidebar-link <?= $activeMenu === 'profile' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Profil">
                <i class="bi bi-person-circle"></i>
                <span>Profil</span>
            </a>
            <a href="<?= site_url('admin/change-password') ?>" class="sidebar-link <?= $activeMenu === 'change-password' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Ganti Password">
                <i class="bi bi-key-fill"></i>
                <span>Ganti Password</span>
            </a>
            <?php if ($isAdministrator) : ?>
                <a href="<?= site_url('admin/setting') ?>" class="sidebar-link <?= $activeMenu === 'setting' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Setting">
                    <i class="bi bi-gear-fill"></i>
                    <span>Setting</span>
                </a>
                <a href="<?= site_url('admin/backup') ?>" class="sidebar-link <?= $activeMenu === 'backup' ? 'active' : '' ?>" data-bs-toggle="tooltip" data-bs-placement="right" title="Backup Database">
                    <i class="bi bi-database-down"></i>
                    <span>Backup Database</span>
                </a>
            <?php endif; ?>
            <a href="<?= site_url('logout') ?>" class="sidebar-link" data-bs-toggle="tooltip" data-bs-placement="right" title="Logout">
                <i class="bi bi-box-arrow-right"></i>
                <span>Logout</span>
            </a>
        </nav>

      
    </div>
</aside>
