<?php
ob_start();
session_start();

// require "connection.php";










// $errors = [];

// if (isset($_POST['signup'])) {
//     // Sanitize inputs
//     $fullname = trim($_POST['fullname'] ?? '');
//     $emailorphone = trim($_POST['emailorphone'] ?? '');
//     $country = trim($_POST['country'] ?? '');
//     $accounttype = trim($_POST['accounttype'] ?? '');
//     $currency = trim($_POST['currency'] ?? '');
//     $phonenumber = trim($_POST['phonenumber'] ?? '');
//     $password = $_POST['password'] ?? '';
//     $confirmpassword = $_POST['confirmpassword'] ?? '';

//     // Validate password match
//     if ($password !== $confirmpassword) {
//         $errors['password'] = "Passwords do not match!";
//     }

//     // Check if email already exists
//     $stmt = $conn->prepare("SELECT id FROM crytobaseusers WHERE emailaddress = ?");
//     $stmt->bind_param("s", $emailorphone);
//     $stmt->execute();
//     $stmt->store_result();

//     if ($stmt->num_rows > 0) {
//         $errors['emailorphone'] = "This email is already registered!";
//     }

//     $stmt->close();

//     // Proceed if no errors
//     if (empty($errors)) {
//         //️ Store plain text password (only for demo/learning)
//         $stmt = $conn->prepare("
//             INSERT INTO crytobaseusers (fullname, emailaddress, country, phonenumber, accounttype, currency, password)
//             VALUES (?, ?, ?, ?, ?, ?, ?)
//         ");
//         $stmt->bind_param("sssssss", $fullname, $emailorphone, $country, $phonenumber, $accounttype, $currency, $password);

//         if ($stmt->execute()) {
//             $_SESSION['fullname'] = $fullname;
//             $_SESSION['emailorphone'] = $emailorphone;
//             header('Location: login.php');
//             exit;
//         } else {
//             $errors['db'] = "Failed to register. Please try again.";
//         }

//         $stmt->close();
//     }

//     // Display error messages
//     foreach ($errors as $error) {
//         echo "<p style='color:red;'>$error</p>";
//     }
// }












//                           if (isset($_POST['login'])) {
//     $emailorphone = trim($_POST['emailorphone'] ?? '');
//     $password = trim($_POST['password'] ?? '');

//     // Check for empty fields
//     if (empty($emailorphone)) {
//         $loginerrors = 'Please enter email address or username.';
//     } elseif (empty($password)) {
//         $loginerrors = 'Please enter your password.';
//     } else {
//         // Use prepared statement
//         $stmt = $conn->prepare("SELECT * FROM crytobaseusers WHERE emailaddress = ? AND password = ?");
//         $stmt->bind_param("ss", $emailorphone, $password); // Assuming password is stored in plain text (NOT recommended)

//         $stmt->execute();
//         $result = $stmt->get_result();

//         if ($result && $result->num_rows === 1) {
//             $user = $result->fetch_assoc();

//             $_SESSION['fullname'] = $user['fullname']; // assuming your DB has a 'fullname' column
//             $_SESSION['emailorphone'] = $user['emailorphone'];

//             header('Location: ../dashboard');
//             exit;
//         } else {
//             $loginerrors = "Invalid Email or Password.";
//         }

//         $stmt->close();
//     }

//     // Optional: Display error if set
//     if (isset($loginerrors)) {
//         echo "<p style='color:red;'>$loginerrors</p>";
//     }
// }


require "connection.php";

// Always initialize your error arrays
$errors = [];
$loginerrors = '';

