<?php
ob_start();
?>
<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
 
$conn= new mysqli('das115.truehost.cloud','honyehtx_honyehtx','B7PcCzi46xP(*8','honyehtx_mavy')or die("Could not connect to mysql".mysqli_error($conn));
 

// Redirect to login if user is not logged in
if (!isset($_SESSION['email'])) {
    header("Location: ../auth/login.php");
    exit();
}

// 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['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']);
                    
                    // Generate unique transaction ID
                        $transid = strtoupper(substr(md5($email), 0, 14) . time() . "HFX");
                        
                        // Prepare the query (just once!)
                        $sql = "
                            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, $sql);
                        
                        //Redirect based on result
                        if ($result) {
                            header('Location: succes.php');
                            exit();
                        } else {
                            header('Location: platform.php');
                            exit();
                        }
                                                                
                }
                
               
    
    ?>

                <!-- SELLING OF TRADE -->
                
                
                
                 <?php
                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']);
                    
                    // Generate unique transaction ID
                        $transid = strtoupper(substr(md5($email), 0, 14) . time() . "HFX");
                        
                        // Prepare the query (just once!)
                        $sql = "
                            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, $sql);
                        
                       // Redirect based on result
                        if ($result) {
                            header('Location: succes.php');
                            exit();
                        } else {
                            header('Location: platform.php');
                            exit();
                        }
                                                                
                }
                
               
    
    ?>

    <?php
ob_end_flush();
?>
