<?php
require 'auth_check.php';
require '../config/db.php';

// HANDLE APPROVAL
if (isset($_GET['approve'])) {
    $id = $_GET['approve'];
    
    // Fetch Request
    $req = $pdo->query("SELECT * FROM wallet_transactions WHERE id=$id")->fetch();
    
    if ($req && $req['status'] == 'pending') {
        // 1. Add Money
        $pdo->prepare("UPDATE users SET wallet_balance = wallet_balance + ? WHERE id = ?")->execute([$req['amount'], $req['user_id']]);
        // 2. Mark Approved
        $pdo->prepare("UPDATE wallet_transactions SET status = 'approved' WHERE id = ?")->execute([$id]);
        
        header("Location: wallet_requests.php?msg=approved");
    }
}

// HANDLE REJECT
if (isset($_GET['reject'])) {
    $pdo->prepare("UPDATE wallet_transactions SET status = 'rejected' WHERE id = ?")->execute([$_GET['reject']]);
    header("Location: wallet_requests.php?msg=rejected");
}

// Fetch Pending Requests
$requests = $pdo->query("SELECT w.*, u.name, u.email FROM wallet_transactions w JOIN users u ON w.user_id = u.id WHERE w.status='pending' ORDER BY w.created_at DESC")->fetchAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Wallet Requests</title>
    <link rel="stylesheet" href="admin_style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <div class="sidebar">
        <?php include 'sidebar.php'; ?>
    </div>

    <div class="content">
        <h1>Deposit Requests</h1>
        
        <?php if(empty($requests)): ?>
            <p>No pending deposits.</p>
        <?php else: ?>
            <table style="width: 100%; background: white; border-collapse: collapse;">
                <thead>
                    <tr style="background: #f1f1f1;">
                        <th style="padding: 10px;">User</th>
                        <th>Amount</th>
                        <th>Proof</th>
                        <th>Date</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($requests as $r): ?>
                    <tr style="border-bottom: 1px solid #eee;">
                        <td style="padding: 10px;">
                            <strong><?php echo htmlspecialchars($r['name']); ?></strong><br>
                            <small><?php echo htmlspecialchars($r['email']); ?></small>
                        </td>
                        <td style="font-weight: bold; color: #27ae60;">₹<?php echo number_format($r['amount']); ?></td>
                        <td>
                            <a href="../<?php echo $r['proof_img']; ?>" target="_blank" style="color: blue;">View</a>
                        </td>
                        <td><?php echo date('d M H:i', strtotime($r['created_at'])); ?></td>
                        <td>
                            <a href="wallet_requests.php?approve=<?php echo $r['id']; ?>" class="btn-view" style="background: green; color: white; padding: 5px 10px; text-decoration: none; border-radius: 4px;">Approve</a>
                            <a href="wallet_requests.php?reject=<?php echo $r['id']; ?>" class="btn-view" style="background: red; color: white; padding: 5px 10px; text-decoration: none; border-radius: 4px;">Reject</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</body>
</html>