<?php 
session_start();
require('connect.php');
mysqli_set_charset($connection,'utf8');
//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])){
//3.1.1 Assigning posted values to variables.
$username = $_POST['username'];
$password = $_POST['password'];



?>

<!doctype html>
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		
		<title>Administración de cuentas</title>

		<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs-3.3.7/jqc-1.12.4/moment-2.18.1/dt-1.10.18/b-1.5.6/sl-1.3.0/datatables.min.css">
		<link rel="stylesheet" type="text/css" href="css/generator-base.css">
		<link rel="stylesheet" type="text/css" href="css/editor.bootstrap.min.css">

		<script type="text/javascript" charset="utf-8" src="https://cdn.datatables.net/v/bs-3.3.7/jqc-1.12.4/moment-2.18.1/dt-1.10.18/b-1.5.6/sl-1.3.0/datatables.min.js"></script>
		<script type="text/javascript" charset="utf-8" src="js/dataTables.editor.min.js"></script>
		<script type="text/javascript" charset="utf-8" src="js/editor.bootstrap.min.js"></script>
		<script type="text/javascript" charset="utf-8" src="js/table.login.js"></script>
	</head>
	<a href="">

	<?php  //Start the Session

//3.1.2 Checking the values are existing in the database or not
$query = "SELECT * FROM `login` WHERE login='$username' and password='$password'and administrador='administrador'";



$result = mysqli_query($connection, $query) or die(mysqli_error($connection));


$count = mysqli_num_rows($result);
//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
if ($count > 0){
$_SESSION['username'] = $username;
}else{
//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
echo "
<body class='bootstrap'>
	<div class='container'>
	<h2>Credenciales Inválidas</h2>
	<a href='index.php' class='btn btn-danger' role='button'>Intente de nuevo</a>
	</div>
</body>		
";
}
}
//3.1.4 if the user is logged in Greets the user with message
if (isset($_SESSION['username'])){
	$username = $_SESSION['username'];


echo"
	<body class='bootstrap'>
		<div class='container'>

		<img src='../reporte/img/compo.png'>
		<p>Tabla de administración de usuarios</p>
		<hr>
		<a href='logout.php' class='btn btn-warning' role='button'>Cerrar sesión</a>
		<hr>

			<h1>
				Tabla de administración de usuarios
			</h1>
			
			<table cellpadding='0' cellspacing='0' border='0' class='table table-striped table-bordered' id='login' width='100%'>
				<thead>
					<tr>
						<th>login</th>
						<th>password</th>
						<th>administrador</th>
					</tr>
				</thead>
			</table>

		</div>
	</body>
</html>
";
}else{
	//3.2 When the user visits the page first time, simple login form will be displayed.
	echo "
	<body class='bootstrap'>
		<div class='container'>
		<h2>Tabla de administración</h2>
		<a href='index.php' class='btn btn-danger' role='button'>Ingresar</a>
		</div>
	</body>		
	";
	
	}
	?>	