<?php
require_once('../connect.php');
include("functions.php");
$error = "";
global $con;
if(isset($_SESSION['LoggedIn']) && $_SESSION['LoggedIn'] == 1){
	header("Location: index.php");
	return;
}
if(isset($_POST['logincode'])){
	if($_POST['logincode'] == ""){
		$error = "The login code was left blank.";
		$correct = false;
	}else{
		$logincode = mysqli_escape_string($con,$_POST['logincode']);
		$query = "SELECT * FROM ADMINISTRATOR WHERE MD5(Concat(Username,`Password`)) LIKE '$logincode'";
		
		$result = mysqli_query($con, $query);
		
		$row = mysqli_fetch_array($result);
		if(mysqli_num_rows($result) )
			$correct = true;
		else {
			$correct = false;
			$error = "Wrong login code";
		}
		
		//grabar logins
		LoginLog('A', $username, $correct);
		if($correct){
			$_SESSION['LoggedIn'] = 1;
			$_SESSION['isAdmin'] = true;
			$_SESSION['Username'] = $row['Username'];
			$_SESSION['ID'] = $row['id'];
			$_SESSION['LastLogin'] = $row['LastLogin'];
			$_SESSION['Level'] = $row['Level'];
			$query = "UPDATE ADMINISTRATOR SET LastLogin = DATE_ADD(NOW(), INTERVAL -4 HOUR) WHERE Username LIKE '$username'";
			$result = mysqli_query($con, $query);
			header("Location: index.php");
		}
	}
}

?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta charset="utf-8">
		<title><?= $config['CompanyName']; ?></title>
		<meta name="generator" content="Bootply" />
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link href="../css/bootstrap.min.css" rel="stylesheet">
		<!--[if lt IE 9]>
			<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
		<![endif]-->
		<link href="../css/styles.css" rel="stylesheet">
	</head>
	<body>
<!--login modal-->
<div id="loginModal" class="modal show" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
  <div class="modal-content">
      <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <img src="../img/Logo_500.png" class="img-responsive" style="width: auto;" alt="" />
      </div>
      <div class="modal-body">
		  <div class="alert alert-dismissable alert-danger" style="<?php if($error == ""){ echo "display: none;"; } ?>">
		    <button type="button" class="close" data-dismiss="alert">&times;</button>
		    <span id="errorMessage"><b>Error! </b><?= $error ?></span>
		  </div>
          <form method="post" id="logincodeform" name="logincodeform" class="form col-md-12 center-block">
            <div class="form-group">
              <input type="text" id="logincode" class="form-control input-lg" name="logincode" placeholder="Scan Login Code">
            </div>
            
            <div class="form-group">
              <button class="btn btn-primary btn-lg btn-block">Entrar</button>
              
            </div>
          </form>
      </div>
      <div class="modal-footer">
          <div class="col-md-12">
          
		  </div>	
      </div>
  </div>
  </div>
</div>
	<!-- script references -->
		<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
		<script src="../js/bootstrap.min.js"></script>
		<script>
			$('#logincode').on("keyup", function(e){
				if(e.keyCode === 0){
					$('#logincodeform').submit();
				}
				});
			$(document).ready(function(){
				$('#logincode').focus();
				});
		</script>
	</body>
</html>

