<?php
ob_start();
?>
<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

 if (!isset($_SESSION['email'])) {
    header("Location: ../auth/login.php");
    exit();
}
	$conn= new mysqli('das115.truehost.cloud','honyehtx_honyehtx','B7PcCzi46xP(*8','honyehtx_mavy')or die("Could not connect to mysql".mysqli_error($conn));

// Get user email from session
$userEmail = $_SESSION['email'];

// Fetch user info from database
$query = "SELECT * FROM user WHERE email = '$userEmail' LIMIT 1";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) === 1) {
    $user = mysqli_fetch_assoc($result);
} else {
    echo "User not found.";
    exit();
    
}



?>

    
    
  <?php
	if (isset($_POST['stake1']))
	{
                $plan = mysqli_real_escape_string($conn, $_POST['plan']);
                $price = mysqli_real_escape_string($conn, $_POST['price']);
                $duration = mysqli_real_escape_string($conn, $_POST['duration']);
                $amount = mysqli_real_escape_string($conn, $_POST['amount']);
                $status = mysqli_real_escape_string($conn, $_POST['status']);
                $email = mysqli_real_escape_string($conn, $_POST['email']);
            
                // Get user's current balance
                $check_balance_query = "SELECT balance FROM user WHERE email = '$email'";
                $result = mysqli_query($conn, $check_balance_query);
            
                if (mysqli_num_rows($result) > 0) {
                    $row = mysqli_fetch_assoc($result);
                    $balance = $row['balance'];
            
                    if ($balance >= $amount) {
                        // User has enough balance
                        $query = "INSERT INTO plans (plan, price, duration, amount, status, emailaddress) 
                                  VALUES ('$plan', '$price', '$duration', '$amount', '$status', '$email')"; $insert_result = mysqli_query($conn, $query);

            if ($insert_result) {
                header("Location: succes.php");
                exit(); // Always use exit after header redirection
            } else {
                echo "<script>alert('Database error: " . mysqli_error($conn) . "'); window.location.href='plans.php';</script>";
            }
                                   $insert_result = mysqli_query($conn, $query);

            if ($insert_result) {
                header("Location: succes.php");
                exit(); // Always use exit after header redirection
            } else {
                echo "<script>alert('Database error: " . mysqli_error($conn) . "'); window.location.href='plans.php';</script>";
            }
            
                       
                    } else {
                        // Not enough funds
                        echo "<script>alert('Insufficient funds!'); window.location.href='plans.php';</script>";
                    }
                } else {
                    echo "<script>alert('User not found!'); window.location.href='plans.php';</script>";
                }
    }

    ?>







 <?php
	if (isset($_POST['stake2']))
	{
                $plan = mysqli_real_escape_string($conn, $_POST['plan']);
                $price = mysqli_real_escape_string($conn, $_POST['price']);
                $duration = mysqli_real_escape_string($conn, $_POST['duration']);
                $amount = mysqli_real_escape_string($conn, $_POST['amount']);
                $status = mysqli_real_escape_string($conn, $_POST['status']);
                $email = mysqli_real_escape_string($conn, $_POST['email']);
            
                // Get user's current balance
                $check_balance_query = "SELECT balance FROM user WHERE email = '$email'";
                $result = mysqli_query($conn, $check_balance_query);
            
                if (mysqli_num_rows($result) > 0) {
                    $row = mysqli_fetch_assoc($result);
                    $balance = $row['balance'];
            
                    if ($balance >= $amount) {
                        // User has enough balance
                        $query = "INSERT INTO plans (plan, price, duration, amount, status, emailaddress) 
                                  VALUES ('$plan', '$price', '$duration', '$amount', '$status', '$email')";
                                   $insert_result = mysqli_query($conn, $query);

            if ($insert_result) {
                header("Location: succes.php");
                exit(); // Always use exit after header redirection
            } else {
                echo "<script>alert('Database error: " . mysqli_error($conn) . "'); window.location.href='plans.php';</script>";
            }
            
                       
                    } else {
                        // Not enough funds
                        echo "<script>alert('Insufficient funds!');";
                    }
                } else {
                    echo "<script>alert('User not found!'); window.location.href='plans.php';</script>";
                }
    }

    ?>
    
    
    
    <?php
	if (isset($_POST['stake3']))
	{
                $plan = mysqli_real_escape_string($conn, $_POST['plan']);
                $price = mysqli_real_escape_string($conn, $_POST['price']);
                $duration = mysqli_real_escape_string($conn, $_POST['duration']);
                $amount = mysqli_real_escape_string($conn, $_POST['amount']);
                $status = mysqli_real_escape_string($conn, $_POST['status']);
                $email = mysqli_real_escape_string($conn, $_POST['email']);
            
                // Get user's current balance
                $check_balance_query = "SELECT balance FROM user WHERE email = '$email'";
                $result = mysqli_query($conn, $check_balance_query);
            
                if (mysqli_num_rows($result) > 0) {
                    $row = mysqli_fetch_assoc($result);
                    $balance = $row['balance'];
            
                    if ($balance >= $amount) {
                        // User has enough balance
                        $query = "INSERT INTO plans (plan, price, duration, amount, status, emailaddress) 
                                  VALUES ('$plan', '$price', '$duration', '$amount', '$status', '$email')";
                                 
                $insert_result = mysqli_query($conn, $query);

            if ($insert_result) {
                header("Location: succes.php");
                exit(); // Always use exit after header redirection
            } else {
                echo "<script>alert('Database error: " . mysqli_error($conn) . "'); window.location.href='plans.php';</script>";
            }
            
                       
                    } else {
                        // Not enough funds
                        echo "<script>alert('Insufficient funds!');";
                    }
                } else {
                    echo "<script>alert('User not found!'); window.location.href='plans.php';</script>";
                }
    }

    ?>






    <?php
if (isset($_POST['stake4'])) {
    $plan = mysqli_real_escape_string($conn, $_POST['plan']);
    $price = mysqli_real_escape_string($conn, $_POST['price']);
    $duration = mysqli_real_escape_string($conn, $_POST['duration']);
    $amount = mysqli_real_escape_string($conn, $_POST['amount']);
    $status = mysqli_real_escape_string($conn, $_POST['status']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);

    // Check user's balance
    $check_balance_query = "SELECT balance FROM user WHERE email = '$email'";
    $result = mysqli_query($conn, $check_balance_query);

    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        $balance = $row['balance'];

        if ($balance >= $amount) {
            // Insert the plan
            $query = "INSERT INTO plans (plan, price, duration, amount, status, emailaddress) 
                      VALUES ('$plan', '$price', '$duration', '$amount', '$status', '$email')";

            $insert_result = mysqli_query($conn, $query);

            if ($insert_result) {
                header("Location: succes.php");
                exit(); // Always use exit after header redirection
            } else {
                echo "<script>alert('Database error: " . mysqli_error($conn) . "'); window.location.href='plans.php';</script>";
            }

        } else {
            echo "<script>alert('Insufficient funds!'); window.location.href='plans.php';</script>";
        }
    } else {
        echo "<script>alert('User not found!'); window.location.href='plans.php';</script>";
    }
}
    ?>
    
    <?php
ob_end_flush();
?>
