<?php
date_default_timezone_set('Africa/Mogadishu');

if ((isset($_SESSION['userName']) == true) && ($_SESSION['userName']) != "") {

	// This will be used fore web hosting time
    // This supports only HTTPS
    
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
         $fullURL = "https://";   
    } else {
         $fullURL = "http://"; 
    }
    
    $fullURL.= $_SERVER['HTTP_HOST']; $fullURL.= $_SERVER['REQUEST_URI']; $UrL = explode("/", $fullURL); $realURL = $UrL[3];
	
	//This is for localhost
    
// 	$fullURL = $_SERVER["REQUEST_URI"];
// 	$UrL = explode(".", $fullURL);
// 	$cutEx = $UrL[0];
// 	$sliceUrl = explode("/", $cutEx);
// 	$realURL = $sliceUrl[2];
    

    // echo "<script>alert('" . $realURL . "')</script>";

	$getUserPrivilege = mysqli_query($conn, "SELECT employees.employeePosition 
	FROM employees 
	JOIN system_users ON employees.employeeID = system_users.employeeID AND system_users.userID = '" . $_SESSION['userIdd'] . "'");
	$user_priveleges_rs = mysqli_fetch_array($getUserPrivilege);
	if ($user_priveleges_rs[0] == 'PS0001') {
		// If the user was inactive for the last 360 seconds (6 minutes) then it will logout directly
		if ((time() - $_SESSION['lastLoginTimeStamp']) < 360) {
			// Let the user access ands Re-initialize the Session with new time
			$_SESSION["lastLoginTimeStamp"] = time();
			
			// Save the current visited page URL
    		    $getCurrentUser = mysqli_query($conn, "SELECT userID FROM last_visited_page WHERE userID = '". $_SESSION['userIdd'] ."'") or die(mysqli_error($conn));
    		    if ($getCurrentUser) {
    		        if (mysqli_num_rows($getCurrentUser) > 0) {
    		            mysqli_query($conn, "UPDATE last_visited_page SET visitedPageURL = '". $fullURL ."', visitedDate = '". date("Y-m-d H:i:s") ."' WHERE userID = '". $_SESSION['userIdd'] ."'") or die(mysqli_error($conn));
    		        } else {
    		            mysqli_query($conn, "INSERT INTO last_visited_page( userID, visitedPageURL, visitedDate) VALUES ('". $_SESSION['userIdd'] ."', '". $fullURL ."', '". date("Y-m-d H:i:s") ."')") or die(mysqli_error($conn));
    		        }
    		    }

		} else {
			echo "<script>location ='logout'</script>";
		}
	} else {
		$checkMainMenu = mysqli_query($conn, "SELECT * FROM user_main_menus WHERE userID = '" . $_SESSION['userIdd'] . "' AND mainMenuID IN (SELECT mainMenuID FROM main_menus WHERE mainMenuURL = '" . $realURL . "')");
		if (mysqli_num_rows($checkMainMenu) > 0) {
		    // Save the current visited page URL
		    $getCurrentUser = mysqli_query($conn, "SELECT userID FROM last_visited_page WHERE userID = '". $_SESSION['userIdd'] ."'") or die(mysqli_error($conn));
		    if ($getCurrentUser) {
		        if (mysqli_num_rows($getCurrentUser) > 0) {
		            mysqli_query($conn, "UPDATE last_visited_page SET visitedPageURL = '". $fullURL ."', visitedDate = '". date("Y-m-d H:i:s") ."' WHERE userID = '". $_SESSION['userIdd'] ."'") or die(mysqli_error($conn));
		        } else {
		            mysqli_query($conn, "INSERT INTO last_visited_page( userID, visitedPageURL, visitedDate) VALUES ('". $_SESSION['userIdd'] ."', '". $fullURL ."', '". date("Y-m-d H:i:s") ."')") or die(mysqli_error($conn));
		        }
		    }
		} else {
			$checkSubMenu = mysqli_query($conn, "SELECT * FROM user_sub_menus WHERE userID = '" . $_SESSION['userIdd'] . "' AND subMenuID IN (SELECT subMenuID FROM sub_menus WHERE subMenuURL = '" . $realURL . "')");
			if (mysqli_num_rows($checkSubMenu) > 0) {
                // Save the current visited page URL
    		    $getCurrentUser = mysqli_query($conn, "SELECT userID FROM last_visited_page WHERE userID = '". $_SESSION['userIdd'] ."'") or die(mysqli_error($conn));
    		    if ($getCurrentUser) {
    		        if (mysqli_num_rows($getCurrentUser) > 0) {
    		            mysqli_query($conn, "UPDATE last_visited_page SET visitedPageURL = '". $fullURL ."', visitedDate = '". date("Y-m-d H:i:s") ."' WHERE userID = '". $_SESSION['userIdd'] ."'") or die(mysqli_error($conn));
    		        } else {
    		            mysqli_query($conn, "INSERT INTO last_visited_page( userID, visitedPageURL, visitedDate) VALUES ('". $_SESSION['userIdd'] ."', '". $fullURL ."', '". date("Y-m-d H:i:s") ."')") or die(mysqli_error($conn));
    		        }
    		    }
			} else {

				//List of external pages that is allowed for the users to access
				$allowed_extenral_web_pages_for_users = array("test","sample");

				if (in_array($realURL, $allowed_extenral_web_pages_for_users)) {
					
					// If the user was inactive for the last 360 seconds (6 minutes) then it will logout directly
					if ((time() - $_SESSION['lastLoginTimeStamp']) < 360) {
						// Let the user access ands Re-initialize the Session with new time
						$_SESSION["lastLoginTimeStamp"] = time();
						
						// Save the current visited page URL
            		    $getCurrentUser = mysqli_query($conn, "SELECT userID FROM last_visited_page WHERE userID = '". $_SESSION['userIdd'] ."'") or die(mysqli_error($conn));
            		    if ($getCurrentUser) {
            		        if (mysqli_num_rows($getCurrentUser) > 0) {
            		            mysqli_query($conn, "UPDATE last_visited_page SET visitedPageURL = '". $fullURL ."', visitedDate = '". date("Y-m-d H:i:s") ."' WHERE userID = '". $_SESSION['userIdd'] ."'") or die(mysqli_error($conn));
            		        } else {
            		            mysqli_query($conn, "INSERT INTO last_visited_page( userID, visitedPageURL, visitedDate) VALUES ('". $_SESSION['userIdd'] ."', '". $fullURL ."', '". date("Y-m-d H:i:s") ."')") or die(mysqli_error($conn));
            		        }
            		    }
					} else {
						echo "<script>location ='logout'</script>";
					}

				} else {
					echo "<script>location ='logout'</script>";
				}
			}
		}
	}
} else {
	echo "<script>location ='logout'</script>";
}
?>

