<?php
/**
 * Shared role sidebar renderer for admin, pelaksana, and teknisi dashboards.
 * Usage:
 *   include "../partials/sidebar_menu.php";
 *   komplain_render_sidebar([
 *     'role' => 'admin',
 *     'home' => 'dashboard.php',
 *     'active' => 'dashboard',
 *     'profileUser' => $profileUser,
 *     'notifCount' => $notifCount,
 *   ]);
 */

if (!function_exists('komplain_render_sidebar')) {
    function komplain_render_sidebar(array $cfg): void
    {
        $role = strtolower(trim((string) ($cfg['role'] ?? 'admin')));
        $home = (string) ($cfg['home'] ?? 'dashboard.php');
        $active = strtolower(trim((string) ($cfg['active'] ?? 'dashboard')));
        $profileUser = $cfg['profileUser'] ?? null;
        $notifCount = (int) ($cfg['notifCount'] ?? 0);

        $menus = [
            'dashboard' => [
                'href' => $home,
                'icon' => 'fas fa-home',
                'label' => 'Dashboard',
            ],
            'data_komplain' => [
                'href' => 'data_komplain.php?filter=total',
                'icon' => 'fas fa-table',
                'label' => 'Data Komplain',
            ],
            'absen' => [
                'href' => $role === 'admin' ? 'absen.php' : 'absen.php',
                'icon' => $role === 'admin' ? 'fas fa-user-check' : 'fas fa-camera',
                'label' => $role === 'admin' ? 'Absen Saya' : ($role === 'teknisi' ? 'Absen' : 'Absen'),
            ],
            'data_absensi' => [
                'href' => 'data_absensi.php',
                'icon' => 'fas fa-users',
                'label' => 'Data Absensi',
            ],
            'gaji' => [
                'href' => 'gaji.php',
                'icon' => 'fas fa-money-bill-wave',
                'label' => 'Gaji',
            ],
            'users' => [
                'href' => 'users.php',
                'icon' => 'fas fa-user-cog',
                'label' => 'Akses User',
            ],
            'profil' => [
                'href' => 'profil.php',
                'icon' => 'fas fa-user-circle',
                'label' => 'Profil Pengguna',
            ],
            'notif' => [
                'href' => 'notif.php',
                'icon' => 'fas fa-bell',
                'label' => 'Notifikasi',
            ],
            'cetak_pdf' => [
                'href' => 'cetak_pdf.php',
                'icon' => 'fas fa-print',
                'label' => 'Cetak PDF',
            ],
            'logout' => [
                'href' => '../middleware/logout.php',
                'icon' => 'fas fa-sign-out-alt',
                'label' => 'Logout',
            ],
        ];

        if ($role === 'admin') {
            $menus['input_komplain'] = [
                'href' => 'input_komplain_admin.php',
                'icon' => 'fas fa-plus',
                'label' => 'Input Komplain',
            ];
        }

        ?>
<aside class="main-sidebar sidebar-dark-primary elevation-4">
    <a href="<?= htmlspecialchars($home) ?>" class="brand-link text-center">PT. ALVINDO</a>
    <div class="sidebar">
        <div class="sidebar-profile">
            <?php if (is_array($profileUser) && !empty($profileUser)): ?>
                <img src="<?= komplain_profile_photo_url($profileUser) ?>" alt="Foto profil">
                <div class="mt-2 font-weight-bold"><?= htmlspecialchars((string) ($profileUser['username'] ?? '')) ?></div>
                <small><?= htmlspecialchars(ucfirst((string) ($profileUser['role'] ?? $role))) ?></small>
            <?php endif; ?>
        </div>
        <ul class="nav nav-pills nav-sidebar flex-column">
            <?php foreach ($menus as $key => $menu): ?>
                <li class="nav-item">
                    <a href="<?= htmlspecialchars($menu['href']) ?>" class="nav-link<?= $active === $key ? ' active' : '' ?>">
                        <i class="nav-icon <?= htmlspecialchars($menu['icon']) ?>"></i>
                        <?= htmlspecialchars($menu['label']) ?>
                        <?php if ($key === 'notif' && $notifCount > 0): ?>
                            <span class="badge badge-warning float-right"><?= $notifCount ?></span>
                        <?php endif; ?>
                    </a>
                </li>
            <?php endforeach; ?>
            <li class="nav-item mt-2 px-3">
                <button type="button" class="btn sidebar-theme-toggle" data-theme-toggle>
                    <i data-theme-toggle-icon class="fa fa-adjust"></i>
                    <span data-theme-toggle-label>Mode Siang</span>
                </button>
            </li>
        </ul>
    </div>
</aside>
        <?php
    }
}
