<?php
   include("config.php");
   session_start();
   
   if(isset($_SESSION['login_user'])){
       $my_email = $_SESSION['login_user'];
       $sql5 = "SELECT * FROM members WHERE `Email` = '$my_email'"; 
       $result5 = mysqli_query($db,$sql5);
	   while($row5 = $result5->fetch_array()){
		    $my_type = $row5['Usertype'];
	   }
	   if($my_type == "admin"){
	       	header("location: admin.php");
	   }
	   else if($my_type == "member"){
	       header("location: Client/home.php");
	   }
	   else if($my_type == "Super User (SU)"){
	       header("location: super_user.php");
	   }
   }
	
	//fetch campus from database
	$sql_campus = mysqli_query($db, "SELECT * FROM campus WHERE Campus!='Super User (SU)'");
	
	$selected_camp = "";
	if(isset($_POST["selected_camp"])){
	    $selected_camp = mysqli_real_escape_string($db,$_POST['selected_camp']);
	}
	
	//fetch usertype from database
	$sql_usertype = mysqli_query($db, "SELECT `Department/Office` FROM usertype WHERE Campus='$selected_camp' AND `Usertype`!='admin'");
   
   if(isset($_POST["office"])) {
      // username and password sent from form 
      
      $my_email = mysqli_real_escape_string($db,$_POST['email']);
      $my_password = mysqli_real_escape_string($db,$_POST['pass']); 
	  $my_office = mysqli_real_escape_string($db,$_POST['office']); 
	  $my_campus = mysqli_real_escape_string($db,$_POST['campus']); 
	  $selected_camp = $my_campus;
      
	  $decrypted_pass = md5($my_password);
	  $sql = "SELECT * FROM members WHERE `Email` = '$my_email' AND `Password` = '$decrypted_pass' AND `Department/Office` = '$my_office' AND `Campus` = '$my_campus'"; 

	  $result = mysqli_query($db,$sql);
	  while($row = $result->fetch_array()){
		  $type = $row['Usertype'];
	  }
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];
      
      $count = mysqli_num_rows($result);
      
      // If result matched $myusername and $mypassword, table row must be 1 row
		
      if($count == 1) {
		  if($type == "admin"){
			$_SESSION['login_user'] = $my_email;
			$_SESSION['campus'] = $my_campus;
			$_SESSION['office'] = $my_office;
			header("location: admin.php");
		  }
		  else if($type == "Super User (SU)"){
			$_SESSION['login_user'] = $my_email;
			$_SESSION['campus'] = $my_campus;
			$_SESSION['office'] = $my_office;
			header("location: super_user.php");
		  }
		  else if($type == "member"){
			$_SESSION['login_user'] = $my_email;
			$_SESSION['campus'] = $my_campus;
			$_SESSION['office'] = $my_office;
			header("location: Client/home.php");
		  }
		}else {
         $error = "Invalid! You have entered incorrect data";
      }
   }
   else
   {
	   $error = "";
   }
   
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<title>Login</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->	
	<link rel="icon" type="image/png" href="images/icons/minsu-icon.ico"/>
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="fonts/iconic/css/material-design-iconic-font.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/animsition/css/animsition.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="vendor/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="util.css">
	<link rel="stylesheet" type="text/css" href="main.css">
<!--===============================================================================================-->
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css" />
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
	
	<div class="limiter">
		<div class="container-login100" style="background: linear-gradient( rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5) ), url('images/minsu1.jpg'); background-size: cover;">
			<div class="wrap-login100 p-l-55 p-r-55 p-t-65 p-b-54">

				<form class="login100-form validate-form" action="" method="post">
				<img src="images/ccs_cover.png" alt="Minsu-Logo" width="510px" height="200px" style="margin-top: -65px; margin-left: -57px; min-width: 540px;">
				<br><br>
				
				<center><h3 style="font-weight: bold;">FACULTY</h3></center>
		        <br>
					<div class="wrap-input100 validate-input m-b-23" data-validate = "Email is required">
						<span class="label-input100">Email</span>
						<input class="input100" type="email" name="email" maxlength = "50" placeholder="Type your email">
						<span class="focus-input100" data-symbol="&#xf206;"></span>
					</div>

					<div class="wrap-input100 validate-input" data-validate="Password is required">
						<span class="label-input100">Password</span>
						<input class="input100" type="password" name="pass" id="password" maxlength = "50" placeholder="Type your password">
						<i class="bi bi-eye-slash" id="togglePassword" style="float: right; margin-top: -41px; margin-right: 18px;"></i>
						<span class="focus-input100" data-symbol="&#xf190;"></span>
					</div>

					<br>
					    <!--<form method='post' action='' id='myform'>-->
						<select name="camp1" class="field input" style="width: 392px; border-color: white; border-radius: 5px; background-color: white; border: 2px solid #e8e8e8; height: 40px; font-size: 17px; margin-top: 5px; padding:5px;" disabled>
							<option value="">Select Campus :</option>
								<!-- Fetch the list of usertype in usertype table from database -->
								<?php
									$i=0;
									while($DB_ROW1 = mysqli_fetch_array($sql_campus)) {
									if($DB_ROW1["Campus"] == $selected_camp){
									?>
									    <option selected><?php echo $DB_ROW1["Campus"]; ?></option>
									<?php
									}
									?>
									<option><?php echo $DB_ROW1["Campus"]; ?></option>
									<?php
									$i++;
									}
									?>
						</select>
						<input name="campus" value="<?php echo $selected_camp; ?>" type="hidden">
						<!--</form>-->
						<br>
						
						<select name="office" class="field input" style="width: 392px; border-color: white; border-radius: 5px; background-color: white; border: 2px solid #e8e8e8; height: 40px; font-size: 17px; margin-top: 20px; padding:5px;" required>
							<option value="">Select Office/Department :</option>
								<!-- Fetch the list of usertype in usertype table from database -->
								<?php
									$i=0;
									while($DB_ROW = mysqli_fetch_array($sql_usertype)) {
									?>
									<option>
									<?php echo $DB_ROW["Department/Office"]; ?></option>
									<?php
									$i++;
									}
									?>
						</select>
						

					<div class="container-login100-form-btn m-t-20" style="color:red;">
						<?php echo $error?>
					</div>
					
					<div class="container-login100-form-btn">
						<div class="wrap-login100-form-btn">
							<div class="login100-form-bgbtn"></div>
							<button class="login100-form-btn" style="background-color: #0077a3;">
								Login
							</button>
						</div>
					</div>
					<br>
					<div class="text-center p-t-8 p-b-31">
						<a href="login-admin.php">
							LOGIN AS ADMIN
						</a>
					</div>
				</form>
			</div>
		</div>
	</div>
	

	<div id="dropDownSelect1"></div>
	
<!--===============================================================================================-->
	<script src="vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/bootstrap/js/popper.js"></script>
	<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/daterangepicker/moment.min.js"></script>
	<script src="vendor/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
	<script src="vendor/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
	<script src="js/main.js"></script>

	<script>
        const togglePassword = document.querySelector("#togglePassword");
        const password = document.querySelector("#password");

        togglePassword.addEventListener("click", function () {
            // toggle the type attribute
            const type = password.getAttribute("type") === "password" ? "text" : "password";
            password.setAttribute("type", type);
            
            // toggle the icon
            this.classList.toggle("bi-eye");
        });
    </script>

</body>
</html>