<META NAME="robots" CONTENT="noindex,nofollow">


<?php 
		
			//start PHP session
	session_start();
	include('includes/conn.php');
	include('includes/config.php');
	//check if register form is submitted
	
	$sid = $_SESSION['alogin'];
	
	
	
	
	if(strlen($_SESSION['alogin'])=="")
    {   
    header("Location: login.php"); 
    }
    else{
		
		
    $img = $_POST['image'];
    $folderPath = "Verification/";
  
    $image_parts = explode(";base64,", $img);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];
  
    $image_base64 = base64_decode($image_parts[1]);
    $fileName = $sid. uniqid() . '.jpg';
  
    $file = $folderPath . $fileName;
    file_put_contents($file, $image_base64);
  
 
  
  
	$sql = "SELECT cname FROM users WHERE uname='". $sid. "'";
	$query = $dbh->prepare($sql);
	$query->execute();
	$results=$query->fetchAll(PDO::FETCH_OBJ);
	$cnt=1;

	
	if($query->rowCount() > 0)
	{                        
	foreach($results as $result)
	{   


	}} 




	if(isset($_POST['register'])){
		//assign variables to post values

		$userID = $_SESSION['alogin'];
		$name = $result->cname;
		$description = $_POST['description'];
		$principal = $_POST['principal'];
		$terms = $_POST['terms'];
		$paymentmode = $_POST['paymentmode'];
		$status = "PENDING";
		$IMG = $fileName;

				$str1 = $principal;
				$x = str_replace( ',', '', $str1);
				if( is_numeric($x))
				{
				echo $x."\n";
				}
  
				$principal = $x;
				//insert new loan to our database
				$stmt = $pdo->prepare('INSERT INTO tblloanapp (description, principal, terms, paymentmode, name, userID, status, IMG) VALUES (:description, :principal, :terms, :paymentmode, :name, :userID, :status, :IMG)');

				try{
					$stmt->execute(['description' => $description, 'principal' => $principal, 'terms' => $terms, 'paymentmode' => $paymentmode, 'name' => $name, 'userID' => $userID, 'status' => $status, 'IMG' => $IMG]);

					$_SESSION['success'] = 'Loan application successfully submitted!';
				}
				catch(PDOException $e){
					$_SESSION['error'] = 'Loan application submission failed! Try again. <br> Click &quot<strong>Applied Loans</strong>&quot to check loan status!';
			
				}

		
			
		
		
	}
	else{
		$_SESSION['error'] = 'Loan application submission failed! Try again. <br> Click &quot<strong>Applied Loans</strong>&quot to check loan status!'