<?php
require_once '../connection.php';

if (!isLoggedIn() || isAdmin()) {
    header('Location: login.php');
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['amount'])) {
    $client_id = $_SESSION['user_id'];
    $amount = floatval($_POST['amount']);
    $payment_method = $_POST['payment_method'];
    
    if ($amount <= 0) {
        $_SESSION['error'] = "Invalid amount";
        header('Location: dashboard.php');
        exit();
    }
    
    require_once '../functions.php';
    
    if (addWalletFunds($client_id, $amount, $payment_method)) {
        $_SESSION['success'] = "Successfully added $" . number_format($amount, 2) . " to your wallet!";
    } else {
        $_SESSION['error'] = "Failed to add funds. Please try again.";
    }
    
    header('Location: dashboard.php');
    exit();
} else {
    header('Location: dashboard.php');
    exit();
}
?>