<?php
session_start();

//error_reporting(0);
require_once "config.php";
 
$displayTenant = "display:none";
$displayStaff = "display:none";
$displarErrMsg = "style='display:none'";
$errormsg = "";
	IF(isset($_GET["errormsg"])){
		$errormsg = $_GET["errormsg"];
		$login = $_GET["login"];
		
		if($login == "tenant"){
			$displayTenant="display:block";
			$displayStaff="display:none";
		}
		else if($login == "staff"){
			$displayTenant="display:none";
			$displayStaff="display:block";
		}
		$displarErrMsg = "display:block";
	}


//include "session.php";

if(isset($_POST["submit"])){
	
	echo "LOGIN1234: ".$login = $_POST["login"];
	echo "USERNAME: " . $username    = $_POST["username"];
	$password = $_POST["password"];
	$error_message = "";

	if ($login == "tenant") {
		 $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: ".$_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");
				exit();
			}
			else {
				$error_message = "Incorrect Username or Password!!!";
				header("Location: index.php?errormsg=$error_message&login=$login");
				exit();
			}
		} else {
			$error_message = "Incorrect Username or Password!!!";
			header("Location: index.php?errormsg=$error_message&login=$login");
			exit();
		}
	}
	
}
else if(isset($_POST["staff_login"])){
	//echo "YES!!";
	//echo "LOGIN1234: ".$login = $_POST["login"];
	$username = mysqli_real_escape_string($link, $_POST["username"]);  
    $password = mysqli_real_escape_string($link, $_POST['password']);  
	if ($username != "" && $password != "") {
		//ECHO "not empty";
		 $sqlStaff = "SELECT * FROM users WHERE username = '" . $username . "'" ;
		$runSqlStaff = mysqli_query($link,$sqlStaff );
		$num = mysqli_num_rows($runSqlStaff);
		if ($num == 1){
			while($row=mysqli_fetch_assoc($runSqlStaff)){
				// The hashed password retrieved from database 
				
				
				   $hash =  $row['password'];
				 "VERIFY: ". $verify = password_verify($password, $hash); 
				if ($verify) {
					$_SESSION["staff_id"]     = $row["id"];
					 "username: ". $_SESSION["username"]   = $row["username"];
					$_SESSION["staff_name"]   = $row["name"];
					$_SESSION["type"] = $row["type"];
					//$_SESSION["login"] = $login;
					$_SESSION["stafflog"] = true;
					$_SESSION["projectLocation"] = "";
					$_SESSION["projectName"] = "";
					$_SESSION["location_id"] = "";
					$_SESSION["menu"] = "Dashboard";
					
					
					//auto select location if PIC set for one location
					 // if($_SESSION["type"] == "staff"){
						if($_SESSION["username"] != "admin"){
						   $sqlLocations = "SELECT * FROM ( 
											(select EL.location_id, locations.location_name, projects.project_name from extra_locations as EL 
											inner join locations on locations.id = EL.location_id 
											inner join projects on locations.project_id = projects.id 
											inner join users on EL.user_id =  users.id
											where users.id = ". $_SESSION['staff_id'] .") 
										UNION 
											(select locations.id, locations.location_name, projects.project_name from locations 
											inner join users on locations.id = users.location_id 
											inner join projects on locations.project_id = projects.id
											where users.id = ". $_SESSION['staff_id'] .") 
										) results ORDER BY project_name ASC";
						
						$runLocations = mysqli_query($link,$sqlLocations);
						if($runLocations === FALSE){
							die(mysqli_error($link));//TODO: better error handling
						}
					}
					else if($_SESSION["username"] == "admin"){
					//else if($_SESSION["type"] == "admin"){
							 $sqlLocations = "select locations.*,projects.project_name, projects.id as project_id from locations 
								
								inner join projects on locations.project_id = projects.id
								where projects.status = 'enable' and projects.project_name != 'admin'
								ORDER BY `projects`.`project_name` ASC";
						$runLocations = mysqli_query($link, $sqlLocations);
						if($runLocations === FALSE){
							die(mysqli_error($link));
						}
					}
					$countListLocations = mysqli_num_rows($runLocations);
					if($countListLocations == 1){
								$resultLocations = mysqli_fetch_assoc($runLocations);
								$location_id = $resultLocations["id"];
								$location_name = $resultLocations["location_name"];
								$project_name = $resultLocations["project_name"];
								$projectLocation = $project_name ."-" . $location_name;
								$_SESSION["projectLocation"] = $projectLocation;
					}
					 
					
					header("Location: staff_dashboard.php?projectLocation=".$_SESSION["projectLocation"]."&menu=".$_SESSION["menu"]);
					//header("location:entry.php"); 
				} 
				else{
					ECHO $showError = "Invalid Credentials 1";
				} 
			}
		}
		else{
			ECHO $showError = "Invalid Credentials 2";
		}
		
	}
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="au theme template">
    <meta name="author" content="Hau Nguyen">
    <meta name="keywords" content="au theme template">

    <!-- Title Page-->
    <title>Login</title>

    <!-- Fontfaces CSS-->
    <link href="css/font-face.css" rel="stylesheet" media="all">
    <link href="vendor/font-awesome-4.7/css/font-awesome.min.css" rel="stylesheet" media="all">
    <link href="vendor/font-awesome-5/css/fontawesome-all.min.css" rel="stylesheet" media="all">
    <link href="vendor/mdi-font/css/material-design-iconic-font.min.css" rel="stylesheet" media="all">

    <!-- Bootstrap CSS-->
    <link href="vendor/bootstrap-4.1/bootstrap.min.css" rel="stylesheet" media="all">

    <!-- Vendor CSS-->
    <link href="vendor/animsition/animsition.min.css" rel="stylesheet" media="all">
    <link href="vendor/bootstrap-progressbar/bootstrap-progressbar-3.3.4.min.css" rel="stylesheet" media="all">
    <link href="vendor/wow/animate.css" rel="stylesheet" media="all">
    <link href="vendor/css-hamburgers/hamburgers.min.css" rel="stylesheet" media="all">
    <link href="vendor/slick/slick.css" rel="stylesheet" media="all">
    <link href="vendor/select2/select2.min.css" rel="stylesheet" media="all">
    <link href="vendor/perfect-scrollbar/perfect-scrollbar.css" rel="stylesheet" media="all">

    <!-- Main CSS-->
    <link href="css/theme.css" rel="stylesheet" media="all">

	<script text="javascript">
		function tenantClick(){
		  document.getElementById('tenantClick').style.display ='block';
		  document.getElementById('staffClick').style.display ='none';
		}
		function staffClick(){
		  document.getElementById('tenantClick').style.display ='none';
		  document.getElementById('staffClick').style.display ='block';
		}
		function tenantValidation()  
		{  
			alert("TENANT VALIDATION");
			var tenant_id=document.tenantValidation.username.value;  
			var tenant_ps=document.tenantValidation.password.value;  
			if(tenant_id.length=="" && tenant_ps.length=="") {  
				alert("User Name and Password fields are empty");  
				return false;  
			}  
			else  
			{  
				if(tenant_id.length=="") {  
					alert("Username is empty");  
					return false;  
				}   
				if (tenant_ps.length=="") {  
				alert("Password field is empty");  
				return false;  
				}  
			}                             
		}  
		function staffValidation()  
		{  
			var staff_id=document.staffValidation.username.value;  
			var staff_ps=document.staffValidation.password.value;  
			if(staff_id.length=="" && staff_ps.length=="") {  
				alert("User Name and Password fields are empty");  
				return false;  
			}  
			else  
			{  
				if(staff_id.length=="") {  
					alert("Username is empty");  
					return false;  
				}   
				if (staff_ps.length=="") {  
				alert("Password field is empty");  
				return false;  
				}  
			}                             
		}  
	</script>

