<?php
/**
 * Sidebar Layout with Logo
 * 
 * Variables expected:
 * $active - Active menu item
 * $user - Current user data
 */

// Ensure Auth class exists
if (!class_exists('Auth')) {
    class Auth {
        public static function check() { return isset($_SESSION['user_id']); }
        public static function can($perm) { return true; }
        public static function canAny($perms) { return true; }
    }
}

// Ensure BASE_URL is defined
if (!defined('BASE_URL')) {
    define('BASE_URL', 'https://nis-migrant.com.ng/nis_ams');
}

// Get user data
$userName = $_SESSION['full_name'] ?? 'User';
$userRole = $_SESSION['roles'][0] ?? 'Staff';
$isSuperAdmin = isset($_SESSION['is_super_admin']) && $_SESSION['is_super_admin'] === true;
?>
<!-- Sidebar -->
<aside class="sidebar">
    
    

    <!-- Navigation Menu -->
    <nav class="sidebar-menu">
        <!-- Dashboard -->
        <a href="<?php echo BASE_URL; ?>/dashboard.php" class="menu-item <?php echo ($active === 'dashboard') ? 'active' : ''; ?>">
            <i class="fas fa-tachometer-alt"></i>
            <span class="menu-text">Dashboard</span>
        </a>
        
        <!-- Asset Management Dropdown -->
        <div class="dropdown-group">
            <div class="menu-item dropdown-toggle" onclick="toggleDropdown('assetDropdown')">
                <i class="fas fa-boxes"></i>
                <span class="menu-text">Asset Management</span>
                <i class="fas fa-chevron-right arrow"></i>
            </div>
            <div class="dropdown-items" id="assetDropdown">
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('land.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/land" class="sub-item <?php echo ($active === 'land') ? 'active' : ''; ?>">
                    <i class="fas fa-map-marked-alt"></i>
                    <span class="menu-text">Land Assets</span>
                </a>
                <?php endif; ?>
                
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('building.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/buildings" class="sub-item <?php echo ($active === 'buildings') ? 'active' : ''; ?>">
                    <i class="fas fa-building"></i>
                    <span class="menu-text">Building Assets</span>
                </a>
                <?php endif; ?>
                
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('rented.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/rented" class="sub-item <?php echo ($active === 'rented') ? 'active' : ''; ?>">
                    <i class="fas fa-house-user"></i>
                    <span class="menu-text">Rented Properties</span>
                </a>
                <?php endif; ?>
                
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('movable.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/views/movable/index.php" class="sub-item <?php echo ($active === 'movable') ? 'active' : ''; ?>">
                    <i class="fas fa-tools"></i>
                    <span class="menu-text">Movable Assets</span>
                </a>
                <?php endif; ?>
                
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('ict.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/views/ict/index.php" class="sub-item <?php echo ($active === 'ict') ? 'active' : ''; ?>">
                    <i class="fas fa-server"></i>
                    <span class="menu-text">ICT Assets</span>
                </a>
                <?php endif; ?>
                
                <a href="<?php echo BASE_URL; ?>/views/projects/index.php" class="sub-item <?php echo ($active === 'projects') ? 'active' : ''; ?>">
                    <i class="fas fa-hard-hat"></i>
                    <span class="menu-text">Ongoing Projects</span>
                </a>
            </div>
        </div>
        
        <!-- Fleet Management Dropdown - FIXED VERSION -->
        <div class="dropdown-group">
            <div class="menu-item dropdown-toggle" onclick="toggleDropdown('fleetDropdown')">
                <i class="fas fa-car"></i>
                <span class="menu-text">Fleet Management</span>
                <i class="fas fa-chevron-right arrow"></i>
            </div>
            <div class="dropdown-items" id="fleetDropdown">
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('fleet.view'))): ?>
                <!-- Fleet Dashboard -->
                <a href="<?php echo BASE_URL; ?>/views/fleet/dashboard.php" class="sub-item <?php echo ($active === 'fleet-dashboard') ? 'active' : ''; ?>">
                    <i class="fas fa-tachometer-alt"></i>
                    <span class="menu-text">Fleet Dashboard</span>
                </a>
                
                <!-- Vehicles -->
                <a href="<?php echo BASE_URL; ?>/views/fleet/vehicles/index.php" class="sub-item <?php echo ($active === 'vehicles') ? 'active' : ''; ?>">
                    <i class="fas fa-car"></i>
                    <span class="menu-text">Vehicles</span>
                </a>
                
                <!-- Aircraft -->
                <a href="<?php echo BASE_URL; ?>/views/fleet/aircraft/index.php" class="sub-item <?php echo ($active === 'aircraft') ? 'active' : ''; ?>">
                    <i class="fas fa-helicopter"></i>
                    <span class="menu-text">Aircraft</span>
                </a>
                
                <!-- Marine -->
                <a href="<?php echo BASE_URL; ?>/views/fleet/marine/index.php" class="sub-item <?php echo ($active === 'marine') ? 'active' : ''; ?>">
                    <i class="fas fa-ship"></i>
                    <span class="menu-text">Marine</span>
                </a>
                
                <!-- Motorcycles -->
                <a href="<?php echo BASE_URL; ?>/views/fleet/motorcycles/index.php" class="sub-item <?php echo ($active === 'motorcycles') ? 'active' : ''; ?>">
                    <i class="fas fa-motorcycle"></i>
                    <span class="menu-text">Motorcycles</span>
                </a>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- Weapons Management Dropdown -->
        <div class="dropdown-group">
            <div class="menu-item dropdown-toggle" onclick="toggleDropdown('weaponDropdown')">
                <i class="fas fa-gun"></i>
                <span class="menu-text">Weapons Management</span>
                <i class="fas fa-chevron-right arrow"></i>
            </div>
            <div class="dropdown-items" id="weaponDropdown">
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('weapons.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/views/weapons/index.php" class="sub-item <?php echo ($active === 'weapons') ? 'active' : ''; ?>">
                    <i class="fas fa-clipboard-list"></i>
                    <span class="menu-text">Weapons Inventory</span>
                </a>
                <?php endif; ?>
                
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('ammunition.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/views/ammunition/index.php" class="sub-item <?php echo ($active === 'ammunition') ? 'active' : ''; ?>">
                    <i class="fas fa-bullseye"></i>
                    <span class="menu-text">Ammunition</span>
                </a>
                <?php endif; ?>
                
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('requisition.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/views/requisition/index.php" class="sub-item <?php echo ($active === 'requisitions') ? 'active' : ''; ?>">
                    <i class="fas fa-file-alt"></i>
                    <span class="menu-text">Requisition</span>
                </a>
                <?php endif; ?>
                
                <a href="<?php echo BASE_URL; ?>/views/returns/index.php" class="sub-item <?php echo ($active === 'returns') ? 'active' : ''; ?>">
                    <i class="fas fa-undo-alt"></i>
                    <span class="menu-text">Returns</span>
                </a>
              
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('weapon_issue.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/views/weapon_issue/index.php" class="sub-item <?php echo ($active === 'weapon_issue') ? 'active' : ''; ?>">
                    <i class="fas fa-file-alt"></i>
                    <span class="menu-text">Weapon Issue</span>
                </a>
                <?php endif; ?>

                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('audit.view'))): ?>
                <a href="<?php echo BASE_URL; ?>/views/audit/quarterly/index.php" class="sub-item <?php echo ($active === 'audit') ? 'active' : ''; ?>">
                    <i class="fas fa-clipboard-check"></i>
                    <span class="menu-text">Quarterly Audit</span>
                </a>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- Reports Dropdown -->
        <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('reports.view'))): ?>
        <div class="dropdown-group">
            <div class="menu-item dropdown-toggle" onclick="toggleDropdown('reportsDropdown')">
                <i class="fas fa-chart-bar"></i>
                <span class="menu-text">Reports</span>
                <i class="fas fa-chevron-right arrow"></i>
            </div>
            <div class="dropdown-items" id="reportsDropdown">
                <a href="<?php echo BASE_URL; ?>/views/reports/index.php" class="sub-item <?php echo ($active === 'asset-reports') ? 'active' : ''; ?>">
                    <i class="fas fa-chart-pie"></i>
                    <span class="menu-text">Asset Reports</span>
                </a>
                <a href="<?php echo BASE_URL; ?>/views/reports/weapons.php" class="sub-item <?php echo ($active === 'weapon-reports') ? 'active' : ''; ?>">
                    <i class="fas fa-gun"></i>
                    <span class="menu-text">Weapons Dashboard</span>
                </a>
                <a href="<?php echo BASE_URL; ?>/views/audit/history.php" class="sub-item <?php echo ($active === 'audit-history') ? 'active' : ''; ?>">
                    <i class="fas fa-history"></i>
                    <span class="menu-text">Audit History</span>
                </a>
            </div>
        </div>
        <?php endif; ?>
        
        <!-- Admin Dropdown -->
        <?php if ($isSuperAdmin || (class_exists('Auth') && (Auth::can('users.manage') || Auth::can('settings.manage')))): ?>
        <div class="dropdown-group">
            <div class="menu-item dropdown-toggle" onclick="toggleDropdown('adminDropdown')">
                <i class="fas fa-users-cog"></i>
                <span class="menu-text">Admin</span>
                <i class="fas fa-chevron-right arrow"></i>
            </div>
            <div class="dropdown-items" id="adminDropdown">
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('users.manage'))): ?>
                <a href="<?php echo BASE_URL; ?>/views/users/index.php" class="sub-item <?php echo ($active === 'users') ? 'active' : ''; ?>">
                    <i class="fas fa-users"></i>
                    <span class="menu-text">User Management</span>
                </a>
                <?php endif; ?>
                
                <?php if ($isSuperAdmin || (class_exists('Auth') && Auth::can('settings.manage'))): ?>
                <a href="<?php echo BASE_URL; ?>/views/settings/index.php" class="sub-item <?php echo ($active === 'settings') ? 'active' : ''; ?>">
                    <i class="fas fa-cogs"></i>
                    <span class="menu-text">System Settings</span>
                </a>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
    </nav>
</aside>

<!-- Main Content Wrapper -->
<main class="main-content">

<script>
// Toggle dropdown function
function toggleDropdown(id) {
    var dropdown = document.getElementById(id);
    var toggle = event.currentTarget;
    
    if (dropdown.style.display === 'block') {
        dropdown.style.display = 'none';
        toggle.classList.remove('open');
    } else {
        dropdown.style.display = 'block';
        toggle.classList.add('open');
    }
}

// Auto-expand dropdown if any child is active
document.addEventListener('DOMContentLoaded', function() {
    // Auto-expand dropdown if any child is active
    var activeItems = document.querySelectorAll('.sub-item.active');
    activeItems.forEach(function(item) {
        var dropdown = item.closest('.dropdown-items');
        if (dropdown) {
            dropdown.style.display = 'block';
            var toggle = document.querySelector('[onclick*="' + dropdown.id + '"]');
            if (toggle) toggle.classList.add('open');
        }
    });

    // User Profile Dropdown
    const adminProfileBtn = document.getElementById('adminProfileBtn');
    const adminProfileMenu = document.getElementById('adminProfileMenu');
    
    if (adminProfileBtn && adminProfileMenu) {
        adminProfileBtn.addEventListener('click', function(e) {
            e.stopPropagation();
            adminProfileBtn.classList.toggle('active');
            adminProfileMenu.classList.toggle('active');
        });
        
        document.addEventListener('click', function(e) {
            if (!adminProfileBtn.contains(e.target) && !adminProfileMenu.contains(e.target)) {
                adminProfileBtn.classList.remove('active');
                adminProfileMenu.classList.remove('active');
            }
        });
    }
    
    // Mobile menu toggle
    const mobileMenuToggle = document.getElementById('mobileMenuToggle');
    const sidebar = document.querySelector('.sidebar');
    const sidebarOverlay = document.getElementById('sidebarOverlay');
    
    if (mobileMenuToggle && sidebar && sidebarOverlay) {
        mobileMenuToggle.addEventListener('click', function(e) {
            e.stopPropagation();
            sidebar.classList.toggle('active');
            sidebarOverlay.classList.toggle('active');
        });
        
        sidebarOverlay.addEventListener('click', function() {
            sidebar.classList.remove('active');
            sidebarOverlay.classList.remove('active');
        });
    }
});
</script>