<?php

require_once "config/database.php";

$username = mysqli_real_escape_string($mysqli, stripslashes(strip_tags(htmlspecialchars(trim($_POST['username'])))));
$password = md5(mysqli_real_escape_string($mysqli, stripslashes(strip_tags(htmlspecialchars(trim($_POST['password']))))));

if (!ctype_alnum($username) OR !ctype_alnum($password)) {
	header("Location: index.php?alert=1");
}





else {

	$query = mysqli_query($mysqli, "SELECT * FROM usuarios WHERE username='$username' AND password='$password' AND status='activo' ")
									or die('error'.mysqli_error($mysqli));
	$rows  = mysqli_num_rows($query);

	if ($rows > 0) {
		$data  = mysqli_fetch_assoc($query);

		session_start();
		$_SESSION['id_user']   = $data['id_user'];
		$_SESSION['username']  = $data['username'];
		$_SESSION['password']  = $data['password'];
		$_SESSION['name_user'] = $data['name_user'];
		$_SESSION['permisos_acceso'] = $data['permisos_acceso'];
	
		#header("Location: main.php?module=start_psc");
		#and permisos_acceso='Admin' 
		

		$Modulo = $data['permisos_acceso'];
		
		
		if($Modulo == 'Permiso'){
			     header("Location: main.php?module=start_psc");
				 }
				 
				 

			elseif ($Modulo == 'Supervisor') {
				header("Location: main.php?module=start_supervisor");
			}	
			
			
			
	
			elseif ($Modulo == 'Admin') {
				header("Location: main.php?module=start");
			}	
			
			elseif ($Modulo == 'Almacenista') {
				header("Location: main.php?module=start_almacenista");
			}
			elseif ($Modulo == 'root') {
				header("Location: main.php?module=start");
			}
		
	}
	
	
	else {
		header("Location: index.php?alert=1");
	
	}
	
	
	
}

?>