<?php
session_start();
require("php_con.php");
require("function.php");

if(isset($_COOKIE['user_id'])){
echo "<script>window.location='dashboard';</script>";
}

$error_msg=0;

if(isset($_POST['log_bt'])){
	
	$user_username=mysqli_real_escape_string($connation,$_POST['user_username']);
	$user_password=md5(mysqli_real_escape_string($connation,$_POST['user_password']));
	
	$log_qury=mysqli_query($connation,"SELECT * FROM user_details WHERE user_username='$user_username'");
	
	if(mysqli_num_rows($log_qury)>0){
		
		$log_resalt=mysqli_fetch_array($log_qury);
		
		if($log_resalt['user_password']==$user_password){
			setcookie("user_id", $log_resalt['user_id'], time() + (86400 * 365), "/"); // 86400 = 1 day
			//active_log("Information: $log_resalt[user_username] successfully logged into the system.");
			echo "<script>window.location='dashboard';</script>";
		}
		else{
			//password not match
			//active_log("Login Error: Attempted to log in to the system with an incorrect password.");
			$error_msg=2;
		}
		
	}
	else{
		//user not found
		//active_log("Login Error: Attempted to log in to the system with an incorrect username.");
		$error_msg=1;
	}
	
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
<link href="data_table/css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="icon/css/font-awesome.css" rel="stylesheet" type="text/css">
<link href="icon/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>

<body class="log-bg">
	
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>
	
<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="50px;">Please Wait...</div></div>
	
<div class="main-login">

<p align="center" style="color: white; font-size: 14px;">Enter your Username and Password to enter the NOva POS System.</p>
	
	<?php if($error_msg==1){ ?><div class="error-msg"><strong>Sorry!</strong> User not found.</div><?php } ?>
	<?php if($error_msg==2){ ?><div class="error-msg"><strong>Sorry!</strong> Password not match.</div><?php } ?>
	
<form method="post" autocomplete="off">
	<input name="user_username" type="text" required placeholder="User name" autofocus value="<?php if(isset($_GET['guest'])){echo "guest";} ?>">
	<input name="user_password" type="password" required placeholder="Password" value="<?php if(isset($_GET['guest'])){echo "guest";} ?>">
	<button name="log_bt" type="submit">Login</button>
</form>
<p style="margin-bottom: 0px; margin-top: 0px;"><a href="#" style="text-decoration: none; color: black;" target="_blank"><i class="fa fa-book"></i> System Documentation</a></p>
<p style="font-size: 12px; color: black;">
System Powerd by <i class="fa fa-bolt text-danger"></i> <a style="font-size: 12px; color: black; font-weight: bold;" href="#" target="_blank">Nova Script</a><br>0769146421, 0777404233<br>
<img src="image/logo-sm.jpg" style="width: 60px; height: 60px; object-fit: contain; border-radius: 100%; background-color: white; padding: 3px; margin-top: 5px;"></p>
</div>
	
</body>
</html>
<?php @mysqli_close($connation); ?>