<?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 email, deposit, bonus, profits, balance 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();
}


    
   

    
    
    
    
    
if (isset($_POST['bankwithdraw'])) {
    // Sanitize input
    $accountnum = mysqli_real_escape_string($conn, $_POST['accountnum']);
    $accountname = mysqli_real_escape_string($conn, $_POST['accountname']);
    $bankname = mysqli_real_escape_string($conn, $_POST['bankname']);
    $routin = mysqli_real_escape_string($conn, $_POST['routin']);
    $amat = floatval(mysqli_real_escape_string($conn, $_POST['amat']));
    $emailorphone = mysqli_real_escape_string($conn, $_POST['emailorphone']);
    $id = strtoupper(substr(md5($emailorphone), 0, 14) . time() . "UID");

    // Step 1: Get balance from crytobaseusers table
    $checkBalance = mysqli_query($conn, "SELECT balance FROM user WHERE email = '$userEmail'");

    if (mysqli_num_rows($checkBalance) > 0) {
        $user = mysqli_fetch_assoc($checkBalance);
        $balance = floatval($user['balance']);

        // Step 2: Compare balance with withdrawal amount
        if ($balance < $amat) {
            echo "<script>alert('Error: Insufficient balance for withdrawal');</script>";
        } else {
            // Step 3: Insert into withdrawal table
            $insert = mysqli_query($conn, "INSERT INTO withdrawal (accountnum, accountname, bankname, routin, amat, emailaddress, transactionid) 
                                          VALUES ('$accountnum', '$accountname', '$bankname', '$routin', '$amat', '$emailorphone', '$id')");

            if ($insert) {
                header("Location:loading.php");
            } else {
              echo "MySQL Error: " . mysqli_error($conn);
            }
        }
        } else {
        echo "<script>alert('Error: User not found');</script>";
    }
}
    
   









// if (isset($_POST['bankwithdraw'])) {
//     // Sanitize inputs
//     $accountnum = mysqli_real_escape_string($conn, $_POST['accountnum']);
//     $accountname = mysqli_real_escape_string($conn, $_POST['accountname']);
//     $bankname = mysqli_real_escape_string($conn, $_POST['bankname']);
//     $routin = mysqli_real_escape_string($conn, $_POST['routin']);
//     $amat = mysqli_real_escape_string($conn, $_POST['amat']);
//     $emailorphone = mysqli_real_escape_string($conn, $_POST['emailorphone']);
//     $id = strtoupper(substr(md5($emailorphone), 0, 14) . time() . "UID");

//     // ✅ Step 1: Get user balance (FIXED VARIABLE NAME)
//     $checkBalance = mysqli_query($conn, "SELECT balance FROM user WHERE email = '$userEmail' LIMIT 1");

//     if (mysqli_num_rows($checkBalance) > 0) {
//         $user = mysqli_fetch_assoc($checkBalance);
//         $balance = $user['balance'];

//         // Step 2: Check if balance is enough
//         if ($balance < $amat) {
//             echo "<script>alert('Error: Insufficient balance for withdrawal');</script>";
//         } else {
//             // Step 3: Insert withdrawal request
//             $insert = mysqli_query($conn, "INSERT INTO withdrawal (accountnum, accountname, bankname, routin, amat, emailaddress, transactionid) 
//                                           VALUES ('$accountnum', '$accountname', '$bankname', '$routin', '$amat', '$emailorphone', '$id')");

//             if ($insert) {
//                 // ✅ Step 4: Deduct withdrawal amount from user's balance
//                     $balance = floatval($balance);
//                     $amat = floatval($amat);
//                 $newBalance = $balance - $amat;
//                 $updateBalance = mysqli_query($conn, "UPDATE user SET balance = '$newBalance' WHERE email = '$userEmail'");

//                 if ($updateBalance) {
//                     echo "<script>alert('Withdrawal request submitted successfully. Contact admin on livechat for support.');</script>";
//                 } else {
//                     echo "<script>alert('Error updating balance: " . mysqli_error($conn) . "');</script>";
//                 }
//             } else {
//                 echo "<script>alert('Error inserting withdrawal: " . mysqli_error($conn) . "');</script>";
//             }
//         }
//     } else {
//         echo "<script>alert('Error: User not found');</script>";
//     }
// }
















?>

    
    
    
    