</head>
<body class="animsition" >
<div class="table-wrapper-scroll-y my-custom-scrollbar">
    <div class="page-wrapper" >
        <div class="page-content--bge5" style="background-image: url('images/icon/phssb.jpg');">
            <div class="container" >
				
                <div class="login-wrap">
                    <div class="login-content">
                        <div class="login-logo">
                            <a href="#">
                                <img src="images/icon/PHSSBRENTPROP.png" alt="PHSSB PAYPRO V3">
                            </a>
                        </div>
						<div class="card-body">
                            <button type="button" class="btn btn-outline-primary btn-lg btn-block" onclick="tenantClick()">Tenant Login</button>
                            <button type="button" class="btn btn-outline-primary btn-lg btn-block" onclick="staffClick()">Staff Login</button>
						</div>
                        <div class="login-form" id="tenantClick" style='<?php echo $displayTenant?>;font-size:12px'>
							<div class="card">
								<div class="card-header">
									<strong>Tenant Login</strong>
									<small <?php echo $displarErrMsg?> >Please insert correct
										<code>username & password</code>
									</small>
								</div>
								<div class="card-body">
									<form action="index.php" method="post" name="tenantValidation" onsubmit = "tenantValidation()">
										<input type="hidden" name="login" value="tenant">
										<div class="form-group">
											<label>Username</label>
											<input class="au-input au-input--full" type="text" name="username" placeholder="Username">
										</div>
										<div class="form-group">
											<label>Password</label>
											<input class="au-input au-input--full" type="password" name="password" placeholder="Password">
										</div>
										<div class="login-checkbox">
											<label>
												<input type="checkbox" name="remember">Remember Me
											</label>
											<label>
												<a href="#">Forgotten Password?</a>
											</label>
										</div>
										<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">LOGIN</button>
									</form>
								</div>
                           </div>
                        </div>
						<div class="login-form" id="staffClick" style='<?php echo $displayStaff?>;font-size:12px'>
                            <div class="card">
								<div class="card-header">
									<strong>Staff Login</strong>
									<small <?php echo $displarErrMsg?>>Please insert correct
										<code>username & password</code>
									</small>
								</div>
								<div class="card-body">
									<form action="index.php" method="post" name="staffValidation" onsubmit = "staffValidation()">
										<input type="hidden" name="login" value="staff">
										
										<div class="form-group">
											<label>Username</label>
											<input class="au-input au-input--full" type="text" name="username" placeholder="Username">
										</div>
										<div class="form-group">
											<label>Password</label>
											<input class="au-input au-input--full" type="password" name="password" placeholder="Password">
										</div>
										<div class="login-checkbox">
											<label>
												<input type="checkbox" name="remember">Remember Me
											</label>
											<label>
												<a href="#">Forgotten Password?</a>
											</label>
										</div>
										<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit" name="staff_login">LOGIN</button>
									</form>
								</div>
							</div>
                        </div>
                    </div>
                </div>
                </div>
            </div>
        </div>

    </div>

    <!-- Jquery JS-->
    <script src="vendor/jquery-3.2.1.min.js"></script>
    <!-- Bootstrap JS-->
    <script src="vendor/bootstrap-4.1/popper.min.js"></script>
    <script src="vendor/bootstrap-4.1/bootstrap.min.js"></script>
    <!-- Vendor JS       -->
    <script src="vendor/slick/slick.min.js">
    </script>
    <script src="vendor/wow/wow.min.js"></script>
    <script src="vendor/animsition/animsition.min.js"></script>
    <script src="vendor/bootstrap-progressbar/bootstrap-progressbar.min.js">
    </script>
    <script src="vendor/counter-up/jquery.waypoints.min.js"></script>
    <script src="vendor/counter-up/jquery.counterup.min.js">
    </script>
    <script src="vendor/circle-progress/circle-progress.min.js"></script>
    <script src="vendor/perfect-scrollbar/perfect-scrollbar.js"></script>
    <script src="vendor/chartjs/Chart.bundle.min.js"></script>
    <script src="vendor/select2/select2.min.js">
    </script>

    <!-- Main JS-->
    <script src="js/main.js"></script>

</body>

</html>
<!-- end document-->