<?php

if(isset($_POST['act']) && 
isset($_POST['symbol'])&& 
isset($_POST['bos'])&& 
isset($_POST['volume'])&& 
isset($_POST['profit2'])&&
isset($_POST['loss'])&&
isset($_POST['dtime'])&&
isset($_POST['etime'])){
	 
	 $act = $_POST['act'];
	 $symbol = $_POST['symbol'];
	 $bos = $_POST['bos'];
	 $volume = $_POST['volume'];
	 $profit = $_POST['profit2'];
	 $loss = $_POST['loss'];
	 $dtime = $_POST['dtime'];
	 $etime = $_POST['etime'];
	 
	if(!empty($_POST['act']) && 
!empty($_POST['symbol'])&& 
!empty($_POST['bos'])&& 
!empty($_POST['volume'])&& 
!empty($_POST['profit2'])&&
!empty($_POST['loss'])&&
!empty($_POST['dtime'])&&
!empty($_POST['etime'])){
	 
	 
	 $status = "closed";
	 $state = '2';
	 $mail =$act;
	 // $rdd = rand(1,50);
	 // $etime = date('d M Y H:i:s',time());
	 // if($rdd>10 && $rdd <20){ $extime = date('d M Y H:i:s',time()+5);}
	 // elseif($rdd>20 && $rdd<30){ $extime = date('d M Y H:i:s',time()+10);}
	 // elseif($rdd>30 && $rdd<40){ $extime = date('d M Y H:i:s',time()+20);}
	 // elseif($rdd>40 && $rdd<50){ $extime = date('d M Y H:i:s',time()+25);}
	 // else{ $extime = date('d M Y H:i:s',time()+30);}
	 
	 $query ="INSERT INTO trades2 VALUES(NuLL,'$dtime','$symbol','$bos','$volume','$status','$etime','$profit','$loss','$state','$act')";
	 
	 if(mysqli_query($con,$query)){
// 		 echo '<script> alert("trade Placed"); </SCRIPT>';
		 	// $_SESSION['success'] = "Trade Placed";
		 	$_SESSION['success'] = "Trade Placed";
					header('location:./');
					die();
	 }
 }
	 
 }







    if(isset($_POST['tp']) && isset($_POST['tp2'])){
        $tp = mysqli_real_escape_string($con,$_POST['tp']);
        $tp2 = mysqli_real_escape_string($con,$_POST['tp2']);
        
        if(!empty($tp)){
                
                $q = "update  trades2 set profit='$tp2'  where id = '$tp' ";
                if(mysqli_query($con,$q)){
                        echo '<script> alert("changed"); </script>';
                        	$_SESSION['success'] = "Profit Updated";
					header('location:./');
					die();
                }
        }
    }


 if(isset($_POST['tl']) && isset($_POST['tl2'])){
        $tl = mysqli_real_escape_string($con,$_POST['tl']);
        $tl2 = mysqli_real_escape_string($con,$_POST['tl2']);
        
        if(!empty($tl)){
                
                $q = "update  trades2 set loss='$tl2'  where id = '$tl' ";
                if(mysqli_query($con,$q)){
                        echo '<script> alert("changed"); </script>';
                                    // 	$maro = "alert('action Successful.')";
					$_SESSION['success'] = "Loss Updated";
					header('location:./');
					die();
                }
        }
    }

if(isset($_POST['del_trade'])){
    $trade_id = $_POST['del_trade'];
    if(!empty($trade_id)){
        
        $q ="delete from trades2 where id = '$trade_id'";
        if(mysqli_query($con,$q)){
            $_SESSION['success'] = "Trade Deleted";
					header('location:./');
					die();
        }
    }
    
}





function TRADE(){

global $con;

$query = "SELECT * FROM trades2 WHERE 1 ORDER BY id DESC";

	$result = mysqli_query($con,$query);
	$i = 0;
	while($query_row = mysqli_fetch_assoc($result)){
		
		$id = $query_row['id'];
		$dtime = $query_row['dtime'];
		$symbol = $query_row['symbol'];
		$bos = $query_row['bos'];
		$volume = $query_row['volume'];
		$status = $query_row['status'];
		$etime = $query_row['etime'];
		$profit = $query_row['profit'];
		$loss = $query_row['loss'];
		$state = $query_row['state'];
		$email = $query_row['email'];
		
	$i++;	
	
	
	$tr = '
			<tr> 
			<td> '.$i.'</td>
			<td>'.$dtime.' </td>
			<td> '.$symbol.'</td>
			<td>'.$bos.'</td>
			<td> '.$volume.'</td>
			<td> '.$status.'</td>
			<td> '.$etime.'</td>
			<td> '.$profit.'</td>
			<td> '.$loss.'</td>
			<td> '.$email.'</td>
<td style="white-space:nowrap;"><form method="POST"><input hidden name="tp" value="'.$id.'"> <input  name="tp2" value="" placeholder="enter amount"> <button type="submit" class="btn btn-success" >Profit</button></form></td>
<td  style="white-space:nowrap;"><form method="POST"><input hidden name="tl" value="'.$id.'">  <input  name="tl2" value="" placeholder="enter amount"><button type="submit" class="btn btn-danger" >Loss</button></form></td>
<td style="white-space:nowrap;"><form method="POST"><input hidden name="del_trade" value="'.$id.'"> <button type="submit" class="btn btn-danger" ><i class="fa fa-trash"></i></button></form></td>
			
			
			
			</tr>
	
	';
	echo $tr;
	}



}function TRADER(){
$mail =$_COOKIE['emails1'];


global $con;

$query = "SELECT * FROM trades WHERE email = '$mail' ORDER BY id DESC ";

	$result = mysqli_query($con,$query);
	$i = 0;
	while($query_row = mysqli_fetch_assoc($result)){
		
		$asset = $query_row['asset'];
		$option = $query_row['option'];
		$invested = $query_row['invested'];
		$erate = $query_row['erate'];
		$etime = $query_row['etime'];
		$status = $query_row['status'];
		$extime = $query_row['extime'];
		$email = $query_row['email'];
		$rtn = $query_row['return'];
		
	$i++;	
	
	
	$tr = '
			<tr> 
			<td> '.$i.'</td>
			<td> '.$asset.'</td>
			<td> '.$option.'</td>
			<td> '.$invested.'</td>
			<td> '.$rtn.'</td>
			<td> '.$erate.'</td>
			<td> '.$rate.'</td>
			<td> '.$etime.'</td>
			<td> '.$status.'</td>
			<td> '.$extime.'</td>
			
			
			
			</tr>
	
	';
	// echo $tr;
	}



}



?>