<?php 
include ('database.php');
session_start();

$id= $_SESSION['id'];
$results= mysqli_query($connection, "SELECT `date`, `id`, `name`, `email`, `number`, `password`, `phone`, `address`, `country`, `gender`, `image`, `vcode`, `verified`, `verify`, `balance`, `totalbalance`, `investbalance`, `profit`, `totaldeposit`, `totalwithdrawal`, `loanlimit`, `availableloan`, `ongoingloan`, `savings`, `idcard`, `idcardback`, `accountstatus`, `pina`, `pinb`, `pinc`, `pind` FROM users WHERE id ='$id'");
$retrive= mysqli_fetch_array($results);
$date = $retrive['date'];
$id = $retrive['id'];
$name = $retrive['name'];
$email = $retrive['email'];
$number = $retrive['number'];
$password = $retrive['password'];
$phone = $retrive['phone'];
$address = $retrive['address'];
$country = $retrive['country'];
$gender = $retrive['gender'];
$image = $retrive['image'];
$vcode = $retrive['vcode'];
$verified = $retrive['verified'];
$verify = $retrive['verify'];
$balance = $retrive['balance'];
$totalbalance = $retrive['totalbalance'];
$investbalance = $retrive['investbalance'];
$profit = $retrive['profit'];
$totaldeposit = $retrive['totaldeposit'];
$totalwithdrawal = $retrive['totalwithdrawal'];
$loanlimit = $retrive['loanlimit'];
$availableloan = $retrive['availableloan'];
$ongoingloan = $retrive['ongoingloan'];
$savings = $retrive['savings'];
$idcard = $retrive['idcard'];
$idcardback = $retrive['idcardback'];
$accountstatus = $retrive['accountstatus'];
$pina = $retrive['pina'];
$pinb = $retrive['pinb'];
$pinc = $retrive['pinc'];
$pind = $retrive['pind'];



if(isset($_POST['submit']));
{
    
   
	$actnumber = mysqli_real_escape_string($connection, $_POST['number']);
	 $amount = mysqli_real_escape_string($connection, $_POST['amount']);
	 $message = mysqli_real_escape_string($connection, $_POST['message']);
	 $bank = mysqli_real_escape_string($connection, $_POST['bank']);
	 $routing = mysqli_real_escape_string($connection, $_POST['routing']);
	 $swift = mysqli_real_escape_string($connection, $_POST['swift']);
	 $actname = mysqli_real_escape_string($connection, $_POST['actname']);
	$from = $number;
	$to = $actnumber;
	$icon= "arrow-forward-circle-outline";
	$colora= "red";
	$colorb= "green";
	$status="Pending";
	$signa= "-";
	$format= "1";
	$type= "Bank Transfer";
	$snotification = "sent";
	$smessage ='You Transfered $'.$amount.' to '.$actnumber.'';
if($amount > $balance){

	$_SESSION['status'] = "Insuficient Funds";
	 
	header("location: ../failed.php");  
}else{
	

	    
	$currentb= $balance - $amount;
		$currentotal= $totalbalance - $amount;
  
   $update_query= "UPDATE users SET balance='$currentb', totalbalance='$currentotal' WHERE id='$id' LIMIT 1";
   $update_query_run = mysqli_query($connection, $update_query);
  if($update_query_run)
  {
      
      
   $alert_query = "INSERT INTO `notifications` (`date`, `nid`, `id`, `subject`, `message`) VALUES (current_timestamp(), NULL, '$id', '$snotification', '$smessage')";
   $alert_query_run = mysqli_query($connection, $alert_query);
   
  
  $transaction_query = "INSERT INTO `transactions` (`date`, `tid`, `id`, `type`, `amount`, `from`, `to`, `icon`, `color`, `sign`, `message`, `status`, `actname`, `bankname`, `routing`, `swift`, `format`) VALUES (current_timestamp(), NULL, '$id', '$type', '$amount', '$from', '$to', '$icon', '$colora', '$signa', '$message', '$status', '$actname', '$bank', '$routing', '$swift', '$format')";	 
  $transaction_query_run = mysqli_query($connection, $transaction_query);
   $_SESSION['status'] = 'Your Transfer of $'.$amount.' to '.$actnumber.' is pending';
	
   header("location:../pending.php"); 
}
else
{
	$_SESSION['status'] = "Failed Try Again Later";
	 
  header("location: ../failed.php");  
}

}

	  }

  
?>