<?php

require_once('../sql_connect.php');
require_once('cookie.php');

if(isset($_POST['user']) && isset($_POST['pass'])){
	if(isset($_COOKIE['user_info'])){
		unset($_COOKIE['user_info']);
	}
	$u = $_POST['user'];
	$p = $_POST['pass'];

	if($sql_conn_login->connect_errno){
		die('Could not connect');
	}

	if (!($prepared = $sql_conn_login->prepare("SELECT username, admin FROM pico_ch2.users WHERE username = ? AND password = ?;"))) {
	    die("SQL error");
	}

	$prepared->bind_param('ss', $u, $p);
	
	if (!$prepared->execute()) {
	    die("SQL error");
	}
	
	if (!($result = $prepared->get_result())) {
	    die("SQL error");
	}

	$r = $result->fetch_all();

	if($result->num_rows === 1){
		$perm = new permissions($u, $p);
		setcookie('user_info', urlencode(base64_encode(serialize($perm))), time() + (86400 * 30), "/");
		header('Location: index.php?file=login');
	}
	else{
		$error = '<h6 class="text-center" style="color:red">Invalid Login.</h6>';
	}
	$sql_conn_login->close();
}
else if(isset($perm) && $perm->is_admin()){
	header('Location: index.php?file=admin');
	die();
}
else if(isset($perm)){
	header('Location: index.php?file=regular_user');
	die();
}

?>