<?php
// FILE VERSION: 2.0 - WITHDRAWAL HISTORY (COMPLETED/REJECTED)
require_once '../config/database.php';
require_once '../includes/functions.php';
$pageTitle = 'Withdrawal History';
include 'side.php';
require_once 'admin_protect.php';

$conn = getConnection();

// ============================================================================
// GET FILTER PARAMETERS
// ============================================================================
$status = isset($_GET['status']) ? sanitize($_GET['status']) : '';
$search = isset($_GET['search']) ? sanitize($_GET['search']) : '';
$wallet_type = isset($_GET['wallet_type']) ? sanitize($_GET['wallet_type']) : '';
$date_from = isset($_GET['date_from']) ? sanitize($_GET['date_from']) : '';
$date_to = isset($_GET['date_to']) ? sanitize($_GET['date_to']) : '';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$perPage = 20;
$offset = ($page - 1) * $perPage;

// Build query - Completed and Rejected only
$where = "w.status IN ('completed', 'rejected')";
$params = [];
$types = "";

if ($status && in_array($status, ['completed', 'rejected'])) {
    $where .= " AND w.status = ?";
    $params[] = $status;
    $types .= "s";
}

if ($search) {
    $where .= " AND (u.username LIKE ? OR u.email LIKE ? OR w.phone_number LIKE ?)";
    $searchParam = "%$search%";
    $params[] = $searchParam;
    $params[] = $searchParam;
    $params[] = $searchParam;
    $types .= "sss";
}

if ($wallet_type) {
    $where .= " AND w.wallet_type = ?";
    $params[] = $wallet_type;
    $types .= "s";
}

if ($date_from) {
    $where .= " AND DATE(w.processed_at) >= ?";
    $params[] = $date_from;
    $types .= "s";
}

if ($date_to) {
    $where .= " AND DATE(w.processed_at) <= ?";
    $params[] = $date_to;
    $types .= "s";
}

// Get total count
$countSql = "SELECT COUNT(*) as total FROM withdrawals w JOIN users u ON w.user_id=u.id WHERE $where";
$countStmt = $conn->prepare($countSql);
if (!empty($params)) {
    $countStmt->bind_param($types, ...$params);
}
$countStmt->execute();
$total = $countStmt->get_result()->fetch_assoc()['total'];
$totalPages = ceil($total / $perPage);

// Get withdrawals
$query = "
    SELECT w.*, u.username, u.email, u.country,
           c.currency_symbol, c.currency_code,
           a.username as processed_by_name
    FROM withdrawals w 
    JOIN users u ON w.user_id=u.id
    LEFT JOIN countries c ON u.country = c.name
    LEFT JOIN admins a ON w.processed_by = a.id
    WHERE $where 
    ORDER BY w.processed_at DESC
    LIMIT ?, ?
";

$stmt = $conn->prepare($query);
$params[] = $offset;
$params[] = $perPage;
$types .= "ii";
$stmt->bind_param($types, ...$params);
$stmt->execute();
$withdrawals = $stmt->get_result();

