<?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']);
		$divide= $amount / 1.2;
	$bank = "Goldlinebanking";
	$icon= "icon-group-dollar";
	$colorb= "critical";
	$status="Successful";
	$signb= "-";
	
	$type= "Loan";
	$rnotification = "Loan repayment";
	$rmessage ='You Payed $'.$amount.' to Goldlinebanking Loan';
	
	
	$newavalable= $availableloan + $divide;
	

      $newongoing= $ongoingloan - $repayment;
	  
	
     $addbalance= $balance - $amount;	 
     $add_query="UPDATE users SET balance='$addbalance', availableloan='$newavalable', ongoingloan='$newongoing' 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 `loan` (`id`, `amount`, `status`, `color`, `icon`, `sign`) VALUES ('$id', '$amount', '$status', '$colorb', '$icon', '$signb')";
     $alert_query_run = mysqli_query($connection, $alert_query);
	 
	 
		 $_SESSION['status'] ='You Payed $'.$amount.' to Goldlinebanking Loan';
      
	 header("location:../success.php"); 
 }
 else
 {
	  $_SESSION['status'] = "Failed Try Again Later";
       
	header("location: ../failed.php");  
 }
	  }

  
?>