<?php
include('include/header.php');
$page_title = WEB_NAME . " | All Withdrawals";

try {
    $stmt = $pdo->query(
        "SELECT t.*, u.username, u.email, u.full_name 
         FROM transactions t 
         LEFT JOIN users u ON t.user_id = u.id 
         WHERE t.transaction_type = 'withdrawal' OR t.payment_gateway = 'paystack_transfer'
         ORDER BY t.created_at DESC"
    );
    $withdrawals = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { $withdrawals = []; }

$total_amount = 0;
$successful = 0;
$pending = 0;
$failed = 0;
foreach ($withdrawals as $w) {
    $total_amount += (float)$w['amount'];
    if ($w['status'] === 'successful') $successful++;
    elseif ($w['status'] === 'pending') $pending++;
    else $failed++;
}
?>
<title><?php echo htmlspecialchars($page_title); ?></title>

<div class="container-fluid" id="container-wrapper">
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">All Withdrawals</h1>
    </div>

    <!-- Stats Row -->
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Total Withdrawals</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo count($withdrawals); ?></div>
                        </div>
                        <div class="col-auto"><i class="fas fa-list fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Successful</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $successful; ?></div>
                        </div>
                        <div class="col-auto"><i class="fas fa-check-circle fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Pending</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $pending; ?></div>
                        </div>
                        <div class="col-auto"><i class="fas fa-clock fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-3">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">Total Amount</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">&#8358;<?php echo number_format($total_amount, 2); ?></div>
                        </div>
                        <div class="col-auto"><i class="fas fa-naira-sign fa-2x text-gray-300"></i></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Withdrawals Table -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary"><i class="fas fa-money-bill-transfer mr-2"></i>Withdrawal Records</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover" id="withdrawalsTable" width="100%" cellspacing="0">
                    <thead class="thead-light">
                        <tr>
                            <th>#</th>
                            <th>User</th>
                            <th>Amount</th>
                            <th>Bank Details</th>
                            <th>Reference</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($withdrawals as $i => $w):
                            $meta = json_decode($w['metadata'] ?? '{}', true);
                            $acct = $meta['account'] ?? '';
                            $bank = $meta['bank'] ?? '';
                            $name = $meta['name'] ?? '';
                            $fee  = $meta['fee'] ?? 0;

                            if ($w['status'] === 'successful') $badge = 'badge-success';
                            elseif ($w['status'] === 'pending') $badge = 'badge-warning';
                            else $badge = 'badge-danger';
                        ?>
                        <tr>
                            <td><?php echo $i + 1; ?></td>
                            <td>
                                <strong><?php echo htmlspecialchars($w['username'] ?? 'Unknown'); ?></strong><br>
                                <small class="text-muted"><?php echo htmlspecialchars($w['email'] ?? ''); ?></small>
                            </td>
                            <td>
                                <strong>&#8358;<?php echo number_format((float)$w['amount'], 2); ?></strong>
                                <?php if ($fee > 0): ?>
                                <br><small class="text-muted">Fee: &#8358;<?php echo number_format((float)$fee, 2); ?></small>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php echo htmlspecialchars($name); ?><br>
                                <small class="text-muted"><?php echo htmlspecialchars($acct); ?> - <?php echo htmlspecialchars($bank); ?></small>
                            </td>
                            <td><small class="text-monospace"><?php echo htmlspecialchars($w['transaction_ref']); ?></small></td>
                            <td><span class="badge <?php echo $badge; ?>"><?php echo ucfirst($w['status']); ?></span></td>
                            <td><small><?php echo date('M d, Y H:i', strtotime($w['created_at'])); ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
$(document).ready(function() {
    if ($.fn.DataTable) {
        $('#withdrawalsTable').DataTable({ order: [[6, 'desc']], pageLength: 25 });
    }
});
</script>

<?php include('include/footer.php'); ?>
