<?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']);
	$repayment = mysqli_real_escape_string($connection, $_POST['repayment']);

	$bank = "Lenexbanking";
	$icon = "icon-group-dollar";
	$colorb = "success";
	$status = "Successful";
	$signb = "+";

	$type = "Loan";
	$rnotification = "Receive";
	$rmessage = 'You Received $' . $amount . ' from Lenexbanking Loan';


	$newavalable = $availableloan - $amount;


	$newongoing = $ongoingloan + $repayment;


	$addbalance = $balance + $amount;
	$addtotalbalance = $totalbalance + $amount;
	$add_query = "UPDATE users SET balance='$addbalance', totalbalance='$addtotalbalance', 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`, `repayment`, `status`, `color`, `icon`, `sign`) VALUES ('$id', '$amount', '$repayment', '$status', '$colorb', '$icon', '$signb')";
		$alert_query_run = mysqli_query($connection, $alert_query);


		$_SESSION['status'] = 'Your Loan of $' . $amount . ' from Lenexbanking Loan is successful';

		header("location:../success.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");
	}
}


?>