<?php
session_start();
include_once('controller/config.php');
if(isset($_POST["do"])&&($_POST["do"]=="user_login")){

	$email=$_POST["email"];
	$password=$_POST["password"]; 
	
	$sql="SELECT * FROM user WHERE email='$email'";	
	$result=mysqli_query($conn,$sql);
	$row=mysqli_fetch_assoc($result);
	
	$email1=$row['email'];
	$password1=$row['password'];
	$type=$row['type'];
	
	//******************************** Etudiants **************************************
	
    $sql2="SELECT * FROM etudiants WHERE nins=$email";	
	$result2=mysqli_query($conn,$sql2);
	$row2=mysqli_fetch_assoc($result2);
	//****************************
	$email22=$row2['nins'];
	$email2 = trim($email22);
    //**************************
	$password2=$row2['pw'];
	$type1="Student";
	//**********************************************************************************
	
	$msg=0;
	
	if (($email==$email1 && $password==$password1) or ($email==$email2 && $password==$password2)) {	
		if($type1 == "Student"){
			$msg = "Etudiants";
			
			$sql1="SELECT * FROM etudiants where nins=$email";	
			$result1=mysqli_query($conn,$sql1);
			$row1=mysqli_fetch_assoc($result1);
	
			$index_number=$row1['nins'];
			$_SESSION["index_number"]=$index_number;
			$_SESSION["type"]="Student";
			header("Location: view/dashboard1.php");
		}
		
		if($type == "Teacher"){
			$msg ;//
			
			$sql1="SELECT * FROM teacher where email='$email'";	
			$result1=mysqli_query($conn,$sql1);
			$row1=mysqli_fetch_assoc($result1);
	
			$index_number=$row1['index_number'];
			$_SESSION["index_number"]=$index_number;
			$_SESSION["type"]="Teacher";
			header("Location: view/dashboard2.php");
		}
		
		if($type == "Admin"){
			
			$sql1="SELECT * FROM admin where email='$email'";	
			$result1=mysqli_query($conn,$sql1);
			$row1=mysqli_fetch_assoc($result1);
	
			$index_number=$row1['index_number'];
			$_SESSION["index_number"]=$index_number;
			$_SESSION["type"]="Admin";
			header("Location: view/dashboard.php");
		}
		
		if($type == "TEC"){
			
			$sql1="SELECT * FROM teacher WHERE email='$email'";	
			$result1=mysqli_query($conn,$sql1);
			$row1=mysqli_fetch_assoc($result1);
	
			$index_number=$row1['index_number'];
			$_SESSION["index_number"]=$index_number;
			$_SESSION["type"]="TEC";
			header("Location: view/dashboard3.php");
		}
		if($type == "ch-dpt"){
			
			$sql1="SELECT * FROM teacher WHERE email='$email'";	
			$result1=mysqli_query($conn,$sql1);
			$row1=mysqli_fetch_assoc($result1);
			$index_number=$row1['index_number'];
			$_SESSION["index_number"]=$index_number;
			$_SESSION["type"]="ch-dpt";
			header("Location: view/dashboard_dpt.php");
		}
		if($type == "med"){
			
			$sql1="SELECT * FROM teacher WHERE email='$email'";	
			$result1=mysqli_query($conn,$sql1);
			$row1=mysqli_fetch_assoc($result1);
			$index_number=$row1['index_number'];
			$_SESSION["index_number"]=$index_number;
			$_SESSION["type"]="med";
			header("Location: clinic/dashboard_med.php");
		}
	}else{
		$msg+=1;//Email or Password is incorrect
		header("Location: view/login.php?do=login_error&msg=$msg");
		
	}
	
	echo $_SESSION["index_number"];
	echo $_SESSION["type"];

//header("Location:view/attendents.php?do=alert_from_insert&msg=$msg&msg1=$monthly_fee&student_late=$student_late&teacher_late=$teacher_late&intime=$intime&outtime=$outtime&alert=$alert");//MSK-000143-5

}
?>

