<?php
require_once dirname(__DIR__) . '/config/config.php';
requireLogin();
requireAdmin();

$pdo = getDBConnection();
$userModel = new User();
$stats = $userModel->getStatistics();

$smmOrders = 0; $smsOrders = 0; $pendingFunds = 0; $totalDeposits = 0;
try {
    $smmOrders = $pdo->query("SELECT COUNT(*) FROM smm_orders")->fetchColumn();
    $smsOrders = $pdo->query("SELECT COUNT(*) FROM sms_orders")->fetchColumn();
    $pendingFunds = $pdo->query("SELECT COUNT(*) FROM fund_requests WHERE status = 'pending'")->fetchColumn();
    $totalDeposits = $pdo->query("SELECT COALESCE(SUM(amount), 0) FROM fund_requests WHERE status = 'approved'")->fetchColumn();
} catch (Exception $e) {}

$isAdmin = $_SESSION['role'] === 'admin';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Segoe UI', Tahoma, sans-serif; background: #0A1628; color: #fff; min-height: 100vh; display: flex; }
        
        .sidebar { width: 260px; background: #0F2744; height: 100vh; position: fixed; left: 0; top: 0; z-index: 100; display: flex; flex-direction: column; transition: transform 0.3s; }
        .sidebar-header { padding: 1.5rem; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid #1E3A5F; }
        .sidebar-header i { font-size: 1.5rem; color: #F5A623; }
        .sidebar-header span { font-weight: 700; color: #F5A623; font-size: 1.25rem; }
        .sidebar-nav { flex: 1; padding: 1rem 0; overflow-y: auto; }
        .nav-link { display: flex; align-items: center; gap: 12px; padding: 0.875rem 1.5rem; color: #E5E7EB; text-decoration: none; border-left: 3px solid transparent; }
        .nav-link:hover, .nav-link.active { background: #1E3A5F; color: #F5A623; border-left-color: #F5A623; }
        .nav-link i { width: 20px; text-align: center; }
        .nav-divider { height: 1px; background: #1E3A5F; margin: 0.75rem 1.5rem; }
        
        .main { flex: 1; margin-left: 260px; min-height: 100vh; display: flex; flex-direction: column; }
        .header { background: #0F2744; padding: 1rem 1.5rem; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 50; }
        .header-toggle { background: none; border: none; color: #fff; font-size: 1.25rem; cursor: pointer; display: none; }
        .header-user { display: flex; align-items: center; gap: 0.75rem; }
        .header-user i { color: #F5A623; }
        
        .content { flex: 1; padding: 1.5rem; }
        .page-title { font-size: 1.5rem; margin-bottom: 1.5rem; }
        .page-title i { color: #F5A623; margin-right: 0.5rem; }
        
        .stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
        .stat-card { background: #0F2744; border-radius: 12px; padding: 1.5rem; display: flex; align-items: center; gap: 1rem; }
        .stat-icon { width: 50px; height: 50px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 1.5rem; }
        .stat-icon.blue { background: rgba(59,130,246,0.2); color: #3B82F6; }
        .stat-icon.green { background: rgba(16,185,129,0.2); color: #10B981; }
        .stat-icon.gold { background: rgba(245,166,35,0.2); color: #F5A623; }
        .stat-icon.red { background: rgba(239,68,68,0.2); color: #EF4444; }
        .stat-icon.purple { background: rgba(139,92,246,0.2); color: #8B5CF6; }
        .stat-info h4 { font-size: 1.5rem; margin: 0; }
        .stat-info p { margin: 0; color: #9CA3AF; font-size: 0.875rem; }
        
        .card { background: #0F2744; border-radius: 12px; margin-bottom: 1.5rem; }
        .card-header { padding: 1rem 1.5rem; border-bottom: 1px solid #1E3A5F; }
        .card-header h3 { margin: 0; font-size: 1.1rem; }
        .card-body { padding: 1.5rem; }
        
        .quick-links { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 1rem; }
        .quick-link { background: #1E3A5F; border-radius: 8px; padding: 1.25rem; text-align: center; text-decoration: none; color: #fff; }
        .quick-link:hover { background: #2D4A6F; }
        .quick-link i { font-size: 2rem; color: #F5A623; margin-bottom: 0.5rem; display: block; }
        
        .menu-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 99; display: none; }
        .menu-overlay.active { display: block; }
        
        @media (max-width: 768px) {
            .sidebar { transform: translateX(-260px); }
            .sidebar.active { transform: translateX(0); }
            .main { margin-left: 0; }
            .header-toggle { display: block; }
        }
    </style>
</head>
<body>
    <div class="menu-overlay" id="menuOverlay" onclick="toggleSidebar()"></div>
    
    <aside class="sidebar" id="sidebar">
        <div class="sidebar-header"><i class="fas fa-feather-alt"></i><span><?php echo SITE_NAME; ?></span></div>
        <nav class="sidebar-nav">
            <a href="dashboard.php" class="nav-link active"><i class="fas fa-home"></i> Dashboard</a>
            <a href="users.php" class="nav-link"><i class="fas fa-users"></i> Users</a>
            <a href="orders.php" class="nav-link"><i class="fas fa-shopping-cart"></i> Orders</a>
            <a href="fund-requests.php" class="nav-link"><i class="fas fa-money-bill"></i> Fund Requests</a>
            <a href="deposits.php" class="nav-link"><i class="fas fa-coins"></i> Deposit History</a>
            <div class="nav-divider"></div>
            <a href="smm-services.php" class="nav-link"><i class="fas fa-rocket"></i> SMM Services</a>
            <a href="smm-categories.php" class="nav-link"><i class="fas fa-folder"></i> SMM Categories</a>
            <a href="sms-services.php" class="nav-link"><i class="fas fa-sms"></i> SMS Services</a>
            <div class="nav-divider"></div>
            <a href="banners.php" class="nav-link"><i class="fas fa-ad"></i> Banners</a>
            <a href="offers.php" class="nav-link"><i class="fas fa-tags"></i> Offers</a>
            <a href="support.php" class="nav-link"><i class="fas fa-headset"></i> Support</a>
            <?php if ($isAdmin): ?>
            <div class="nav-divider"></div>
            <a href="settings.php" class="nav-link"><i class="fas fa-cog"></i> Settings</a>
            <?php endif; ?>
            <div class="nav-divider"></div>
            <a href="<?php echo SITE_URL; ?>/logout.php" class="nav-link"><i class="fas fa-sign-out-alt"></i> Logout</a>
        </nav>
    </aside>
    
    <main class="main">
        <header class="header">
            <button class="header-toggle" onclick="toggleSidebar()"><i class="fas fa-bars"></i></button>
            <div class="header-user"><i class="fas fa-user-shield"></i> <span><?php echo htmlspecialchars($_SESSION['name']); ?></span></div>
        </header>
        
        <div class="content">
            <h1 class="page-title"><i class="fas fa-home"></i> Dashboard</h1>
            
            <div class="stats-grid">
                <div class="stat-card"><div class="stat-icon blue"><i class="fas fa-users"></i></div><div class="stat-info"><h4><?php echo number_format($stats['total_users']); ?></h4><p>Total Users</p></div></div>
                <div class="stat-card"><div class="stat-icon green"><i class="fas fa-shopping-cart"></i></div><div class="stat-info"><h4><?php echo number_format($smmOrders + $smsOrders); ?></h4><p>Total Orders</p></div></div>
                <div class="stat-card"><div class="stat-icon gold"><i class="fas fa-wallet"></i></div><div class="stat-info"><h4><?php echo formatCurrency($stats['total_balance']); ?></h4><p>User Balances</p></div></div>
                <div class="stat-card"><div class="stat-icon purple"><i class="fas fa-coins"></i></div><div class="stat-info"><h4><?php echo formatCurrency($totalDeposits); ?></h4><p>Total Deposits</p></div></div>
                <div class="stat-card"><div class="stat-icon red"><i class="fas fa-clock"></i></div><div class="stat-info"><h4><?php echo number_format($pendingFunds); ?></h4><p>Pending Requests</p></div></div>
            </div>
            
            <div class="card">
                <div class="card-header"><h3>Quick Actions</h3></div>
                <div class="card-body">
                    <div class="quick-links">
                        <a href="users.php" class="quick-link"><i class="fas fa-user-plus"></i> Manage Users</a>
                        <a href="fund-requests.php" class="quick-link"><i class="fas fa-money-check"></i> Fund Requests</a>
                        <a href="deposits.php" class="quick-link"><i class="fas fa-coins"></i> Deposit History</a>
                        <a href="orders.php" class="quick-link"><i class="fas fa-list"></i> View Orders</a>
                        <a href="offers.php" class="quick-link"><i class="fas fa-percent"></i> Manage Offers</a>
                        <a href="support.php" class="quick-link"><i class="fas fa-headset"></i> Support</a>
                        <?php if ($isAdmin): ?><a href="settings.php" class="quick-link"><i class="fas fa-cogs"></i> Settings</a><?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </main>
    
    <script>function toggleSidebar() { document.getElementById('sidebar').classList.toggle('active'); document.getElementById('menuOverlay').classList.toggle('active'); }</script>
</body>
</html>