<?php

// Retrieve data from the session
// $amountPaid = $_SESSION['amount'];
// $userId = $_SESSION['user_id'];
$amountPaid = isset($_SESSION['amountPaid']) ? $_SESSION['amountPaid'] : 0;
$userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0;

// Establish a database connection (replace with your database connection code)
$servername = "67.225.140.44";
$username = "magadata_Aayareemah";
$password = "_mkR^x0?*vAR";
$dbname = "magadata_Magadata";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Update the wallet balance
$sql = "UPDATE wallet SET balance = balance + $amountPaid/100 WHERE user_id = $userId";
if ($conn->query($sql) === TRUE) {
    // Close the database connection
    $conn->close();

    // Output success information or redirect as needed
    $_SESSION['addedAmount'] = $amountPaid;

    echo '<script>window.location.href = "wallet?success";</script>';
    unset($_SESSION['amountPaid']);
} else {
    echo "Error updating wallet balance: " . $conn->error;
}
?>

<!-- <script>
    // Set a flag to track if the page has already been refreshed
    var refreshed = false;

    // Delayed page redirect after 3 seconds (adjust the delay as needed)
    setTimeout(function () {
        // Check if the page has not been refreshed yet
        if (!refreshed) {
            window.location.reload();
            refreshed = true; // Set the flag to true after refreshing
        }
    }, 6000);
</script> -->

