<?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://niscoreapps.com.ng/nis_ams');
}

// Get user data
$userName = $_SESSION['full_name'] ?? 'User';
$userRole = $_SESSION['roles'][0] ?? 'Staff';
$userRoles = $_SESSION['roles'] ?? [];
$isSuperAdmin = isset($_SESSION['is_super_admin']) && $_SESSION['is_super_admin'] === true;

// Check specific roles - Direct checks
$isSuperAdminUser = $isSuperAdmin || in_array('Super Admin Officer', $userRoles);

// Role-based access flags
$showDashboard = $isSuperAdminUser;  // ONLY Super Admin sees Dashboard
$showAssetManagement = $isSuperAdminUser;  // ONLY Super Admin sees full Asset Management
$showFleetManagement = $isSuperAdminUser || in_array('Sectional Head Transport', $userRoles);
$showICTAssets = $isSuperAdminUser || in_array('Sectional Head ICT', $userRoles);
$showWeaponsManagement = $isSuperAdminUser || in_array('Armourer', $userRoles) || in_array('Command Armourer', $userRoles) || in_array('Command Approval Officer', $userRoles) || in_array('HQ Vetting Officer', $userRoles);
$showReports = $isSuperAdminUser;  // ONLY Super Admin sees Reports
$showAdmin = $isSuperAdminUser;  // ONLY Super Admin sees Admin

// For Requisition only access
$showRequisitionOnly = in_array('Command Armourer', $userRoles) || in_array('Command Approval Officer', $userRoles) || in_array('HQ Vetting Officer', $userRoles);
$showFullWeapons = $isSuperAdminUser || in_array('Armourer', $userRoles);