<!-- Sidebar menu-->
<div class="app-sidebar__overlay" data-toggle="sidebar"></div>
<aside class="app-sidebar">
	<div class="app-sidebar__user">
		<img style="position: absolute; margin: 30px 0px 0px 30px; height: 20px; width: 20px;" src="img/online-u.png" />
		<img class="app-sidebar__user-avatar" src="<?php echo $_SESSION['userPhoto']; ?>" height="60px" width="60px">
		<div style="width: 100%">
			<p class="app-sidebar__user-name"><b><?php echo $_SESSION['userName']; ?></b></p>
			<p class="app-sidebar__user-designation" style="line-height: 1.15;"><?php echo $_SESSION['userType']; ?></p>
		</div>
	</div>
    <div class="row mb-2" style="margin: -5px;">
        <div class="col-md-12">
            <div class="input-group border border-white rounded-pill">
                <input class="form-control border pl-4 border-0 bg-transparent text-white" type="text" name="txtSearchMenuOrSubmenu" id="txtSearchMenuOrSubmenu" placeholder="Search..">
                <span class="input-group-append">
                    <div class="input-group-text bg-transparent border border-0 text-white"><i class="fa fa-search fa-sm"></i></div>
                </span>
            </div>
        </div>
    </div>
	<ul class="app-menu">
		<?php
    		if ($_SESSION['userTypeID'] == 'PS0001') {
    		    
    			$getSystemMenus = mysqli_query($conn, "SELECT * FROM main_menus ORDER BY mainMenuRank");
    			if ($getSystemMenus) {
    				if (mysqli_num_rows($getSystemMenus) > 0) {
    					while ($system_menus_rs = mysqli_fetch_array($getSystemMenus)) {
    						$getSubmenus = mysqli_query($conn, "SELECT * FROM sub_menus WHERE mainMenuID = '" . $system_menus_rs[0] . "' ORDER BY subMenuRank");
    						if ($getSubmenus) {
    							if (mysqli_num_rows($getSubmenus) > 0) {
        						    ?><li class="treeview"><a class="app-menu__item" href="#" data-toggle="treeview"><i class="app-menu__icon <?php echo $system_menus_rs[2]; ?>"></i><span class="app-menu__label"><?php echo $system_menus_rs[1]; ?></span><i class="treeview-indicator fa fa-angle-right"></i></a>
    									<ul class="treeview-menu">
    									    <?php
        										while ($submenu_rs = mysqli_fetch_array($getSubmenus)) {
        											if ($submenu_rs[1] == "Change Password") {
        										        ?><li><a class="treeview-item" href="<?php echo $submenu_rs[3]; ?>" data-id="<?php echo $_SESSION['userName']; ?>" data-toggle="modal" data-target="#changePasswordModal"><i class="icon <?php echo $submenu_rs[2]; ?>"></i> <?php echo $submenu_rs[1]; ?></a></li><?php
        										    } else if ($submenu_rs[1] == "Security Options") {
        											    ?><li><a class="treeview-item" href="<?php echo $submenu_rs[3]; ?>" id="securitySubMenu" data-id="<?php echo $_SESSION['userIdd']; ?>" data-toggle="modal" data-target="#securitySettingsModal"><i class="icon <?php echo $submenu_rs[2]; ?>"></i> <?php echo $submenu_rs[1]; ?></a></li><?php
        											} else {
        												?><li><a class="treeview-item" href="<?php echo $submenu_rs[3]; ?>"><i class="icon <?php echo $submenu_rs[2]; ?>"></i> <?php echo $submenu_rs[1]; ?></a></li><?php
        											}
        										}
        									?>
    									</ul>
    							    </li><?php
        					    } else {
        						    if ($system_menus_rs[1] == "Logout") {
        							    ?><li><a class="app-menu__item" href="<?php echo $system_menus_rs[3]; ?>" id="logoutOutSide" data-id="<?php echo $_SESSION['userName']; ?>"><i class="app-menu__icon <?php echo $system_menus_rs[2]; ?>"></i><span class="app-menu__label">Logout</a></li><?php
                                    } else {
                                        ?><li><a class="app-menu__item no_child_menu" href="<?php echo $system_menus_rs[3]; ?>"><i class="app-menu__icon <?php echo $system_menus_rs[2]; ?>"></i><span class="app-menu__label"><?php echo $system_menus_rs[1]; ?></span></a></li><?php
                                    }
                                }
                            } else {
                                echo mysqli_error($conn);
                            }
                        }
                    } else {
                        echo "<center><h6 class='text-light text-center'>Menus Not Found</h6></center>";
                    }
                } else {
                    echo mysqli_error($conn);
                }
            
            } else {

                $getSystemMenus = mysqli_query($conn, "SELECT DISTINCT main_menus.mainMenuID, main_menus.mainMenuText, main_menus.mainMenuIcon, main_menus.mainMenuURL, user_main_menus.userID FROM user_main_menus JOIN main_menus ON (user_main_menus.mainMenuID = main_menus.mainMenuID AND user_main_menus.userID = '" . $_SESSION['userIdd'] . "') ORDER BY mainMenuRank");
                if ($getSystemMenus) {
                    if (mysqli_num_rows($getSystemMenus) > 0) {
                        while ($system_menus_rs = mysqli_fetch_array($getSystemMenus)) {
                            $getSubmenus = mysqli_query($conn, "SELECT DISTINCT sub_menus.subMenuID, sub_menus.subMenuText, sub_menus.subMenuIcon, sub_menus.subMenuURL, sub_menus.mainMenuID, user_sub_menus.userID FROM sub_menus JOIN user_sub_menus ON (user_sub_menus.subMenuID = sub_menus.subMenuID AND user_sub_menus.userID = '" . $_SESSION['userIdd'] . "' AND sub_menus.mainMenuID = '" . $system_menus_rs[0] . "') ORDER BY subMenuRank");
                            if ($getSubmenus) {
                                if (mysqli_num_rows($getSubmenus) > 0) {
                                    ?><li class="treeview"><a class="app-menu__item" href="#" data-toggle="treeview"><i class="app-menu__icon <?php echo $system_menus_rs[2]; ?>"></i><span class="app-menu__label"><?php echo $system_menus_rs[1]; ?></span><i class="treeview-indicator fa fa-angle-right"></i></a>
                                        <ul class="treeview-menu">
                                            <?php
                                                while ($submenu_rs = mysqli_fetch_array($getSubmenus)) {
                                            	    if ($submenu_rs[1] == "Change Password") {
                                                        ?><li><a class="treeview-item" href="<?php echo $submenu_rs[3]; ?>" data-id="<?php echo $_SESSION['userName']; ?>" data-toggle="modal" data-target="#changePasswordModal"><i class="icon <?php echo $submenu_rs[2]; ?>"></i> <?php echo $submenu_rs[1]; ?></a></li><?php
                							        } else if ($submenu_rs[1] == "Security Options") {
                                                        ?><li><a class="treeview-item" href="<?php echo $submenu_rs[3]; ?>" id="securitySubMenu" data-id="<?php echo $_SESSION['userIdd']; ?>" data-toggle="modal" data-target="#securitySettingsModal"><i class="icon <?php echo $submenu_rs[2]; ?>"></i> <?php echo $submenu_rs[1]; ?></a></li><?php
                                                    } else {
                                                        ?><li><a class="treeview-item" href="<?php echo $submenu_rs[3]; ?>"><i class="icon <?php echo $submenu_rs[2]; ?>"></i> <?php echo $submenu_rs[1]; ?></a></li><?php
                                                    }
                                                }
                                            ?>
                                        </ul>
                                    </li><?php
                                } else {
                                    if ($system_menus_rs[1] == "Logout") {
                                        ?><li><a class="app-menu__item" href="<?php echo $system_menus_rs[3]; ?>" id="logoutOutSide" data-id="<?php echo $_SESSION['userName']; ?>"><i class="app-menu__icon <?php echo $system_menus_rs[2]; ?>"></i><span class="app-menu__label">Logout</a></li><?php
                                    } else {
                                        ?><li><a class="app-menu__item no_child_menu" href="<?php echo $system_menus_rs[3]; ?>"><i class="app-menu__icon <?php echo $system_menus_rs[2]; ?>"></i><span class="app-menu__label"><?php echo $system_menus_rs[1]; ?></span></a></li><?php
                                    }
                                }
                            } else {
                                echo mysqli_error($conn);
                            }
                        }
                    } else {
                        echo "<center><h6 class='text-light text-center'>Menus Not Found</h6></center>";
                    }
                } else {
                    echo mysqli_error($conn);
                }
            }
        ?>
        
        
    </ul>


</aside>