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

$pdo = getDBConnection();

$page = max(1, intval($_GET['page'] ?? 1));
$perPage = 20;
$offset = ($page - 1) * $perPage;
$status = $_GET['status'] ?? '';

// Build query
$where = "WHERE 1=1";
$params = [];

if ($status && in_array($status, ['pending', 'approved', 'rejected'])) {
    $where .= " AND f.status = ?";
    $params[] = $status;
}

// Get total count
$countSql = "SELECT COUNT(*) FROM fund_requests f $where";
$stmt = $pdo->prepare($countSql);
$stmt->execute($params);
$total = $stmt->fetchColumn();
$totalPages = ceil($total / $perPage);

// Get deposits with user info
$sql = "SELECT f.*, u.name as user_name, u.email as user_email 
        FROM fund_requests f 
        LEFT JOIN users u ON f.user_id = u.id 
        $where 
        ORDER BY f.created_at DESC 
        LIMIT $perPage OFFSET $offset";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$deposits = $stmt->fetchAll();

// Get stats
$stats = [
    'total' => $pdo->query("SELECT COALESCE(SUM(amount), 0) FROM fund_requests WHERE status = 'approved'")->fetchColumn(),
    'pending' => $pdo->query("SELECT COUNT(*) FROM fund_requests WHERE status = 'pending'")->fetchColumn(),
    'approved' => $pdo->query("SELECT COUNT(*) FROM fund_requests WHERE status = 'approved'")->fetchColumn(),
    'rejected' => $pdo->query("SELECT COUNT(*) FROM fund_requests WHERE status = 'rejected'")->fetchColumn(),
];

