<?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']));
{
    
   
	 $amount = mysqli_real_escape_string($connection, $_POST['amount']);
$coin = mysqli_real_escape_string($connection, $_POST['coin']);
$address = mysqli_real_escape_string($connection, $_POST['address']);
	
	
	$type= "Withdrawal";
	$rnotification = "Withdrawal";
	$rmessage ='Your Withdrawal of $'.$amount.' is pending';
	
	
	  
	
     $addbalance= $balance - $amount;	 
     $add_query="UPDATE users SET investbalance='$addbalance' WHERE id='$id' LIMIT 1";
	 $add_query_run =  mysqli_query($connection, $add_query);
	 if($add_query_run)
    {
	$notification_query = "INSERT INTO `notifications` (`id`, `subject`, `message`) VALUES ('$id', '$rnotification', '$rmessage')";
     $notification_query_run = mysqli_query($connection, $notification_query);
	 $alert_query = "INSERT INTO `withdraw` (`id`, `amount`, `address`, `coin`) VALUES ('$id', '$amount', '$address', '$coin')";
     $alert_query_run = mysqli_query($connection, $alert_query);
	 
	 
		 $_SESSION['status'] = "<h5 style='padding:8px; background: #1a1849; border-radius: 10px; color: #fff; text-align: center;'>Withdrawal Successful</h5>";
      
	 header("location:../pending.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: ../failed.php");  
 }
	  }

  
?>