<?php
// includes/admin_sidebar.php
$pending_recharge = $db->query("SELECT COUNT(*) FROM transactions WHERE type IN ('topup','drive') AND status='pending'")->fetchColumn();
$pending_deposits = $db->query("SELECT COUNT(*) FROM deposits WHERE status='pending'")->fetchColumn();
$current_page = basename($_SERVER['PHP_SELF']);

// Dynamic colors
$_ap = getSetting('color_primary')   ?: '#1a3c8f';
$_as = getSetting('color_secondary') ?: '#f5a623';
$_ad = getSetting('color_dark')      ?: '#0d1f4e';

// Site name two colors
$_sn = defined('SITE_NAME') ? SITE_NAME : (getSetting('site_name') ?: 'Priyo Telecom');
$_snParts = explode(' ', trim($_sn), 2);
?>
<style>
:root{--primary:<?= $_ap ?> !important;--secondary:<?= $_as ?> !important;--dark:<?= $_ad ?> !important}
</style>
<div class="sidebar-overlay" id="sidebarOverlay" onclick="closeSidebar()"></div>

<nav class="sidebar" id="sidebar">
    <div class="sidebar-logo">
        <div class="sidebar-logo-inner">
            <div>
                <h2><?= htmlspecialchars($_snParts[0]) ?><?php if(isset($_snParts[1])): ?> <span style="color:<?= $_as ?>"><?= htmlspecialchars($_snParts[1]) ?></span><?php endif; ?></h2>
                <p>Admin Control Panel</p>
            </div>
            <button class="sidebar-close" onclick="closeSidebar()"><i class="fas fa-times"></i></button>
        </div>
    </div>

    <div class="nav-label">Main</div>
    <a href="index.php" class="nav-item <?= $current_page==='index.php'?'active':'' ?>"><i class="fas fa-tachometer-alt"></i> Dashboard</a>
    <a href="users.php" class="nav-item <?= $current_page==='users.php'?'active':'' ?>"><i class="fas fa-users"></i> Users</a>
    <a href="resellers.php" class="nav-item <?= $current_page==='resellers.php'?'active':'' ?>"><i class="fas fa-user-tie"></i> Resellers</a>

    <div class="nav-label">Transactions</div>
    <a href="recharges.php" class="nav-item <?= $current_page==='recharges.php'?'active':'' ?>">
        <i class="fas fa-mobile-alt"></i> Recharge Requests
        <?php if($pending_recharge>0): ?><span class="nav-badge"><?= $pending_recharge ?></span><?php endif; ?>
    </a>
    <a href="transactions.php" class="nav-item <?= $current_page==='transactions.php'?'active':'' ?>"><i class="fas fa-exchange-alt"></i> All Transactions</a>
    <a href="deposits.php" class="nav-item <?= $current_page==='deposits.php'?'active':'' ?>">
        <i class="fas fa-plus-circle"></i> Deposits
        <?php if($pending_deposits>0): ?><span class="nav-badge"><?= $pending_deposits ?></span><?php endif; ?>
    </a>

    <div class="nav-label">Services</div>
    <a href="operators.php" class="nav-item <?= $current_page==='operators.php'?'active':'' ?>"><i class="fas fa-sim-card"></i> Operators</a>
    <a href="packages.php" class="nav-item <?= $current_page==='packages.php'?'active':'' ?>"><i class="fas fa-box"></i> Packages</a>
    <a href="user_packages.php" class="nav-item <?= $current_page==='user_packages.php'?'active':'' ?>"><i class="fas fa-tags"></i> User Packages</a>
    <a href="bill_pay.php" class="nav-item <?= $current_page==='bill_pay.php'?'active':'' ?>"><i class="fas fa-receipt"></i> Bill Pay</a>
    <a href="user_services.php" class="nav-item <?= $current_page==='user_services.php'?'active':'' ?>"><i class="fas fa-toggle-on"></i> User Services</a>
    <a href="banners.php" class="nav-item <?= $current_page==='banners.php'?'active':'' ?>"><i class="fas fa-images"></i> Banners</a>
    <a href="devices.php" class="nav-item <?= $current_page==='devices.php'?'active':'' ?>"><i class="fas fa-mobile-alt"></i> Device Manager</a>
    <a href="mbanking.php" class="nav-item <?= $current_page==='mbanking.php'?'active':'' ?>"><i class="fas fa-university"></i> mBanking</a>
    <a href="cards.php" class="nav-item <?= $current_page==='cards.php'?'active':'' ?>">
        <i class="fas fa-layer-group"></i> Cards
        <?php
        try { $card_orders=$db->query("SELECT COUNT(*) FROM card_orders WHERE status='pending'")->fetchColumn(); if($card_orders>0) echo '<span class="nav-badge">'.$card_orders.'</span>'; } catch(Exception $e){}
        ?>
    </a>
    <a href="drive_packages.php" class="nav-item <?= $current_page==='drive_packages.php'?'active':'' ?>"><i class="fas fa-bolt"></i> Drive Plans</a>
    <a href="drive_requests.php" class="nav-item <?= $current_page==='drive_requests.php'?'active':'' ?>">
        <i class="fas fa-paper-plane"></i> Drive Requests
        <?php 
        try { $dr_pending=$db->query("SELECT COUNT(*) FROM drive_requests WHERE status='pending'")->fetchColumn(); if($dr_pending>0) echo '<span class="nav-badge">'.$dr_pending.'</span>'; } catch(Exception $e){}
        ?>
    </a>
    <a href="manual_drive.php" class="nav-item <?= $current_page==='manual_drive.php'?'active':'' ?>">
        <i class="fas fa-hand-paper" style="color:#805ad5"></i> Manual Drive
        <?php
        try { $md_pending=$db->query("SELECT COUNT(*) FROM manual_drive_requests WHERE status='pending'")->fetchColumn(); if($md_pending>0) echo '<span class="nav-badge">'.$md_pending.'</span>'; } catch(Exception $e){}
        ?>
    </a>
    <a href="drive.php" class="nav-item <?= $current_page==='drive.php'?'active':'' ?>"><i class="fas fa-bolt"></i> Drive Plans</a>
    <a href="international.php" class="nav-item <?= $current_page==='international.php'?'active':'' ?>" style="background:<?= $current_page==='international.php'?'':''; ?>">
        <i class="fas fa-globe"></i> International
        <span style="background:var(--secondary);color:var(--dark);font-size:9px;padding:2px 6px;border-radius:8px;margin-left:auto;font-weight:800">NEW</span>
    </a>

    <div class="nav-label">System</div>
    <a href="api_manager.php" class="nav-item <?= $current_page==='api_manager.php'?'active':'' ?>"><i class="fas fa-plug"></i> API Manager</a>
    <a href="ecare_settings.php" class="nav-item <?= $current_page==='ecare_settings.php'?'active':'' ?>"><i class="fas fa-bolt" style="color:#f5a623"></i> ECARE API</a>
    <a href="error_logs.php" class="nav-item <?= $current_page==='error_logs.php'?'active':'' ?>"><i class="fas fa-exclamation-triangle" style="color:#e53e3e"></i> Error Logs</a>
    <a href="currencies.php" class="nav-item <?= $current_page==='currencies.php'?'active':'' ?>"><i class="fas fa-coins"></i> Currencies</a>
    <a href="settings.php" class="nav-item <?= $current_page==='settings.php'?'active':'' ?>"><i class="fas fa-cog"></i> Settings</a>
    <a href="../logout.php" class="nav-item"><i class="fas fa-sign-out-alt"></i> Logout</a>
</nav>

<script>
function openSidebar(){
    document.getElementById('sidebar').classList.add('open');
    var ov=document.getElementById('sidebarOverlay');
    ov.style.display='block';
    setTimeout(function(){ov.style.opacity='1';ov.style.pointerEvents='all';},10);
    document.body.style.overflow='hidden';
}
function closeSidebar(){
    document.getElementById('sidebar').classList.remove('open');
    var ov=document.getElementById('sidebarOverlay');
    ov.style.opacity='0';
    ov.style.pointerEvents='none';
    document.body.style.overflow='';
}
</script>
