<?php 
session_start();
//include "session.php";
require_once "config.php";

 "login : "  . $login = $_POST["login"];
$username    = mysqli_real_escape_string($link, $_POST['username']);
 "PASSWORD: ".$password = mysqli_real_escape_string($link, $_POST['password']);
$error_message = "";
/*if (isset($_SESSION['user_id']) != "") {
	//header("Location: tenant_dashboard.php");
}
else if(isset($_SESSION['staff_id']) != ""){
	header("Location: index.php?errormsg=$error_message&login=$login");
}*/
if ($login == "tenant") {
	echo $tenantSQL = "SELECT * FROM users WHERE username = '" . $username . "'";
	$result = mysqli_query($link,$tenantSQL );
	if ($row = mysqli_fetch_array($result)) {
		$error_message = "";
		if(password_verify($password,  $row['password'])){
			$_SESSION['staff_id']     = $row['id'];
			$_SESSION['tenant_id']     = $row['tenant_id'];
			$_SESSION['username']   = $row['username'];
			$_SESSION["login"] = $login;
			$_SESSION["tenantLog"] = true;
			
			/* The password is correct. */
			header("Location: tenant_dashboard.php");
		}
		else {
			$error_message = "Incorrect Username or Password!!!";
			header("Location: index.php?errormsg=$error_message&login=$login");
		}
	} else {
		$error_message = "Incorrect Username or Password!!!";
		header("Location: index.php?errormsg=$error_message&login=$login");
	}
}
else if($login == "staff"){
	
	 $staffSQL = "SELECT * FROM users WHERE username = '" . $username . "'" ;
	$result = mysqli_query($link,$staffSQL );
	
	if ($row = mysqli_fetch_array($result)) {
		$error_message = "";
		//ECHO "password DATABASE: ".$row['password'];
		ECHO "PASSWORD VERIFY: ".$verify = password_verify($password,  $row['password']);
		if ($verify) { 
		echo 'Password Verified!'; 
		} else { 
		echo 'Incorrect Password!'; 
		} 
		//crypt('something','$2a$09$anexamplestringforsalt$')
		//ECHO "PASSWORD VERIFY: ".password_verify($password,  $row['password']);
		/* if(crypt($password,  $row['password'])){
			 "staff ID: ".$_SESSION['staff_id']     = $row['id'];
			$_SESSION['username']   = $row['username'];
			$_SESSION['staff_name']   = $row['name'];
			$_SESSION["type"] = $row['type'];
			$_SESSION["login"] = $login;
			$_SESSION["stafflog"] = true;
			$_SESSION["projectLocation"] = "";
			$_SESSION["location_id"] = "";
			$_SESSION["menu"] = "Dashboard";
			
			header("Location: staff_dashboard.php?on=menu=".$_SESSION["menu"]);
		}
		else {
			 $error_message = "1 Incorrect Username or Password!!! ";
			header("Location: index.php?errormsg=$error_message&login=$login");
		} */
	} else {
		 $error_message = "2 Incorrect Username or Password!!!";
		header("Location: index.php?errormsg=$error_message&login=$login");
	}
}

?>