<?php
// um-admin/sidebar.php  — shared sidebar, include INSIDE the layout wrapper
require_once __DIR__ . '/auth.php';

// Badge counts
$pending_dep = (int)$pdo->query("SELECT COUNT(*) FROM deposits WHERE status='pending'")->fetchColumn();
$pending_wd  = (int)$pdo->query("SELECT COUNT(*) FROM withdrawals WHERE status='pending'")->fetchColumn();
$open_tick   = (int)$pdo->query("SELECT COUNT(*) FROM tickets WHERE status IN ('Open','In Progress')")->fetchColumn();

$cur = basename($_SERVER['PHP_SELF']);
function navItem($href, $icon, $label, $badge=0, $cur='') {
    $active = (basename($href)===$cur) ? 'active' : '';
    $b = $badge>0 ? "<span class='badge'>{$badge}</span>" : '';
    return "<a href='{$href}' class='nav-item {$active}'><i class='fa-solid fa-{$icon}'></i><span>{$label}</span>{$b}</a>";
}
?>
<style>
.admin-sidebar{width:240px;min-height:100vh;background:#111827;border-right:1px solid rgba(255,255,255,.07);display:flex;flex-direction:column;flex-shrink:0;position:sticky;top:0;transition:transform .3s ease;}
.sidebar-brand{padding:24px 20px 16px;border-bottom:1px solid rgba(255,255,255,.07);display:flex;align-items:center;gap:12px}
.brand-icon{width:38px;height:38px;background:linear-gradient(135deg,#f26d0f,#c45a0a);border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:17px;color:#fff}
.brand-text{font-size:16px;font-weight:700;color:#fff}
.brand-sub{font-size:11px;color:#8892a4}
.close-sidebar-btn{display:none;margin-left:auto;background:transparent;border:none;color:#8892a4;font-size:20px;cursor:pointer;}
.nav-section{padding:20px 12px 8px;color:#4b5563;font-size:10px;font-weight:700;text-transform:uppercase;letter-spacing:1.2px}
.nav-item{display:flex;align-items:center;gap:12px;padding:11px 14px;border-radius:10px;color:#9ca3af;font-size:14px;font-weight:500;text-decoration:none;margin-bottom:2px;transition:all .2s;position:relative}
.nav-item i{width:18px;text-align:center;font-size:15px}
.nav-item:hover,.nav-item.active{background:rgba(242,109,15,.12);color:#f26d0f}
.nav-item .badge{margin-left:auto;background:#ef4444;color:#fff;font-size:10px;font-weight:700;padding:2px 7px;border-radius:20px;min-width:20px;text-align:center}
.sidebar-footer{margin-top:auto;padding:16px 12px;border-top:1px solid rgba(255,255,255,.07)}
.sidebar-footer a{display:flex;align-items:center;gap:10px;padding:10px 14px;border-radius:10px;color:#9ca3af;text-decoration:none;font-size:13px;transition:all .2s}
.sidebar-footer a:hover{background:rgba(239,68,68,.1);color:#f87171}

/* Mobile Overlay & Toggle Button */
.sidebar-overlay{position:fixed;inset:0;background:rgba(0,0,0,0.7);z-index:9998;display:none;opacity:0;transition:opacity .3s;}
.sidebar-overlay.active{display:block;opacity:1;}
.mobile-menu-btn{display:none;position:fixed;bottom:24px;right:24px;z-index:9997;width:48px;height:48px;background:#111827;border:1px solid rgba(255,255,255,.1);color:#f26d0f;border-radius:4px;font-size:20px;align-items:center;justify-content:center;cursor:pointer;}

/* Responsive Rules */
@media(max-width:900px){
    .admin-sidebar{position:fixed;top:0;left:0;height:100vh;z-index:9999;transform:translateX(-100%);background:#111827;}
    .admin-sidebar.open{transform:translateX(0);}
    .close-sidebar-btn{display:block;}
    .mobile-menu-btn{display:flex;}
}
</style>

<div class="sidebar-overlay" id="sidebarOverlay"></div>
<button class="mobile-menu-btn" id="mobileMenuBtn"><i class="fa-solid fa-bars"></i></button>

<aside class="admin-sidebar" id="adminSidebar">
  <div class="sidebar-brand">
    <div class="brand-icon"><i class="fa-solid fa-shield-halved"></i></div>
    <div><div class="brand-text">PakClub</div><div class="brand-sub">Admin Panel</div></div>
    <button class="close-sidebar-btn" id="closeSidebarBtn"><i class="fa-solid fa-xmark"></i></button>
  </div>

  <div class="nav-section">Main</div>
  <?= navItem('dashboard.php','gauge-high','Dashboard',0,$cur) ?>
  <?= navItem('users.php','users','Users',0,$cur) ?>
  <?= navItem('sus.php','user-ninja','Suspicious Users',0,$cur) ?>

  <div class="nav-section">Games</div>
  <?= navItem('games.php','layer-group','Games Library',0,$cur) ?>
  <?= navItem('Wingo.php','gamepad','WinGo Game',0,$cur) ?>
  <?= navItem('CarRace.php','car','Car Race Game',0,$cur) ?>
  <?= navItem('Chicken.php','drumstick-bite','Chicken Game',0,$cur) ?>
  <?= navItem('Mines.php','bomb','Mines Game',0,$cur) ?>


  <div class="nav-section">Finance</div>
  <?= navItem('deposits.php','money-bill-wave','Deposits',$pending_dep,$cur) ?>
  <?= navItem('withdrawals.php','money-bill-transfer','Withdrawals',$pending_wd,$cur) ?>
  <?= navItem('Payments.php','credit-card','Payment Gateways',0,$cur) ?>

  <div class="nav-section">Content</div>
  <?= navItem('notifications.php','bell','Notifications',0,$cur) ?>
  <?= navItem('tickets.php','headset','Support',$open_tick,$cur) ?>
  <?= navItem('giftcodes.php','gift','Gift Codes',0,$cur) ?>
  <?= navItem('referrals.php','people-group','Referrals',0,$cur) ?>

  <div class="nav-section">System</div>
  <?= navItem('settings.php','gear','Settings',0,$cur) ?>

  <div class="sidebar-footer">
    <a href="../index.php" target="_blank"><i class="fa-solid fa-arrow-up-right-from-square"></i> View Site</a>
    <a href="logout.php"><i class="fa-solid fa-right-from-bracket"></i> Logout</a>
  </div>
</aside>

<script>
document.addEventListener('DOMContentLoaded', () => {
    const sidebar = document.getElementById('adminSidebar');
    const overlay = document.getElementById('sidebarOverlay');
    const menuBtn = document.getElementById('mobileMenuBtn');
    const closeBtn = document.getElementById('closeSidebarBtn');

    const toggleMenu = () => {
        sidebar.classList.toggle('open');
        if(sidebar.classList.contains('open')) {
            overlay.classList.add('active');
        } else {
            overlay.classList.remove('active');
        }
    };

    if(menuBtn) menuBtn.addEventListener('click', toggleMenu);
    if(closeBtn) closeBtn.addEventListener('click', toggleMenu);
    if(overlay) overlay.addEventListener('click', toggleMenu);
});
</script>