<?php
require_once '../includes/functions.php';
requireLogin();
requireAdmin();

$msg = '';

if (isset($_GET['action']) && isset($_GET['id'])) {
    $id = $_GET['id'];
    $request = $pdo->prepare("SELECT * FROM wallet_requests WHERE id = ?");
    $request->execute([$id]);
    $req = $request->fetch();

    if ($req && $req['status'] === 'pending') {
        if ($_GET['action'] === 'approve') {
            $pdo->beginTransaction();
            try {
                // Credit user balance
                $updateUser = $pdo->prepare("UPDATE users SET balance = balance + ? WHERE id = ?");
                $updateUser->execute([$req['amount'], $req['user_id']]);

                // Update request status
                $updateReq = $pdo->prepare("UPDATE wallet_requests SET status = 'approved' WHERE id = ?");
                $updateReq->execute([$id]);

                $pdo->commit();
                $msg = "<div class='alert alert-success'>Request approved and wallet credited.</div>";
            } catch (Exception $e) {
                $pdo->rollBack();
                $msg = "<div class='alert alert-danger'>Error: " . $e->getMessage() . "</div>";
            }
        } elseif ($_GET['action'] === 'reject') {
            $updateReq = $pdo->prepare("UPDATE wallet_requests SET status = 'rejected' WHERE id = ?");
            $updateReq->execute([$id]);
            $msg = "<div class='alert alert-warning'>Request rejected.</div>";
        }
    }
}

$requests = $pdo->query("SELECT wr.*, u.name, u.email FROM wallet_requests wr JOIN users u ON wr.user_id = u.id ORDER BY wr.request_date DESC")->fetchAll();
include '../includes/header.php';
?>

<div class="row animate-fade-in">
    <div class="col-12">
        <div class="mb-4">
            <h2 class="fw-bold m-0"><i class="fas fa-wallet text-primary me-2"></i> Recharge <span class="text-primary">Requests</span></h2>
            <p class="text-muted small">Verify and approve user payment requests manually.</p>
        </div>
        
        <?php echo $msg; ?>

        <div class="glass-card overflow-hidden">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">User Information</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">Amount</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">UTR / Trans ID</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">Screenshot</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">Status</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold text-end">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($requests)): ?>
                            <tr>
                                <td colspan="6" class="text-center py-5">
                                    <i class="fas fa-inbox fa-3x text-light mb-3"></i>
                                    <p class="text-muted">No pending requests found.</p>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php foreach ($requests as $r): ?>
                        <tr>
                            <td class="px-4 py-3">
                                <div class="fw-bold text-dark"><?php echo htmlspecialchars($r['name']); ?></div>
                                <div class="small text-muted"><?php echo htmlspecialchars($r['email']); ?></div>
                                <div class="small text-muted mt-1 opacity-50"><?php echo date('d M, Y h:i A', strtotime($r['request_date'])); ?></div>
                            </td>
                            <td class="px-4 py-3">
                                <span class="fw-800 text-primary">₹<?php echo number_format($r['amount'], 2); ?></span>
                            </td>
                            <td class="px-4 py-3">
                                <code class="bg-light px-2 py-1 rounded text-primary fw-bold"><?php echo htmlspecialchars($r['utr_number']); ?></code>
                            </td>
                            <td class="px-4 py-3">
                                <?php if($r['screenshot_path']): ?>
                                    <a href="../<?php echo $r['screenshot_path']; ?>" target="_blank" class="btn btn-sm btn-outline-secondary rounded-pill">
                                        <i class="fas fa-image me-1"></i> View
                                    </a>
                                <?php else: ?>
                                    <span class="text-light small italic">No proof</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-4 py-3">
                                <?php if($r['status'] === 'approved'): ?>
                                    <span class="badge bg-success bg-opacity-10 text-success rounded-pill px-3 py-2 fw-bold">APPROVED</span>
                                <?php elseif($r['status'] === 'pending'): ?>
                                    <span class="badge bg-warning bg-opacity-10 text-warning rounded-pill px-3 py-2 fw-bold">PENDING</span>
                                <?php else: ?>
                                    <span class="badge bg-danger bg-opacity-10 text-danger rounded-pill px-3 py-2 fw-bold">REJECTED</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-4 py-3 text-end">
                                <?php if($r['status'] === 'pending'): ?>
                                    <div class="btn-group shadow-sm rounded-pill overflow-hidden">
                                        <a href="?action=approve&id=<?php echo $r['id']; ?>" class="btn btn-sm btn-success px-3" title="Approve">
                                            <i class="fas fa-check"></i>
                                        </a>
                                        <a href="?action=reject&id=<?php echo $r['id']; ?>" class="btn btn-sm btn-danger px-3" title="Reject">
                                            <i class="fas fa-times"></i>
                                        </a>
                                    </div>
                                <?php else: ?>
                                    <i class="fas fa-lock text-light" title="Closed"></i>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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