<?php
$current_page = basename($_SERVER['PHP_SELF']);

// Helper to check permissions for Staff accounts
function canAccess($perm) {
    // Super Admin has all permissions
    if (!isset($_SESSION['role']) || $_SESSION['role'] === 'admin') return true; 
    
    // Staff check
    $my_perms = isset($_SESSION['permissions']) ? json_decode($_SESSION['permissions'], true) : [];
    
    // If perms is null or empty string, treat as empty array
    if (!is_array($my_perms)) $my_perms = [];
    
    return in_array($perm, $my_perms);
}
?>

<div class="sidebar">
    <div style="text-align: center; margin-bottom: 20px;">
        <h2 style="color: white; margin: 0; font-size: 22px; letter-spacing: 1px;">Pro Subscription Offers</h2>
        <span style="font-size: 10px; color: #7f8c8d; text-transform: uppercase; letter-spacing: 2px;">Admin Panel</span>
    </div>
    
    <!-- DASHBOARD -->
    <a href="index.php" class="<?php echo $current_page == 'index.php' ? 'active' : ''; ?>">
        <i class="fas fa-tachometer-alt"></i> Dashboard
    </a>
    
    <?php if(canAccess('orders')): ?>
    <a href="analytics.php" class="<?php echo $current_page == 'analytics.php' ? 'active' : ''; ?>">
        <i class="fas fa-chart-pie"></i> Analytics
    </a>
    
    <div class="menu-label">Sales & Orders</div>
    
    <a href="orders.php" class="<?php echo $current_page == 'orders.php' ? 'active' : ''; ?>">
        <i class="fas fa-shopping-bag"></i> Manage Orders
    </a>
    
    <a href="vendors.php" class="<?php echo $current_page == 'vendors.php' ? 'active' : ''; ?>">
    <i class="fas fa-truck"></i> Vendors
</a>
    
    <?php endif; ?>

    <?php if(canAccess('products')): ?>
    <div class="menu-label">Inventory</div>
    
    <a href="products.php" class="<?php echo $current_page == 'products.php' ? 'active' : ''; ?>">
        <i class="fas fa-box"></i> All Products
    </a>
    <a href="categories.php" class="<?php echo $current_page == 'categories.php' ? 'active' : ''; ?>">
        <i class="fas fa-layer-group"></i> Categories
    </a>
    <a href="bundles.php" class="<?php echo $current_page == 'bundles.php' ? 'active' : ''; ?>">
        <i class="fas fa-cubes"></i> Bundles
    </a>
    <a href="keys.php" class="<?php echo $current_page == 'keys.php' ? 'active' : ''; ?>">
        <i class="fas fa-key"></i> License Keys
    </a>
    <a href="import_products.php" class="<?php echo $current_page == 'import_products.php' ? 'active' : ''; ?>">
        <i class="fas fa-file-import"></i> Bulk Import
    </a>
    <?php endif; ?>

    <?php if(canAccess('users')): ?>
    <div class="menu-label">User Management</div>
    
    <a href="users.php" class="<?php echo ($current_page == 'users.php' || $current_page == 'user_edit.php' || $current_page == 'user_history.php') ? 'active' : ''; ?>">
        <i class="fas fa-users"></i> Customers
    </a>
    <a href="resellers.php" class="<?php echo $current_page == 'resellers.php' ? 'active' : ''; ?>">
        <i class="fas fa-handshake"></i> Resellers
    </a>
    <a href="wallet_requests.php" class="<?php echo $current_page == 'wallet_requests.php' ? 'active' : ''; ?>">
        <i class="fas fa-wallet"></i> Wallet Deposits
    </a>
    <?php endif; ?>
    
    <?php if(canAccess('marketing')): ?>
    <div class="menu-label">Marketing</div>
    
    <a href="coupons.php" class="<?php echo $current_page == 'coupons.php' ? 'active' : ''; ?>">
        <i class="fas fa-tags"></i> Coupons
    </a>
    <a href="marketing.php" class="<?php echo $current_page == 'marketing.php' ? 'active' : ''; ?>">
        <i class="fas fa-bullhorn"></i> Email Blast
    </a>
    <a href="blog.php" class="<?php echo ($current_page == 'blog.php' || $current_page == 'blog_editor.php') ? 'active' : ''; ?>">
        <i class="fas fa-newspaper"></i> Blog Posts
    </a>
    <a href="requests.php" class="<?php echo $current_page == 'requests.php' ? 'active' : ''; ?>">
        <i class="fas fa-lightbulb"></i> User Requests
    </a>
    <?php endif; ?>

    <?php if(canAccess('support')): ?>
    <div class="menu-label">Support</div>
    
    <a href="support.php" class="<?php echo ($current_page == 'support.php' || $current_page == 'ticket_view.php') ? 'active' : ''; ?>">
        <i class="fas fa-headset"></i> Tickets
    </a>
    <a href="inbox.php" class="<?php echo ($current_page == 'inbox.php' || $current_page == 'reply_message.php') ? 'active' : ''; ?>">
        <i class="fas fa-envelope"></i> Inbox
    </a>
    <a href="reviews.php" class="<?php echo $current_page == 'reviews.php' ? 'active' : ''; ?>">
        <i class="fas fa-star"></i> Reviews
    </a>
    <a href="faqs.php" class="<?php echo $current_page == 'faqs.php' ? 'active' : ''; ?>">
        <i class="fas fa-question-circle"></i> FAQs
    </a>
    <?php endif; ?>

    <?php if(canAccess('settings')): ?>
    <div class="menu-label">System</div>
    
    <a href="reports.php" class="<?php echo $current_page == 'reports.php' ? 'active' : ''; ?>">
        <i class="fas fa-file-csv"></i> Export Data
    </a>
    <a href="logs.php" class="<?php echo $current_page == 'logs.php' ? 'active' : ''; ?>">
        <i class="fas fa-clipboard-list"></i> Activity Logs
    </a>
    <a href="settings.php" class="<?php echo $current_page == 'settings.php' ? 'active' : ''; ?>">
        <i class="fas fa-cogs"></i> Global Settings
    </a>
    <a href="staff.php" class="<?php echo $current_page == 'staff.php' ? 'active' : ''; ?>">
        <i class="fas fa-user-shield"></i> Staff Manager
    </a>
    <?php endif; ?>
    
    <div style="margin-top: 30px; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 15px;">
        <a href="../index.php" target="_blank" style="color: #3498db;"><i class="fas fa-globe"></i> View Website</a>
        <a href="../logout.php" style="color: #e74c3c;"><i class="fas fa-sign-out-alt"></i> Logout</a>
    </div>
</div>

<style>
    .menu-label {
        padding: 15px 20px 5px;
        font-size: 11px;
        text-transform: uppercase;
        color: #7f8c8d;
        font-weight: bold;
        letter-spacing: 0.5px;
    }
</style>