$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>Deposit History - <?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(180px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
        .stat-card { background: #0F2744; border-radius: 12px; padding: 1.25rem; text-align: center; }
        .stat-card h4 { font-size: 1.5rem; margin-bottom: 0.25rem; }
        .stat-card p { color: #9CA3AF; font-size: 0.85rem; margin: 0; }
        .stat-card.green h4 { color: #10B981; }
        .stat-card.gold h4 { color: #F5A623; }
        .stat-card.red h4 { color: #EF4444; }
        
        .filter-row { display: flex; gap: 0.5rem; margin-bottom: 1.5rem; flex-wrap: wrap; }
        .filter-btn { padding: 0.5rem 1rem; background: #0F2744; border: 1px solid #1E3A5F; border-radius: 8px; color: #9CA3AF; text-decoration: none; font-size: 0.9rem; }
        .filter-btn:hover, .filter-btn.active { background: #F5A623; color: #0A1628; border-color: #F5A623; }
        
        .card { background: #0F2744; border-radius: 12px; overflow: hidden; }
        
        .table-wrapper { overflow-x: auto; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 1rem; text-align: left; border-bottom: 1px solid #1E3A5F; }
        th { background: #1E3A5F; font-weight: 600; font-size: 0.85rem; color: #9CA3AF; text-transform: uppercase; }
        td { font-size: 0.9rem; }
        tr:hover { background: rgba(245,166,35,0.05); }
        
        .user-info { display: flex; flex-direction: column; }
        .user-name { font-weight: 600; }
        .user-email { font-size: 0.8rem; color: #9CA3AF; }
        
        .amount { font-weight: 700; color: #10B981; }
        
        .status { padding: 0.25rem 0.75rem; border-radius: 20px; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; }
        .status-pending { background: rgba(245,166,35,0.2); color: #F5A623; }
        .status-approved { background: rgba(16,185,129,0.2); color: #10B981; }
        .status-rejected { background: rgba(239,68,68,0.2); color: #EF4444; }
        
        .method { display: flex; align-items: center; gap: 0.5rem; }
        .method i { color: #F5A623; }
        
        .empty { text-align: center; padding: 3rem; color: #9CA3AF; }
        .empty i { font-size: 3rem; margin-bottom: 1rem; display: block; color: #1E3A5F; }
        
        .pagination { display: flex; justify-content: center; gap: 0.5rem; padding: 1.5rem; }
        .pagination a, .pagination span { padding: 0.5rem 1rem; background: #1E3A5F; border-radius: 8px; color: #fff; text-decoration: none; }
        .pagination a:hover { background: #F5A623; color: #0A1628; }
        .pagination .current { background: #F5A623; color: #0A1628; }
        
        .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"><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 active"><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="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-coins"></i> Deposit History</h1>
            
            <div class="stats-grid">
                <div class="stat-card green">
                    <h4><?php echo formatCurrency($stats['total']); ?></h4>
                    <p>Total Deposited</p>
                </div>
                <div class="stat-card">
                    <h4><?php echo $stats['approved']; ?></h4>
                    <p>Approved</p>
                </div>
                <div class="stat-card gold">
                    <h4><?php echo $stats['pending']; ?></h4>
                    <p>Pending</p>
                </div>
                <div class="stat-card red">
                    <h4><?php echo $stats['rejected']; ?></h4>
                    <p>Rejected</p>
                </div>
            </div>
            
            <div class="filter-row">
                <a href="?" class="filter-btn <?php echo !$status ? 'active' : ''; ?>">All</a>
                <a href="?status=approved" class="filter-btn <?php echo $status === 'approved' ? 'active' : ''; ?>">Approved</a>
                <a href="?status=pending" class="filter-btn <?php echo $status === 'pending' ? 'active' : ''; ?>">Pending</a>
                <a href="?status=rejected" class="filter-btn <?php echo $status === 'rejected' ? 'active' : ''; ?>">Rejected</a>
            </div>
            
            <div class="card">
                <?php if (empty($deposits)): ?>
                <div class="empty">
                    <i class="fas fa-coins"></i>
                    <h3>No deposits found</h3>
                    <p>Deposits will appear here when users add funds.</p>
                </div>
                <?php else: ?>
                <div class="table-wrapper">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>User</th>
                                <th>Amount</th>
                                <th>Method</th>
                                <th>Reference</th>
                                <th>Status</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($deposits as $deposit): ?>
                            <tr>
                                <td>#<?php echo $deposit['id']; ?></td>
                                <td>
                                    <div class="user-info">
                                        <span class="user-name"><?php echo htmlspecialchars($deposit['user_name'] ?? 'Unknown'); ?></span>
                                        <span class="user-email"><?php echo htmlspecialchars($deposit['user_email'] ?? ''); ?></span>
                                    </div>
                                </td>
                                <td class="amount"><?php echo formatCurrency($deposit['amount']); ?></td>
                                <td>
                                    <div class="method">
                                        <i class="fab fa-bitcoin"></i>
                                        <?php echo ucfirst($deposit['payment_method'] ?? 'Crypto'); ?>
                                    </div>
                                </td>
                                <td style="font-family: monospace; font-size: 0.8rem; color: #9CA3AF;">
                                    <?php echo htmlspecialchars(substr($deposit['payment_reference'] ?? '', 0, 20)); ?>...
                                </td>
                                <td>
                                    <span class="status status-<?php echo $deposit['status']; ?>">
                                        <?php echo ucfirst($deposit['status']); ?>
                                    </span>
                                </td>
                                <td style="color: #9CA3AF; font-size: 0.85rem;">
                                    <?php echo date('M j, Y', strtotime($deposit['created_at'])); ?><br>
                                    <small><?php echo date('g:i A', strtotime($deposit['created_at'])); ?></small>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                
                <?php if ($totalPages > 1): ?>
                <div class="pagination">
                    <?php if ($page > 1): ?>
                    <a href="?<?php echo $status ? "status=$status&" : ''; ?>page=<?php echo $page - 1; ?>"><i class="fas fa-chevron-left"></i></a>
                    <?php endif; ?>
                    <span class="current"><?php echo $page; ?> / <?php echo $totalPages; ?></span>
                    <?php if ($page < $totalPages): ?>
                    <a href="?<?php echo $status ? "status=$status&" : ''; ?>page=<?php echo $page + 1; ?>"><i class="fas fa-chevron-right"></i></a>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>
    </main>
    
    <script>function toggleSidebar() { document.getElementById('sidebar').classList.toggle('active'); document.getElementById('menuOverlay').classList.toggle('active'); }</script>
</body>
</html>