// =======================
// 1. Registration Handler
// =======================
if (isset($_POST['signup'])) {
    $fullname = trim($_POST['fullname'] ?? '');
    $emailorphone = trim($_POST['emailorphone'] ?? '');
    $country = trim($_POST['country'] ?? '');
    $accounttype = trim($_POST['accounttype'] ?? '');
    $currency = trim($_POST['currency'] ?? '');
    $phonenumber = trim($_POST['phonenumber'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirmpassword = $_POST['confirmpassword'] ?? '';

    // Validate password match
    if ($password !== $confirmpassword) {
        $errors[] = "Passwords do not match!";
    }

    // Check if email already exists
    $stmt = $conn->prepare("SELECT id FROM crytobaseusers WHERE emailaddress = ?");
    $stmt->bind_param("s", $emailorphone);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $errors[] = "This email is already registered!";
    }

    $stmt->close();

    // Proceed to insert if no errors
    if (empty($errors)) {
        $stmt = $conn->prepare("
            INSERT INTO crytobaseusers (fullname, emailaddress, country, phonenumber, accounttype, currency, password)
            VALUES (?, ?, ?, ?, ?, ?, ?)
        ");
        $stmt->bind_param("sssssss", $fullname, $emailorphone, $country, $phonenumber, $accounttype, $currency, $password);

        if ($stmt->execute()) {
            $_SESSION['fullname'] = $fullname;
            $_SESSION['emailorphone'] = $emailorphone;
            header('Location: login.php');
            exit;
        } else {
            $errors[] = "Failed to register. Please try again.";
        }

        $stmt->close();
    }

    // Display registration errors
    foreach ($errors as $error) {
        echo "<p style='color:red;'>$error</p>";
    }
}

// =======================
// 2. Login Handler
// =======================
if (isset($_POST['login'])) {
    $emailorphone = trim($_POST['emailorphone'] ?? '');
    $password = trim($_POST['password'] ?? '');

    if (empty($emailorphone)) {
        $loginerrors = "Please enter email address or username.";
    } elseif (empty($password)) {
        $loginerrors = "Please enter your password.";
    } else {
        $stmt = $conn->prepare("SELECT * FROM crytobaseusers WHERE emailaddress = ? AND password = ?");
        $stmt->bind_param("ss", $emailorphone, $password);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result && $result->num_rows === 1) {
            $user = $result->fetch_assoc();
            
            $_SESSION['fullname'] = $user['fullname'];
            $_SESSION['emailorphone'] = $user['emailaddress']; 
            
            header('Location: ../dashboard');
            exit();
        } else {
            $loginerrors = "Invalid Email or Password.";
        }

        $stmt->close();
    }

    // Show login error if any
    if (!empty($loginerrors)) {
        echo "<p style='color:red;'>$loginerrors</p>";
    }
}
?>


































<?php
        
                $payment_method_typeerror='';
                $amounterror='';
    
        
                if(isset($_POST['deposit'])){
                    
                    
                
                    $method=mysqli_real_escape_string($conn, $_POST['method']);
                    $amount=mysqli_real_escape_string($conn, $_POST['amount']);
                    $emailaddress=mysqli_real_escape_string($conn, $_POST['emailaddress']);
                    $status=mysqli_real_escape_string($conn, $_POST['status']);
                    
                    $id = strtoupper(substr(md5($emailaddress), 0,14).time()."UID");
                
                
                
            
            
            
                            
                            if(empty($amount)==false  and  empty($emailaddress)==false){
                            
                            $query="insert into deposit(method, amount, emailaddress, status, transactionid)values('$method', '$amount', '$emailaddress', '$status', '$id')";
                             mysqli_query($conn, $query);
                             
                        if($conn){
                
                         echo '<script>window.location="funding.php"</script>';
                        
                         
                         
                     }
             
                            }
                     
                }
    
    ?>




<?php
            if(isset($_POST['sub']))
            {
                $ssn = mysqli_real_escape_string($conn, $_POST['ssn']);
                $dob = mysqli_real_escape_string($conn, $_POST['dob']);
                $houseadd = mysqli_real_escape_string($conn, $_POST['houseadd']);
                $phone = mysqli_real_escape_string($conn, $_POST['phone']);
               

                $filename = $_FILES['image']['name'];
                $tempname = $_FILES['image']['tmp_name'];
                $folder = 'kycimages/'.$filename;
                
                $query = mysqli_query($conn, "insert into kyc (ssn, dob, houseadd, phone, file) values('$ssn', '$dob', '$houseadd', '$phone', '$filename')");
                if(move_uploaded_file($tempname, $folder)){
                    echo  "<h2>File uploaded successfully</h2>";

                }
                else{
                    echo  "<h2>File not uploaded</h2> ";
                }
            }






?>



    <!-- withdrawing  -->

    <?php

    //           rewrite a code for me from same database but diffrent tables called crytobaseusers and withdraw add if balance from crytobaseusers table is less than amount from withdraw then display an error message
		
				// if(isset($_POST['bankwithdraw'])){
					
					
				
				// 	$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");

    //                 $query = mysqli_query($conn, "insert into withdrawal (accountnum, accountname, bankname, routin, amat, emailaddress, transactionid) values('$accountnum', '$accountname', '$bankname', '$routin', '$amat', '$emailorphone', '$id')");
    //                 mysqli_query($conn, $query);

                    


                   
					
				
    //             }


    
    
    ?>
    <!--    FOR COPY TRADING -->
    
    <?php
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 crytobaseusers WHERE emailaddress = '$emailorphone'");

    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) {
                echo "<script>alert('Withdrawal request submitted successfully also contact admin on livechat for support');</script>";
            } else {
               echo "MySQL Error: " . mysqli_error($conn);
            }
        }
    } else {
        echo "<script>alert('Error: User not found');</script>";
    }
}
?>

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    <?php
   
                 
        if(isset($_POST['linkone']))
        {
           
             $name = mysqli_real_escape_string($conn, $_POST['name']);
            $fee = mysqli_real_escape_string($conn, $_POST['fee']);
            $profit = mysqli_real_escape_string($conn, $_POST['profit']);
             $emailorphon = mysqli_real_escape_string($conn, $_POST['emailorphon']);
             $query = "INSERT INTO trade (name, fee, profit, emailaddres)
                        values('$name', '$fee', '$profit', '$emailorphon')";
            $data = mysqli_query($conn, $query);
            if( mysqli_num_rows($data) > 1){
		     header('location: success.php');
		}else{
		    header('location: copy_trader.php');
		    
		}
        }
        
    ?>




     <?php
   
                 
        if(isset($_POST['linktwo']))
        {
           
             $name = mysqli_real_escape_string($conn, $_POST['name']);
            $fee = mysqli_real_escape_string($conn, $_POST['fee']);
            $profit = mysqli_real_escape_string($conn, $_POST['profit']);
             $emailorphon = mysqli_real_escape_string($conn, $_POST['emailorphon']);
             $query = "INSERT INTO trade (name, fee, profit, emailaddres)
                        values('$name', '$fee', '$profit', '$emailorphon')";
            $data = mysqli_query($conn, $query);
            if( mysqli_num_rows($data) > 1){
		     header('location: success.php');
		}else{
		    header('location: copy_trader.php');
		    
		}
        }
        
    ?>
       <?php
   
                 
        if(isset($_POST['linktree']))
        {
           
             $name = mysqli_real_escape_string($conn, $_POST['name']);
            $fee = mysqli_real_escape_string($conn, $_POST['fee']);
            $profit = mysqli_real_escape_string($conn, $_POST['profit']);
             $emailorphon = mysqli_real_escape_string($conn, $_POST['emailorphon']);
             $query = "INSERT INTO trade (name, fee, profit, emailaddres)
                        values('$name', '$fee', '$profit', '$emailorphon')";
            $data = mysqli_query($conn, $query);
            if( mysqli_num_rows($data) > 1){
		     header('location: success.php');
		}else{
		    header('location: copy_trader.php');
		    
		}
        }
        
    ?>
     <?php
   
                 
        if(isset($_POST['linkfour']))
        {
           
             $name = mysqli_real_escape_string($conn, $_POST['name']);
            $fee = mysqli_real_escape_string($conn, $_POST['fee']);
            $profit = mysqli_real_escape_string($conn, $_POST['profit']);
             $emailorphon = mysqli_real_escape_string($conn, $_POST['emailorphon']);
             $query = "INSERT INTO trade (name, fee, profit, emailaddres)
                        values('$name', '$fee', '$profit', '$emailorphon')";
            $data = mysqli_query($conn, $query);
            if( mysqli_num_rows($data) > 1){
		     header('location: success.php');
		}else{
		    header('location: copy_trader.php');
		    
		}
        }
        
    ?>
     <?php
   
                 
        if(isset($_POST['linkfive']))
        {
           
             $name = mysqli_real_escape_string($conn, $_POST['name']);
            $fee = mysqli_real_escape_string($conn, $_POST['fee']);
            $profit = mysqli_real_escape_string($conn, $_POST['profit']);
             $emailorphon = mysqli_real_escape_string($conn, $_POST['emailorphon']);
             $query = "INSERT INTO trade (name, fee, profit, emailaddres)
                        values('$name', '$fee', '$profit', '$emailorphon')";
            $data = mysqli_query($conn, $query);
            if( mysqli_num_rows($data) > 1){
		     header('location: success.php');
		}else{
		    header('location: copy_trader.php');
		    
		}
        }
        
    ?>

 <?php

                $payment_method_typeerror='';
                $amounterror='';
	
		
				if(isset($_POST['buy'])){
					
					
				
					$tpair=mysqli_real_escape_string($conn, $_POST['tpair']);
					$tprice=mysqli_real_escape_string($conn, $_POST['tprice']);
					$volume=mysqli_real_escape_string($conn, $_POST['volume']);
					$tloss	=mysqli_real_escape_string($conn, $_POST['tloss']);
					$tprofit=mysqli_real_escape_string($conn, $_POST['tprofit']);
                    $buy=mysqli_real_escape_string($conn, $_POST['buy']);
                    $status=mysqli_real_escape_string($conn, $_POST['status']);
                    $email = mysqli_real_escape_string($conn, $_POST['email']);
                    
                    $transid = strtoupper(substr(md5($emailorphone), 0,14).time()."HFX");
                    $query = mysqli_query($conn, "insert into tradehistory (transid, type, tpair, tprice, volume, stoploss, takeprofit, status, emailaddress) 
                    values('$transid', '$buy', '$tpair', '$tprice', '$volume', '$tloss', '$tprofit', '$status', '$email')");
                  $result =  mysqli_query($conn, $query);
                 if( mysqli_num_rows($result) > 1)
                  {
                    header('location: success.php');
                    }else{

                        header('location: platform.php');

                    } 
                                   
                    
		

                    


                   
					
				
                }


    
    
    ?>

                <!-- SELLING OF TRADE -->
<?php

                $payment_method_typeerror='';
                $amounterror='';
	
		
				if(isset($_POST['sell'])){
					
					
				
					$tpair=mysqli_real_escape_string($conn, $_POST['tpair']);
					$tprice=mysqli_real_escape_string($conn, $_POST['tprice']);
					$volume=mysqli_real_escape_string($conn, $_POST['volume']);
					$tloss	=mysqli_real_escape_string($conn, $_POST['tloss']);
					$tprofit=mysqli_real_escape_string($conn, $_POST['tprofit']);
                    $sell=mysqli_real_escape_string($conn, $_POST['sell']);
                    $status=mysqli_real_escape_string($conn, $_POST['status']);
                    $email = mysqli_real_escape_string($conn, $_POST['email']);
                    
                    $transid = strtoupper(substr(md5($emailorphone), 0,14).time()."HFX");
                    $query = mysqli_query($conn, "insert into tradehistory (transid, type, tpair, tprice, volume, stoploss, takeprofit, status, emailaddress) 
                    values('$transid', '$sell', '$tpair', '$tprice', '$volume', '$tloss', '$tprofit', '$status', '$email')");
                  $result =  mysqli_query($conn, $query);
                 if( mysqli_num_rows($result) > 1)
                  {
                    header('location: success.php');
                    }else{

                        header('location: platform.php');

                    } 
                                   
                    
		

                    


                   
					
				
                }


    
    
    ?>

    <!-- STARTER PLANS BEGINES FROM HERE -->


    <?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']);

					
                    $query = mysqli_query($conn, "insert into plans (plan, price, duration, amount, status, emailaddress) 
                    values('$plan', '$price', '$duration', '$amount', '$status', '$email')");
                  $result =  mysqli_query($conn, $query);
                 if( mysqli_num_rows($result) > 1)
                  {
                    header('location: success.php');
                    }else{

                        header('location: plans.php');

                    } 
                        
                }

    ?>


 <?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']);

					
                    $query = mysqli_query($conn, "insert into plans (plan, price, duration, amount, status, emailaddress) 
                    values('$plan', '$price', '$duration', '$amount', '$status', '$email')");
                  $result =  mysqli_query($conn, $query);
                 if( mysqli_num_rows($result) > 1)
                  {
                    header('location: success.php');
                    }else{

                        header('location: plans.php');

                    } 
                        
                }

    ?>


<?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']);

					
                    $query = mysqli_query($conn, "insert into plans (plan, price, duration, amount, status, emailaddress) 
                    values('$plan', '$price', '$duration', '$amount', '$status', '$email')");
                  $result =  mysqli_query($conn, $query);
                 if( mysqli_num_rows($result) > 1)
                  {
                    header('location: success.php');
                    }else{

                        header('location: plans.php');

                    } 
                        
                }

    ?>


<?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']);

					
                    $query = mysqli_query($conn, "insert into plans (plan, price, duration, amount, status, emailaddress) 
                    values('$plan', '$price', '$duration', '$amount', '$status', '$email')");
                  $result =  mysqli_query($conn, $query);
                 if( mysqli_num_rows($result) > 1)
                  {
                    header('location: success.php');
                    }else{

                        header('location: plans.php');

                    } 
                        
                }

    ?>
    
    
    
    
    <?php
// Connect to the database first
// Example: $conn = mysqli_connect("localhost", "username", "password", "your_database");

if (isset($_POST['update'])) {
    $emailorphone = mysqli_real_escape_string($conn, $_POST['emailorphone']);
    $newpassword = mysqli_real_escape_string($conn, $_POST['newpassword']);
    $confirmpassword = mysqli_real_escape_string($conn, $_POST['confirmpassword']);

    // Check if passwords match
    if ($newassword !== $confirmPassword) {
        echo "<script>alert('Error: Passwords do not match.');</script>";
    } else {
        // Update password directly (NOT HASHED — demo only)
        $update = mysqli_query($conn, "UPDATE crytobaseusers SET password = '$newPassword' WHERE emailaddress = '$emailorphone'");

        if ($update) {
            echo "<script>alert('Password updated successfully.');</script>";
        } else {
            echo "<script>alert('Error: Could not update password.');</script>";
            echo "MySQL Error: " . mysqli_error($conn);
        }
    }
}
?>

    



