<?php
require("../header.php"); 

// DEBUG
echo "<pre>";
print_r($_POST);
echo "</pre>";
exit();
?>

<?php
// POST data validation
$amount = isset($_POST['amount']) ? floatval($_POST['amount']) : 0;
$method = $_POST['method'] ?? '';
$reference = $_POST['reference'] ?? '';

if ($amount <= 0 || !$method || !$reference) {
    die("Invalid form submission.");
}

// Fetch user info (already included from header.php)
$stmt = $pdo->prepare("SELECT balance, currency FROM logins WHERE id = :id");
$stmt->execute(['id' => $id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user) {
    die("User not found.");
}

$curr = $user['currency'] ?? 'USD';
$currentBalance = $user['balance'];

// Insert into deposits table
$stmt = $pdo->prepare("
    INSERT INTO deposits (user_id, amount, method, reference, status, created_at)
    VALUES (:user_id, :amount, :method, :reference, :status, NOW())
");

$status = 'pending'; // Deposit pending confirmation
$stmt->execute([
    'user_id' => $id,
    'amount' => $amount,
    'method' => $method,
    'reference' => $reference,
    'status' => $status
]);

// Redirect back to topup page with success
header("Location: topup.php?success=1");
exit();
?>