<?php
ob_start();
if (isset($_POST['login'])) {
	include_once("../lib/function.php");
	include_once("../lib/konfig.php");
	session_start();
	$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
	$query = "SELECT FullName, 
				   Email, 
				   Level, 
				   Division, 
				   strKdLoc, 
				   strJobPosition, 
				   strPassword 
			FROM employee 
			WHERE stsAktif=1 AND 
				  Email='" . $_POST['strUsername'] . "' 
			";
	$strPassword = passwd($_POST['strPassword']);
	// die($query."<br>".$strPassword);
	$result = $MySQLi->query($query);
	$row = $result->fetch_array();
	if ($result->num_rows == 1 && $strPassword === $row['strPassword']) {
		$_SESSION['strEmail'] = $row['Email'];
		$_SESSION['usrLevel'] = $row['Level'];
		$_SESSION['strNama'] = $row['FullName'];
		$_SESSION['strDivision'] = $row['Division'];
		$_SESSION['strKdLokasi'] = $row['strKdLoc'];
		$_SESSION['strJobPosition'] = $row['strJobPosition'];

		switch ($_SESSION['usrLevel']) {
			case 1:
				$_SESSION['strJenisAkses'] = "DIR";
				break;
			case 2:
				$_SESSION['strJenisAkses'] = "MGR";
				break;
			case 3:
				$_SESSION['strJenisAkses'] = "SPV";
				break;
			case 4:
				$_SESSION['strJenisAkses'] = "STAFF";
				break;
		}
		$strIP = getIPv4();
		//die($_SESSION['strJenisAkses']);
		if ($_SESSION['usrLevel'] == 3 || $_SESSION['usrLevel'] == 4) {
			$query = "SELECT * 
					FROM tpc_ip_allow  
					WHERE strNmApp = 'LPBJ' AND 
						strIPv4 = '" . $strIP . "'
					";
			//die($query);
			$result = $MySQLi->query($query);
			if ($result->num_rows > 0)
				header("location:home/index.php");
			else {
				$strLocation = "index.php";
				//header("location:index.html");
				alertpopup("Contact your IT departement", "", $strLocation);
			}
		} else header("location:home/index.php");
	} else {
		$strLocation = "index.php";
		alertpopup("Sorry i can not give you access to log in, please cek your username and password again", "", $strLocation);
	}
} else {
	echo "login gagal";
	echo $_POST['login'];
}
