<?php
require_once('../connect.php');
include("functions.php");
$error = "";
$requested = isset($_REQUEST['r'])?$_REQUEST['r']:"";
$url = "index.php";
if ($requested != "") {
	$url = urldecode($requested);
}
global $con;

if(isset($_POST['username']) && isset($_POST['password'])){
	if($_POST['username'] == "" || $_POST['password'] == ""){
		$error = "The username or password was left blank.";
		$correct = false;
	}else{
		$username = mysqli_escape_string($con,$_POST['username']);
		$password = mysqli_escape_string($con,$_POST['password']);
		$query = "SELECT * FROM ADMINISTRATOR WHERE Username LIKE '$username'";
		
		$result = mysqli_query($con, $query);
		
		$row = mysqli_fetch_array($result);
		if($row['Password'] == $password )
			$correct = true;
		else {
			$correct = false;
			$error = "El n&uacute;mero de cuenta o contrase&ntilde;a no es v&aacute;lida";
		}
		
		//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: $url");
		}
	}
}

?>
<!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" class="form col-md-12 center-block">
            <div class="form-group">
              <input type="text" class="form-control input-lg" name="username" placeholder="Nombre de Usuario">
            </div>
            <div class="form-group">
              <input type="password" class="form-control input-lg" name="password" placeholder="Contraseña">
            </div>
            <div class="form-group">
              <button class="btn btn-primary btn-lg btn-block">Entrar</button>
              <span class="pull-right"><a href="#">¿Olvidó su contraseña?</a></span>
            </div>
          </form>
      </div>
      <div class="modal-footer">
          <div class="col-md-12">
          <a href="mobilelogin.php">Mobile Login</a>
		  </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>
	</body>
</html>

