<?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 == "user"){
		header("location: Client/home.php");
	   }
	   else if($my_type == "admin"){
	       header("location: admin.php");
	   }
   }
   
   if($_SERVER["REQUEST_METHOD"] == "POST") {
	$my_email = mysqli_real_escape_string($db,$_POST['email']);
	$my_password = mysqli_real_escape_string($db,$_POST['pass']); 
	$my_usertype = "";

	$sql9 = "SELECT * FROM members WHERE `Email` = '$my_email'"; 
	$result9 = mysqli_query($db,$sql9);
	while($row9 = $result9->fetch_array()){
		$my_usertype = $row9['Usertype'];
		$pro_pic = $row9['Profile_Picture'];
	}
	  
	  if($my_usertype == "admin"){
	      $selected_usertype = "admin";
	  }
      
	  $decrypted_pass = md5($my_password);
	  $sql = "SELECT * FROM members WHERE `Email` = '$my_email' AND `Password` = '$decrypted_pass' AND `Usertype` = '$my_usertype'"; 

	  $result = mysqli_query($db,$sql);
	  while($row = $result->fetch_array()){
		  $type = $row['Usertype'];
		  $verified = $row['Verified'];
		  $_SESSION['usertype'] = $type;
	  }
      $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 == "user"){
			$_SESSION['login_user'] = $my_email;
			$_SESSION['pro_pic'] = $pro_pic;
			header("location: Client/home.php");
		  }
		  else if($type == "admin"){
			$_SESSION['login_user'] = $my_email;
			$_SESSION['pro_pic'] = $pro_pic;
			header("location: admin.php");
		  }
		}else {
			echo '<script>alert("Invalid username or password! You have entered incorrect data")</script>';
      }
   }
   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/logo_2.png"/>
<!--===============================================================================================-->
	<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/blue_bg.jfif'); 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/header_1.png" alt="AICS-Logo" width="100%" height="150px" style="background-color: #bf7a0d; margin-top: -65px; margin-left: -57px; min-width: 502px; border-color: green;">
				<br><br>
		
		        <center><h3 style="font-weight: bold;">SIGN-IN</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>
				
						<br>
					
					<div class="container-login100-form-btn m-t-20" style="color:red;">
						
					</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: #242e42;">
								Login
							</button>
						</div>
					</div>
					<!-- <div class="text-center p-t-8 p-b-31">
						<a href="">
							LOGIN AS USER
						</a>
					</div> -->
					<br>
					<center><b><a href="Forgot-Password/forgot-password.php">Forgot Password</a><b></center>
				</form>
				<center>
				<p style="font-size: 16px; font-weight: ;">or</p>
				<a href="user-registration.php" style="background-color: ; width: 100%; padding: 12px; border-radius: 25px; color: ; font-size: 16px;">SIGN-UP</a></center>
			</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>