<?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']))
	; {


	$usernumber = mysqli_real_escape_string($connection, $_POST['number']);
	$amount = mysqli_real_escape_string($connection, $_POST['amount']);
	$message = mysqli_real_escape_string($connection, $_POST['message']);
	$from = $number;
	$to = $usernumber;
	$bank = "Lenexbanking";
	$icon = "arrow-forward-circle-outline";
	$colora = "red";
	$colorb = "green";
	$status = "Completed";
	$signa = "-";
	$signb = "+";

	$type = "Transfer";
	$rnotification = "Receive";
	$snotification = "sent";
	$smessage = 'You Transfered $' . $amount . ' to ' . $usernumber . '';
	$rmessage = 'You Received $' . $amount . ' from ' . $number . '';





	$currentb = $balance - $amount;
	$currentotal = $totalbalance - $amount;
	$query = "SELECT balance, id, totalbalance, email FROM users WHERE number='$usernumber' LIMIT 1";
	$query_run = mysqli_query($connection, $query);
	if (mysqli_num_rows($query_run) > 0) {
		$srow = mysqli_fetch_array($query_run);
		$sbalance = $srow['balance'];
		$sid = $srow['id'];
		$semail = $srow['email'];
		$stotalbalance = $srow['totalbalance'];
		$addbalance = $sbalance + $amount;
		$addtotalbalance = $stotalbalance + $amount;
		$update_query = "UPDATE users SET balance='$currentb', totalbalance='$currentotal' WHERE id='$id' LIMIT 1";
		$add_query = "UPDATE users SET balance='$addbalance', totalbalance='$addtotalbalance' WHERE id='$sid' LIMIT 1";
		$add_query_run = mysqli_query($connection, $add_query);
		$update_query_run = mysqli_query($connection, $update_query);
		if ($add_query_run) {
			$notification_query = "INSERT INTO `notifications` (`id`, `subject`, `message`) VALUES ('$sid', '$rnotification', '$rmessage')";
			$notification_query_run = mysqli_query($connection, $notification_query);
			$alert_query = "INSERT INTO `notifications` (`id`, `subject`, `message`) VALUES ('$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);



			$transfer_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, '$sid', '$type', '$amount', '$to', '$from', '$icon', '$colorb', '$signb', '$message', '$status', '$actname', '$bank', '$routing', '$swift', '$format')";
			$transfer_query_run = mysqli_query($connection, $transfer_query);
			$alert = 'You Transfered $' . $amount . ' to ' . $usernumber . '';


			$emailheader = 'Received $' . $amount . '';
			$message = 'An amount of $' . $amount . ' was credited to your account by ' . $number . '';

			header("location:transfer-email.php?email=$semail&header=$emailheader&message=$message&alert=$alert");
		} else {
			$_SESSION['status'] = "Failed Try Again Later";

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

		header("location: ../failed.php");
	}
}


?>