// Debug logging
error_log("=== SIDEBAR ROLE DEBUG ===");
error_log("User Roles: " . print_r($userRoles, true));
error_log("showICTAssets: " . ($showICTAssets ? 'YES' : 'NO'));
error_log("showFleetManagement: " . ($showFleetManagement ? 'YES' : 'NO'));
error_log("showWeaponsManagement: " . ($showWeaponsManagement ? 'YES' : 'NO'));
error_log("isSuperAdminUser: " . ($isSuperAdminUser ? 'YES' : 'NO'));
?>
<!-- Sidebar -->
<aside class="sidebar">
    
    <!-- Navigation Menu -->
    <nav class="sidebar-menu">
        <!-- Dashboard - ONLY Super Admin can see -->
        <?php if ($showDashboard): ?>
        <a href="<?php echo BASE_URL; ?>/dashboard" class="menu-item <?php echo ($active === 'dashboard') ? 'active' : ''; ?>">
            <i class="fas fa-tachometer-alt"></i>
            <span class="menu-text">Dashboard</span>
        </a>
        <?php endif; ?>
        
        <!-- Asset Management Dropdown - ONLY Super Admin -->
        <?php if ($showAssetManagement): ?>
        <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">
                <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>
                
                <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>
                
                <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>
                
                <a href="<?php echo BASE_URL; ?>/movable" class="sub-item <?php echo ($active === 'movable') ? 'active' : ''; ?>">
                    <i class="fas fa-tools"></i>
                    <span class="menu-text">Movable Assets</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/ict" class="sub-item <?php echo ($active === 'ict') ? 'active' : ''; ?>">
                    <i class="fas fa-server"></i>
                    <span class="menu-text">ICT Assets</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/projects" 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>
        <?php endif; ?>
        
        <!-- ICT Assets - Standalone menu for Sectional Head ICT (Non-Super Admin) -->
        <?php if ($showICTAssets && !$isSuperAdminUser): ?>
        <a href="<?php echo BASE_URL; ?>/ict" class="menu-item <?php echo ($active === 'ict') ? 'active' : ''; ?>">
            <i class="fas fa-server"></i>
            <span class="menu-text">ICT Assets</span>
        </a>
        <?php endif; ?>
        
        <!-- Fleet Management Dropdown - Super Admin OR Sectional Head Transport -->
        <?php if ($showFleetManagement): ?>
        <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">
                <a href="<?php echo BASE_URL; ?>/fleet/dashboard" class="sub-item <?php echo ($active === 'fleet-dashboard') ? 'active' : ''; ?>">
                    <i class="fas fa-tachometer-alt"></i>
                    <span class="menu-text">Fleet Dashboard</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/fleet/vehicles" class="sub-item <?php echo ($active === 'vehicles') ? 'active' : ''; ?>">
                    <i class="fas fa-car"></i>
                    <span class="menu-text">Vehicles</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/fleet/aircraft" class="sub-item <?php echo ($active === 'aircraft') ? 'active' : ''; ?>">
                    <i class="fas fa-helicopter"></i>
                    <span class="menu-text">Aircraft</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/fleet/marine" class="sub-item <?php echo ($active === 'marine') ? 'active' : ''; ?>">
                    <i class="fas fa-ship"></i>
                    <span class="menu-text">Marine</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/fleet/motorcycles" class="sub-item <?php echo ($active === 'motorcycles') ? 'active' : ''; ?>">
                    <i class="fas fa-motorcycle"></i>
                    <span class="menu-text">Motorcycles</span>
                </a>
            </div>
        </div>
        <?php endif; ?>
        
        <!-- Weapons Management Dropdown -->
        <?php if ($showWeaponsManagement): ?>
        <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">
                <!-- Full Weapons Management for Super Admin and Armourer -->
                <?php if ($showFullWeapons): ?>
                <a href="<?php echo BASE_URL; ?>/weapons" class="sub-item <?php echo ($active === 'weapons') ? 'active' : ''; ?>">
                    <i class="fas fa-clipboard-list"></i>
                    <span class="menu-text">Weapons Inventory</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/ammunition" class="sub-item <?php echo ($active === 'ammunition') ? 'active' : ''; ?>">
                    <i class="fas fa-bullseye"></i>
                    <span class="menu-text">Ammunition Inventory</span>
                </a>
                <?php endif; ?>
                                
                 <a href="<?php echo BASE_URL; ?>/non_lethal_weapons" class="sub-item <?php echo ($active === 'ammunition') ? 'active' : ''; ?>">
                                    <i class="fas fa-bullseye"></i>
                    <span class="menu-text">Non-lethal Inventory</span>
                </a>
                
                
                <!-- Requisition - Shown to everyone with weapons access -->
                <a href="<?php echo BASE_URL; ?>/requisition" class="sub-item <?php echo ($active === 'requisitions') ? 'active' : ''; ?>">
                    <i class="fas fa-file-alt"></i>
                    <span class="menu-text">Requisition</span>
                </a>
                
                <!-- Full Weapons Management continued (only for Armourer and Super Admin) -->
                <?php if ($showFullWeapons): ?>
                <a href="<?php echo BASE_URL; ?>/returns" class="sub-item <?php echo ($active === 'returns') ? 'active' : ''; ?>">
                    <i class="fas fa-undo-alt"></i>
                    <span class="menu-text">Returns</span>
                </a>
              
                <a href="<?php echo BASE_URL; ?>/weapon_issue" class="sub-item <?php echo ($active === 'weapon_issue') ? 'active' : ''; ?>">
                    <i class="fas fa-file-alt"></i>
                    <span class="menu-text">Weapon Issue</span>
                </a>

                <a href="<?php echo BASE_URL; ?>/audit/quarterly" 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>
        <?php endif; ?>
        
        <!-- Reports Dropdown - ONLY Super Admin -->
        <?php if ($showReports): ?>
        <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; ?>/reports" class="sub-item <?php echo ($active === 'reports') ? 'active' : ''; ?>">
                    <i class="fas fa-chart-line"></i>
                    <span class="menu-text">Reports Dashboard</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/reports/assets" 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; ?>/reports/weapons" class="sub-item <?php echo ($active === 'weapon-reports') ? 'active' : ''; ?>">
                    <i class="fas fa-gun"></i>
                    <span class="menu-text">Weapons Reports</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/reports/ammunition" class="sub-item <?php echo ($active === 'ammunition-reports') ? 'active' : ''; ?>">
                    <i class="fas fa-bullseye"></i>
                    <span class="menu-text">Ammunition Reports</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/reports/fleet" class="sub-item <?php echo ($active === 'fleet-reports') ? 'active' : ''; ?>">
                    <i class="fas fa-car"></i>
                    <span class="menu-text">Fleet Reports</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/reports/audit" class="sub-item <?php echo ($active === 'audit-reports') ? 'active' : ''; ?>">
                    <i class="fas fa-clipboard-check"></i>
                    <span class="menu-text">Audit Reports</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/reports/summary" class="sub-item <?php echo ($active === 'summary-reports') ? 'active' : ''; ?>">
                    <i class="fas fa-chart-bar"></i>
                    <span class="menu-text">Summary Reports</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/reports/saved" class="sub-item <?php echo ($active === 'saved-reports') ? 'active' : ''; ?>">
                    <i class="fas fa-save"></i>
                    <span class="menu-text">Saved Reports</span>
                </a>
            </div>
        </div>
        <?php endif; ?>
        
        <!-- Admin Dropdown - ONLY Super Admin -->
        <?php if ($showAdmin): ?>
        <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">
                <a href="<?php echo BASE_URL; ?>/users" class="sub-item <?php echo ($active === 'users') ? 'active' : ''; ?>">
                    <i class="fas fa-users"></i>
                    <span class="menu-text">User Management</span>
                </a>
                
                <a href="<?php echo BASE_URL; ?>/settings" class="sub-item <?php echo ($active === 'settings') ? 'active' : ''; ?>">
                    <i class="fas fa-cogs"></i>
                    <span class="menu-text">System Settings</span>
                </a>
            </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) return;
    
    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>