<?php
if(isset($_SESSION['admin']) || isset($_SESSION['adminId'])) {
	unset($_SESSION['target_page']);
} else {
	$_SESSION['target_page']= $_SERVER['REQUEST_URI'];
	header("Location: index.php?page=login");
	exit();
}

if(isset($_SESSION['admin']) || isset($_SESSION['adminId'])) {
	
	//print_r($_SESSION);
	/*if($page!='home' && $page!='logout' && $page!='settings' && $page!='request' && $page!='manage_profile') {
		if($_SESSION['launch']=='') {
			header('Location:index.php?page=home');
			exit;
		}
	}*/
	
	if($_SESSION['adminId']!=1 && $_SESSION['adminId']!=2){
	$getPageDet = "SELECT access FROM tbl_admin WHERE admin_id = ".$_SESSION['adminId'];		
		$execGetPageDet  = mysqli_query($con, $getPageDet);
		if(mysqli_num_rows($execGetPageDet) > 0) {
			$resGetPageDet = mysqli_fetch_array($execGetPageDet);
			$pageStr = $resGetPageDet['access'];				
			$adminPageArray = explode(',' , $pageStr);
			
			//$page = mysqli_real_escape_string($con, trim($_REQUEST['page']));
			if($page!='home' && $page!='logout' && $page!='login'){
				//get page id from accesss control
				$getPage = "SELECT id, pages FROM tbl_access_control WHERE page = '".$page."'";
				$execPage  = mysqli_fetch_array(mysqli_query($con, $getPage));
				$pageId = $execPage['id'];
				$pages = explode(',',$execPage['pages']);
				//print_r($pages);
				
				/*if(!in_array($pageId, $adminPageArray)) {
				//if(!in_array($_REQUEST['page'], $pages)) {			
					?>
					<script language="javascript">
					alert("You have no sufficient previleges to access this page");
					window.location="./index.php?page=home";
					</script>
					<?php
					exit();
				}*/
			}			
			
		}
		
		}
	
	
	
} else {
	header("Location: index.php?page=login");
	exit();
}

?>
