<?php
include 'includes/header.php'; 
include 'includes/sidebar.php';

// OPTIONAL: ADMIN CHECK
// if (!isset($_SESSION['admin'])) {
//     die("Access denied");
// }

// SUCCESS MESSAGE
if(isset($_GET['msg']) && $_GET['msg'] == 'deleted'){
    echo '<div class="alert alert-success">Withdrawal deleted successfully</div>';
}

$res = mysqli_query($conn,"
SELECT w.*,u.email FROM withdrawals w
JOIN users u ON u.id=w.user_id
ORDER BY w.id DESC
");
?>



    <div class="row">
        <div class="col-xl-8 col-lg-11 mx-auto">

            <div class="card p-3">

                <!-- Header -->
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <h4 class="text-primary mb-0">?? Withdrawals</h4>
                </div>

                <!-- Table -->
                <div class="table-responsive">
                    <table class="table table-dark table-hover align-middle">

                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>User</th>
                                <th>Amount</th>
                                <th>Wallet</th>
                                <th>Reference</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>

                        <tbody>

                        <?php while($w=mysqli_fetch_assoc($res)): ?>
                            <tr>

                                <td><?php echo $w['id']; ?></td>

                                <td><?php echo $w['email']; ?></td>

                                <td>
                                    <span class="fw-semibold text-success">
                                        $<?php echo $w['amount']; ?>
                                    </span>
                                </td>

                                <td>
                                    <span class="badge bg-secondary">
                                        <?php echo $w['wallet']; ?>
                                    </span>
                                </td>

                                <td>
                                    <span class="badge bg-info">
                                        <?php echo $w['reference'] ?? 'N/A'; ?>
                                    </span>
                                </td>

                                <td>
                                    <?php if($w['status'] == 'pending'): ?>
                                        <span class="badge bg-warning text-dark">Pending</span>
                                    <?php elseif($w['status'] == 'paid'): ?>
                                        <span class="badge bg-success">Paid</span>
                                    <?php else: ?>
                                        <span class="badge bg-danger">Failed</span>
                                    <?php endif; ?>
                                </td>

                                <td>
                                    <a href="delete_withdraw.php?id=<?php echo $w['id']; ?>" 
                                       class="btn btn-sm btn-danger"
                                       onclick="return confirm('Are you sure you want to delete this withdrawal?')">
                                       Delete
                                    </a>
                                </td>

                            </tr>
                        <?php endwhile; ?>

                        </tbody>

                    </table>
                </div>

            </div>

        </div>
    </div>



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