<?php
session_start();
if (!isset($_SESSION['admin_id']) && $_SESSION['role'] != 'admin') {
    header("Location: ../login.php");
    exit();
}
include '../../include/config.php';

// Handle withdrawal status update
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['approve'])) {
        $id = intval($_POST['id']);
        $conn->query("UPDATE hps_withdrawal_requests SET status = 'approved', processed_at = NOW() WHERE id = '$id'");
        $success = "Withdrawal approved!";
    }
    if (isset($_POST['reject'])) {
        $id = intval($_POST['id']);
        $conn->query("UPDATE hps_withdrawal_requests SET status = 'rejected', processed_at = NOW() WHERE id = '$id'");
        $success = "Withdrawal rejected!";
    }
    if (isset($_POST['complete'])) {
        $id = intval($_POST['id']);
        $conn->query("UPDATE hps_withdrawal_requests SET status = 'completed', processed_at = NOW() WHERE id = '$id'");
        $success = "Withdrawal marked as completed!";
    }
}

// Get all withdrawal requests
$withdrawals = $conn->query("
    SELECT w.*, p.first_name, p.last_name, p.email 
    FROM hps_withdrawal_requests w
    LEFT JOIN patients p ON w.user_id = p.patient_id
    ORDER BY FIELD(w.status, 'pending', 'approved', 'processing', 'completed', 'rejected'), w.created_at DESC
");

$page_title = 'Withdrawal Management | Admin';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Withdrawal Management - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        body { background: #f0f2f5; font-family: 'Segoe UI', sans-serif; }
        .status-pending { background: #ffc107; color: #856404; }
        .status-approved { background: #17a2b8; color: white; }
        .status-completed { background: #28a745; color: white; }
        .status-rejected { background: #dc3545; color: white; }
        .withdrawal-card { background: white; border-radius: 16px; padding: 20px; margin-bottom: 20px; border-left: 4px solid #d4a017; }
    </style>
</head>
<body>

<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2><i class="fas fa-hand-holding-usd me-2" style="color: #d4a017;"></i> Withdrawal Management</h2>
            <p class="text-muted">Process and manage user withdrawal requests</p>
        </div>
        <a href="../dashboard.php" class="btn btn-outline-secondary rounded-pill">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <?php if(isset($success)): ?>
        <div class="alert alert-success"><?= $success ?></div>
    <?php endif; ?>

    <div class="row">
        <?php if($withdrawals && $withdrawals->num_rows > 0): ?>
            <?php while($w = $withdrawals->fetch_assoc()): 
                $status_class = '';
                $status_text = ucfirst($w['status']);
                if($w['status'] == 'pending') $status_class = 'status-pending';
                elseif($w['status'] == 'approved') $status_class = 'status-approved';
                elseif($w['status'] == 'completed') $status_class = 'status-completed';
                else $status_class = 'status-rejected';
            ?>
                <div class="col-md-6">
                    <div class="withdrawal-card">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <h5 class="mb-1"><?= htmlspecialchars($w['first_name'] . ' ' . $w['last_name']) ?></h5>
                                <small class="text-muted"><?= htmlspecialchars($w['email']) ?></small>
                            </div>
                            <span class="badge <?= $status_class ?> p-2"><?= $status_text ?></span>
                        </div>
                        <div class="row mt-3">
                            <div class="col-6">
                                <small class="text-muted">Amount</small>
                                <h4 class="text-danger mb-0">₦<?= number_format($w['amount'], 2) ?></h4>
                            </div>
                            <div class="col-6">
                                <small class="text-muted">Requested</small>
                                <div><?= date('M d, Y', strtotime($w['created_at'])) ?></div>
                            </div>
                        </div>
                        <div class="mt-3">
                            <small class="text-muted">Bank Details</small>
                            <div class="bg-light rounded-3 p-2 mt-1">
                                <?= htmlspecialchars($w['bank_name'] ?? 'N/A') ?> - 
                                <?= htmlspecialchars($w['account_number'] ?? 'N/A') ?><br>
                                <strong><?= htmlspecialchars($w['account_name'] ?? 'N/A') ?></strong>
                            </div>
                        </div>
                        <?php if($w['status'] == 'pending'): ?>
                            <div class="mt-3 d-flex gap-2">
                                <form method="POST" class="d-inline">
                                    <input type="hidden" name="id" value="<?= $w['id'] ?>">
                                    <button type="submit" name="approve" class="btn btn-success btn-sm">
                                        <i class="fas fa-check me-1"></i> Approve
                                    </button>
                                </form>
                                <form method="POST" class="d-inline">
                                    <input type="hidden" name="id" value="<?= $w['id'] ?>">
                                    <button type="submit" name="reject" class="btn btn-danger btn-sm">
                                        <i class="fas fa-times me-1"></i> Reject
                                    </button>
                                </form>
                            </div>
                        <?php elseif($w['status'] == 'approved'): ?>
                            <form method="POST" class="mt-3">
                                <input type="hidden" name="id" value="<?= $w['id'] ?>">
                                <button type="submit" name="complete" class="btn btn-primary btn-sm w-100">
                                    <i class="fas fa-check-circle me-1"></i> Mark as Completed
                                </button>
                            </form>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="col-12">
                <div class="alert alert-info text-center py-5">
                    <i class="fas fa-inbox fa-3x mb-3"></i>
                    <h5>No withdrawal requests</h5>
                    <p>All withdrawals have been processed.</p>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

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