<?php
session_start();
include 'db.php';

// 🔐 Optional: Admin authentication check
// if (!isset($_SESSION['admin_logged_in'])) {
//     header("Location: admin_login.php");
//     exit();
// }

// ✅ Approve withdrawal
if (isset($_GET['approve'])) {
    $id = intval($_GET['approve']);
    $conn->query("UPDATE withdrawals SET status='Approved' WHERE id=$id");
}

// ❌ Reject withdrawal
if (isset($_GET['reject'])) {
    $id = intval($_GET['reject']);
    $conn->query("UPDATE withdrawals SET status='Rejected' WHERE id=$id");
}

// 📄 Fetch pending withdrawals
$pending = $conn->query("SELECT w.id, w.amount, w.requested_at, u.username 
                         FROM withdrawals w 
                         JOIN users u ON w.user_id = u.id 
                         WHERE w.status='Pending'");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Admin Panel - Withdrawal Requests</title>
    <link rel="stylesheet" href="assets/style.css">
</head>
<body>
    <h1>🧑‍💼 Admin Panel - Withdrawal Requests</h1>

    <?php if ($pending->num_rows > 0): ?>
        <table border="1" cellpadding="10" style="margin: auto; background-color: #1a1a1a; color: #e0e0e0;">
            <tr>
                <th>ID</th>
                <th>User</th>
                <th>Amount</th>
                <th>Requested At</th>
                <th>Actions</th>
            </tr>
            <?php while($row = $pending->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $row['id']; ?></td>
                    <td><?php echo htmlspecialchars($row['username']); ?></td>
                    <td><?php echo number_format($row['amount'], 6); ?></td>
                    <td><?php echo $row['requested_at']; ?></td>
                    <td>
                        <a href="admin.php?approve=<?php echo $row['id']; ?>" style="color: #00ff00;">✅ Approve</a> | 
                        <a href="admin.php?reject=<?php echo $row['id']; ?>" style="color: #ff0000;">❌ Reject</a>
                    </td>
                </tr>
            <?php endwhile; ?>
        </table>
    <?php else: ?>
        <p>No pending withdrawal requests.</p>
    <?php endif; ?>

    <br><br>
    <a href="dashboard.php">← Back to Dashboard</a>
</body>
</html>