// Get statistics
$stats = $conn->query("
    SELECT 
        COUNT(CASE WHEN status='completed' THEN 1 END) as completed_count,
        COUNT(CASE WHEN status='rejected' THEN 1 END) as rejected_count,
        COALESCE(SUM(CASE WHEN status='completed' THEN net_amount ELSE 0 END), 0) as total_paid,
        COALESCE(SUM(CASE WHEN status='rejected' THEN amount ELSE 0 END), 0) as total_refunded,
        COUNT(CASE WHEN status='completed' AND DATE(processed_at) = CURDATE() THEN 1 END) as today_completed
    FROM withdrawals
")->fetch_assoc();

// Get wallet types
$walletTypes = $conn->query("SELECT DISTINCT wallet_type FROM withdrawals ORDER BY wallet_type");
?>

<style>
.page-wrapper { padding: 16px; max-width: 1400px; margin: 0 auto; }
@media (min-width: 768px) { .page-wrapper { padding: 24px; } }

/* HEADER */
.page-header { margin-bottom: 20px; }

.header-top {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 16px;
}

@media (min-width: 640px) {
    .header-top {
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
    }
}

.page-title {
    font-size: 24px;
    font-weight: 800;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 10px;
}

.page-title i { color: #10b981; }

.header-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.btn {
    padding: 10px 18px;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    border: none;
}

.btn-primary {
    background: linear-gradient(135deg, var(--theme-color) 0%, #ec4899 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(var(--theme-rgb), 0.4);
}

.btn-secondary {
    background: var(--card-bg);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover { background: var(--darker-bg); }

.btn-warning {
    background: linear-gradient(135deg, #f59e0b 0%, #ef4444 100%);
    color: white;
}

.btn-warning:hover {
    box-shadow: 0 4px 16px rgba(245, 158, 11, 0.4);
}

/* ALERT */
.alert {
    padding: 14px 18px;
    border-radius: 12px;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
    font-weight: 500;
}

.alert-success {
    background: rgba(16, 185, 129, 0.15);
    border: 1px solid rgba(16, 185, 129, 0.3);
    color: #10b981;
}

/* STATS */
.stats-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    margin-bottom: 20px;
}

@media (min-width: 768px) {
    .stats-row { grid-template-columns: repeat(4, 1fr); }
}

.stat-card {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 16px;
    text-align: center;
    transition: all 0.3s;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}

.stat-card.green {
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.1) 0%, rgba(5, 150, 105, 0.1) 100%);
    border-color: rgba(16, 185, 129, 0.3);
}

.stat-card.red {
    background: linear-gradient(135deg, rgba(239, 68, 68, 0.1) 0%, rgba(220, 38, 38, 0.1) 100%);
    border-color: rgba(239, 68, 68, 0.3);
}

.stat-icon {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 10px;
    color: white;
    font-size: 18px;
}

.stat-card.green .stat-icon { background: linear-gradient(135deg, #10b981 0%, #059669 100%); }
.stat-card.red .stat-icon { background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%); }
.stat-card .stat-icon { background: linear-gradient(135deg, var(--theme-color) 0%, #ec4899 100%); }

.stat-value {
    font-size: 24px;
    font-weight: 800;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.stat-label {
    font-size: 11px;
    color: var(--text-secondary);
    font-weight: 600;
    text-transform: uppercase;
}

/* FILTERS */
.filters-card {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 16px;
    margin-bottom: 20px;
}

.filters-form {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: flex-end;
}

.filter-group {
    flex: 1;
    min-width: 140px;
}

.filter-group.search-group {
    min-width: 180px;
    flex: 2;
}

.filter-label {
    display: block;
    font-size: 11px;
    font-weight: 700;
    color: var(--text-secondary);
    margin-bottom: 6px;
    text-transform: uppercase;
}

.filter-input {
    width: 100%;
    padding: 10px 14px;
    background: var(--darker-bg);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-primary);
    font-size: 14px;
}

.filter-input:focus {
    outline: none;
    border-color: var(--theme-color);
}

.filter-buttons {
    display: flex;
    gap: 8px;
}

/* STATUS TABS */
.status-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    overflow-x: auto;
    padding-bottom: 4px;
}

.status-tab {
    padding: 10px 20px;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}

.status-tab:hover {
    background: var(--darker-bg);
    color: var(--text-primary);
}

.status-tab.active {
    background: linear-gradient(135deg, var(--theme-color) 0%, #ec4899 100%);
    color: white;
    border-color: transparent;
}

.status-tab .badge {
    padding: 2px 8px;
    border-radius: 6px;
    font-size: 11px;
    background: rgba(255, 255, 255, 0.2);
}

.status-tab:not(.active) .badge {
    background: var(--darker-bg);
}

/* WITHDRAWAL CARDS (Mobile) */
.withdrawals-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.withdrawal-card {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    overflow: hidden;
    transition: all 0.3s;
}

.withdrawal-card:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: var(--darker-bg);
    border-bottom: 1px solid var(--border-color);
}

.card-id {
    font-size: 14px;
    font-weight: 700;
    color: var(--text-primary);
}

.status-badge {
    padding: 5px 12px;
    border-radius: 8px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
}

.status-completed {
    background: rgba(16, 185, 129, 0.15);
    color: #10b981;
    border: 1px solid rgba(16, 185, 129, 0.3);
}

.status-rejected {
    background: rgba(239, 68, 68, 0.15);
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.3);
}

.card-body {
    padding: 16px;
}

.user-row {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 14px;
    padding-bottom: 14px;
    border-bottom: 1px solid var(--border-color);
}

.user-avatar {
    width: 42px;
    height: 42px;
    background: linear-gradient(135deg, var(--theme-color) 0%, #ec4899 100%);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 700;
    font-size: 16px;
    flex-shrink: 0;
}

.user-details { flex: 1; min-width: 0; }

.user-name {
    font-size: 15px;
    font-weight: 700;
    color: var(--text-primary);
}

.user-email {
    font-size: 12px;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.amount-display {
    text-align: center;
    padding: 14px;
    background: var(--darker-bg);
    border-radius: 12px;
    margin-bottom: 14px;
}

.amount-label {
    font-size: 11px;
    color: var(--text-secondary);
    text-transform: uppercase;
    font-weight: 600;
    margin-bottom: 4px;
}

.amount-value {
    font-size: 22px;
    font-weight: 800;
}

.amount-value.completed { color: #10b981; }
.amount-value.rejected { color: #ef4444; text-decoration: line-through; }

.detail-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
}

.detail-item {
    padding: 10px;
    background: var(--darker-bg);
    border-radius: 8px;
}

.detail-label {
    font-size: 10px;
    color: var(--text-secondary);
    text-transform: uppercase;
    font-weight: 600;
    margin-bottom: 2px;
}

.detail-value {
    font-size: 13px;
    color: var(--text-primary);
    font-weight: 600;
}

/* TABLE (Desktop) */
@media (min-width: 1024px) {
    .withdrawals-list { display: none; }
    .withdrawals-table-container { display: block; }
}

@media (max-width: 1023px) {
    .withdrawals-list { display: flex; }
    .withdrawals-table-container { display: none; }
}

.withdrawals-table-container {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    overflow: hidden;
}

.table-wrapper { overflow-x: auto; }

.withdrawals-table {
    width: 100%;
    border-collapse: collapse;
}

.withdrawals-table th {
    padding: 14px 16px;
    text-align: left;
    font-size: 11px;
    font-weight: 700;
    color: var(--text-secondary);
    text-transform: uppercase;
    background: var(--darker-bg);
    border-bottom: 1px solid var(--border-color);
}

.withdrawals-table td {
    padding: 14px 16px;
    font-size: 14px;
    color: var(--text-primary);
    border-bottom: 1px solid var(--border-color);
}

.withdrawals-table tbody tr:hover {
    background: var(--darker-bg);
}

/* EMPTY STATE */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
}

.empty-icon {
    font-size: 64px;
    color: var(--text-secondary);
    margin-bottom: 16px;
    opacity: 0.3;
}

.empty-title {
    font-size: 20px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.empty-text {
    color: var(--text-secondary);
    font-size: 14px;
}

/* PAGINATION */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    margin-top: 24px;
    flex-wrap: wrap;
}

.page-btn {
    padding: 10px 14px;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
}

.page-btn:hover { background: var(--darker-bg); }

.page-btn.active {
    background: linear-gradient(135deg, var(--theme-color) 0%, #ec4899 100%);
    color: white;
    border-color: transparent;
}

/* EXPORT SECTION */
.export-section {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-bottom: 16px;
}
</style>

<div class="page-wrapper">
    
    <?php $flash = getFlash(); if ($flash): ?>
        <div class="alert alert-<?php echo $flash['type']; ?>">
            <i class="fas fa-check-circle"></i>
            <?php echo $flash['message']; ?>
        </div>
    <?php endif; ?>
    
    <!-- Header -->
    <div class="page-header">
        <div class="header-top">
            <h1 class="page-title">
                <i class="fas fa-history"></i> Withdrawal History
            </h1>
            <div class="header-actions">
                <a href="pending_withdrawals.php" class="btn btn-warning">
                    <i class="fas fa-clock"></i> Pending (<?php echo $conn->query("SELECT COUNT(*) as c FROM withdrawals WHERE status='pending'")->fetch_assoc()['c']; ?>)
                </a>
                <a href="wallet_withdrawal_settings.php" class="btn btn-primary">
                    <i class="fas fa-cog"></i> Settings
                </a>
            </div>
        </div>
    </div>
    
    <!-- Stats -->
    <div class="stats-row">
        <div class="stat-card green">
            <div class="stat-icon"><i class="fas fa-check-circle"></i></div>
            <div class="stat-value"><?php echo number_format($stats['completed_count']); ?></div>
            <div class="stat-label">Completed</div>
        </div>
        <div class="stat-card red">
            <div class="stat-icon"><i class="fas fa-times-circle"></i></div>
            <div class="stat-value"><?php echo number_format($stats['rejected_count']); ?></div>
            <div class="stat-label">Rejected</div>
        </div>
        <div class="stat-card green">
            <div class="stat-icon"><i class="fas fa-money-bill-wave"></i></div>
            <div class="stat-value">Ksh <?php echo number_format($stats['total_paid'], 0); ?></div>
            <div class="stat-label">Total Paid</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-calendar-check"></i></div>
            <div class="stat-value"><?php echo number_format($stats['today_completed']); ?></div>
            <div class="stat-label">Today</div>
        </div>
    </div>
    
    <!-- Status Tabs -->
    <div class="status-tabs">
        <a href="?<?php echo http_build_query(array_merge($_GET, ['status' => '', 'page' => 1])); ?>" class="status-tab <?php echo empty($status) ? 'active' : ''; ?>">
            <i class="fas fa-list"></i> All
            <span class="badge"><?php echo number_format($stats['completed_count'] + $stats['rejected_count']); ?></span>
        </a>
        <a href="?<?php echo http_build_query(array_merge($_GET, ['status' => 'completed', 'page' => 1])); ?>" class="status-tab <?php echo $status === 'completed' ? 'active' : ''; ?>">
            <i class="fas fa-check"></i> Completed
            <span class="badge"><?php echo number_format($stats['completed_count']); ?></span>
        </a>
        <a href="?<?php echo http_build_query(array_merge($_GET, ['status' => 'rejected', 'page' => 1])); ?>" class="status-tab <?php echo $status === 'rejected' ? 'active' : ''; ?>">
            <i class="fas fa-times"></i> Rejected
            <span class="badge"><?php echo number_format($stats['rejected_count']); ?></span>
        </a>
    </div>
    
    <!-- Filters -->
    <div class="filters-card">
        <form method="GET" class="filters-form">
            <input type="hidden" name="status" value="<?php echo htmlspecialchars($status); ?>">
            
            <div class="filter-group search-group">
                <label class="filter-label">Search</label>
                <input type="text" name="search" class="filter-input" placeholder="Username, email, phone..." value="<?php echo htmlspecialchars($search); ?>">
            </div>
            
            <div class="filter-group">
                <label class="filter-label">Wallet</label>
                <select name="wallet_type" class="filter-input">
                    <option value="">All Wallets</option>
                    <?php while ($wt = $walletTypes->fetch_assoc()): ?>
                        <option value="<?php echo htmlspecialchars($wt['wallet_type']); ?>" <?php echo $wallet_type === $wt['wallet_type'] ? 'selected' : ''; ?>>
                            <?php echo ucfirst($wt['wallet_type']); ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </div>
            
            <div class="filter-group">
                <label class="filter-label">From Date</label>
                <input type="date" name="date_from" class="filter-input" value="<?php echo htmlspecialchars($date_from); ?>">
            </div>
            
            <div class="filter-group">
                <label class="filter-label">To Date</label>
                <input type="date" name="date_to" class="filter-input" value="<?php echo htmlspecialchars($date_to); ?>">
            </div>
            
            <div class="filter-buttons">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-search"></i> Filter
                </button>
                <a href="withdrawal_history.php" class="btn btn-secondary">
                    <i class="fas fa-redo"></i>
                </a>
            </div>
        </form>
    </div>
    
    <?php if ($withdrawals->num_rows > 0): ?>
        
        <!-- Mobile Card View -->
        <div class="withdrawals-list">
            <?php while ($w = $withdrawals->fetch_assoc()): 
                $currency = $w['currency_symbol'] ?? 'Ksh';
            ?>
            <div class="withdrawal-card">
                <div class="card-header">
                    <span class="card-id">#<?php echo $w['id']; ?></span>
                    <span class="status-badge status-<?php echo $w['status']; ?>">
                        <?php echo $w['status']; ?>
                    </span>
                </div>
                <div class="card-body">
                    <div class="user-row">
                        <div class="user-avatar">
                            <?php echo strtoupper(substr($w['username'], 0, 1)); ?>
                        </div>
                        <div class="user-details">
                            <div class="user-name"><?php echo htmlspecialchars($w['username']); ?></div>
                            <div class="user-email"><?php echo htmlspecialchars($w['email']); ?></div>
                        </div>
                    </div>
                    
                    <div class="amount-display">
                        <div class="amount-label"><?php echo $w['status'] === 'completed' ? 'Amount Paid' : 'Amount Refunded'; ?></div>
                        <div class="amount-value <?php echo $w['status']; ?>">
                            <?php echo $currency; ?> <?php echo number_format($w['status'] === 'completed' ? $w['net_amount'] : $w['amount'], 2); ?>
                        </div>
                    </div>
                    
                    <div class="detail-grid">
                        <div class="detail-item">
                            <div class="detail-label">Phone</div>
                            <div class="detail-value"><?php echo htmlspecialchars($w['phone_number']); ?></div>
                        </div>
                        <div class="detail-item">
                            <div class="detail-label">Wallet</div>
                            <div class="detail-value"><?php echo ucfirst($w['wallet_type']); ?></div>
                        </div>
                        <div class="detail-item">
                            <div class="detail-label">Processed</div>
                            <div class="detail-value"><?php echo $w['processed_at'] ? date('M j, g:i A', strtotime($w['processed_at'])) : '-'; ?></div>
                        </div>
                        <div class="detail-item">
                            <div class="detail-label">By</div>
                            <div class="detail-value"><?php echo $w['processed_by_name'] ?? 'System'; ?></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
        
        <!-- Desktop Table View -->
        <?php $withdrawals->data_seek(0); ?>
        <div class="withdrawals-table-container">
            <div class="table-wrapper">
                <table class="withdrawals-table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>User</th>
                            <th>Amount</th>
                            <th>Fee</th>
                            <th>Net/Refund</th>
                            <th>Phone</th>
                            <th>Wallet</th>
                            <th>Status</th>
                            <th>Processed</th>
                            <th>By</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($w = $withdrawals->fetch_assoc()): 
                            $currency = $w['currency_symbol'] ?? 'Ksh';
                        ?>
                        <tr>
                            <td><strong>#<?php echo $w['id']; ?></strong></td>
                            <td>
                                <strong><?php echo htmlspecialchars($w['username']); ?></strong><br>
                                <small style="color: var(--text-secondary);"><?php echo htmlspecialchars($w['email']); ?></small>
                            </td>
                            <td><?php echo $currency; ?> <?php echo number_format($w['amount'], 2); ?></td>
                            <td><?php echo $currency; ?> <?php echo number_format($w['fee'], 2); ?></td>
                            <td>
                                <?php if ($w['status'] === 'completed'): ?>
                                    <strong style="color: #10b981;"><?php echo $currency; ?> <?php echo number_format($w['net_amount'], 2); ?></strong>
                                <?php else: ?>
                                    <span style="color: #ef4444; text-decoration: line-through;"><?php echo $currency; ?> <?php echo number_format($w['amount'], 2); ?></span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo htmlspecialchars($w['phone_number']); ?></td>
                            <td><?php echo ucfirst($w['wallet_type']); ?></td>
                            <td>
                                <span class="status-badge status-<?php echo $w['status']; ?>">
                                    <?php echo $w['status']; ?>
                                </span>
                            </td>
                            <td>
                                <?php echo $w['processed_at'] ? date('M j, Y', strtotime($w['processed_at'])) : '-'; ?><br>
                                <small style="color: var(--text-secondary);"><?php echo $w['processed_at'] ? date('g:i A', strtotime($w['processed_at'])) : ''; ?></small>
                            </td>
                            <td><?php echo $w['processed_by_name'] ?? 'System'; ?></td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
        
        <!-- Pagination -->
        <?php if ($totalPages > 1): ?>
        <div class="pagination">
            <?php if ($page > 1): ?>
                <a href="?<?php echo http_build_query(array_merge($_GET, ['page' => $page-1])); ?>" class="page-btn">
                    <i class="fas fa-chevron-left"></i>
                </a>
            <?php endif; ?>
            
            <?php for ($i = max(1, $page-2); $i <= min($totalPages, $page+2); $i++): ?>
                <a href="?<?php echo http_build_query(array_merge($_GET, ['page' => $i])); ?>" class="page-btn <?php echo $i === $page ? 'active' : ''; ?>">
                    <?php echo $i; ?>
                </a>
            <?php endfor; ?>
            
            <?php if ($page < $totalPages): ?>
                <a href="?<?php echo http_build_query(array_merge($_GET, ['page' => $page+1])); ?>" class="page-btn">
                    <i class="fas fa-chevron-right"></i>
                </a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
        
    <?php else: ?>
        <div class="empty-state">
            <div class="empty-icon"><i class="fas fa-inbox"></i></div>
            <div class="empty-title">No Records Found</div>
            <div class="empty-text">No withdrawal history matches your filters.</div>
        </div>
    <?php endif; ?>
    
</div>

<?php include 'footer.php'; ?>