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

if(isset($_POST['submit']));
{
    
    $id = mysqli_real_escape_string($connection, $_POST['id']);
	$name = mysqli_real_escape_string($connection, $_POST['name']);
	$caddress = mysqli_real_escape_string($connection, $_POST['caddress']);
    $postcode = mysqli_real_escape_string($connection, $_POST['postcode']);
	$descountry = mysqli_real_escape_string($connection, $_POST['descountry']);
	 $descode = mysqli_real_escape_string($connection, $_POST['descode']);
	$imtcurrency = mysqli_real_escape_string($connection, $_POST['imtcurrency']);
	$currency = mysqli_real_escape_string($connection, $_POST['currency']);
	$imtamount = mysqli_real_escape_string($connection, $_POST['imtamount']);
    $payment = mysqli_real_escape_string($connection, $_POST['payment']);
	$bname = mysqli_real_escape_string($connection, $_POST['bname']);
	$bnumber = mysqli_real_escape_string($connection, $_POST['bnumber']);
    $baddress = mysqli_real_escape_string($connection, $_POST['baddress']);
	$bcity = mysqli_real_escape_string($connection, $_POST['bcity']);
	$country = mysqli_real_escape_string($connection, $_POST['country']);
    $bpostcode = mysqli_real_escape_string($connection, $_POST['bpostcode']);
	$bactnumber = mysqli_real_escape_string($connection, $_POST['bactnumber']);
	$swiftcode = mysqli_real_escape_string($connection, $_POST['swiftcode']);
	 $bankname = mysqli_real_escape_string($connection, $_POST['bankname']);
	$bankaddress = mysqli_real_escape_string($connection, $_POST['bankaddress']);
	$bankpostcode = mysqli_real_escape_string($connection, $_POST['bankpostcode']);
	$pot = mysqli_real_escape_string($connection, $_POST['pot']);
    $comment = mysqli_real_escape_string($connection, $_POST['comment']);
	$sic = mysqli_real_escape_string($connection, $_POST['sic']);
	$notification = "International Money Transfer";
	$icon= "send-outline";
	$icolor= "bg-success";
	$status="Pending";
	$scolor="text-warning";
	$amount= $_POST['imtamount'];
	$acolor= "text-warning";
	$avalue= "-$";
	$form= "To";
	$tcategory= "International Money Transfer";
	
   $verify_query = "SELECT identification, balance, id FROM users WHERE id='$id' LIMIT 1";
   $verify_query_run = mysqli_query($connection, $verify_query);

   if(mysqli_num_rows($verify_query_run) > 0)
   { 
      $row = mysqli_fetch_array($verify_query_run);
	  $identification = $row['identification'];
	  
	  if($identification == "1")
	  {
		  $bb= $row['balance'];
      if($bb > $amount || $bb == $amount)
      {
      $currentb= $bb - $amount;
	 $query = "INSERT INTO `imt` (`id`, `name`, `caddress`, `postcode`, `descountry`, `descode`, `imtcurrency`, `currency`, `imtamount`, `payment`, `bname`, `bnumber`, `baddress`, `bcity`, `country`, `bpostcode`, `bactnumber`, `swiftcode`, `bankname`, `bankaddress`, `bankpostcode`, `pot`, `comment`, `sic`) VALUES ('$id', '$name', '$caddress', '$postcode', '$descountry', '$descode', '$imtcurrency', '$currency', '$imtamount', '$payment', '$bname', '$bnumber', '$baddress', '$bcity', '$country', '$bpostcode', '$bactnumber', '$swiftcode', '$bankname', '$bankaddress', '$bankpostcode', '$pot', '$comment', '$sic')";
     $update_query= "UPDATE users SET balance='$currentb' WHERE id='$id' LIMIT 1";
	 $update_query_run =  mysqli_query($connection, $update_query);
	 $query_run = mysqli_query($connection, $query);
    if($query_run)
    {
	$notification_query = "INSERT INTO `notifications` (id, notification, icon, icolor, status, scolor, amount, acolor, avalue) VALUES ('$id', '$notification', '$icon', '$icolor', '$status', '$scolor', '$amount', '$acolor', '$avalue')";
     $notification_query_run = mysqli_query($connection, $notification_query);
    $transaction_query = "INSERT INTO `transactions` (`id`,`icon`, `icolor`, `status`, `scolor`, `form`, `tname`, `bname`, `tcategory`, `amount`, `avalue`, `acolor`) VALUES ('$id', '$icon', '$icolor', '$status', '$scolor', '$form', '$bactnumber', '$bankname', '$tcategory', '$amount', '$avalue', '$acolor')";	 
    $transaction_query_run = mysqli_query($connection, $transaction_query);
	 $_SESSION['status'] = "<h5 style='padding:8px; background: #1a1849; border-radius: 10px; color: #fff; text-align: center;'>International Money Transfer Request Is Successful</h5>";
      
	 header("location:../home.php"); 
 }
 else
 {
	  $_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>Failed Try Again Later</h5>";
       
	header("location: ../imt.php");  
 }
}else{
	 $_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>Insufficient Fund</h5>";
       
	header("location: ../imt.php");  
}
	  }else{
		  $_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>Failed Upgrade account</h5>";
       
	header("location: ../imt.php"); 
	  }
   }
   else{
	 $_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>Failed Upgrade account</h5>";
       
	header("location: ../imt.php");  
}
}
?>