<?php
session_start();
//configuration file
require_once('../constants/config.php');

$email_address = $_POST['email'];
$login = $_POST['password'];

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	
$stmt = $conn->prepare("SELECT email,login,role,avator FROM tbl_admin WHERE email = :email UNION SELECT email,login,role,avator FROM tbl_students WHERE email = :email");
$stmt->bindParam(':email', $email_address);
$stmt->execute();
$result = $stmt->fetchAll();

//getting number of records found
$rec = count($result);

if ($rec > 0) {

foreach($result as $row) {

$role = $row['role'];
$avator = $row['avator'];		

}

switch ($role) {
	case 'admin':
		# verifying password
	if (password_verify($login, $row['login'])) {

	admin_login();

	}else{

	$_SESSION['reply'] = "001";
    header("location:../../");

	}
		break;
	
	case 'student':

	if (password_verify($login, $row['login'])) {

	student_login();

	}else{

	$_SESSION['reply'] = "001";
    header("location:../../");

	}
		break;
}

}else{

$_SESSION['reply'] = "001";
header("location:../../");

}

					  
}catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}

function admin_login() {

$_SESSION['logged'] = "1";
$_SESSION['role'] = "admin";
$_SESSION['email'] = $GLOBALS['email_address'];
$_SESSION['avator'] = $GLOBALS['avator'];
header("location:../../admin");

}

function student_login() {
require('../constants/config.php');
$_SESSION['logged'] = "1";
$_SESSION['role'] = "student";
$_SESSION['email'] = $GLOBALS['email_address'];
$_SESSION['avator'] = $GLOBALS['avator'];


try {
	
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	
	$stmt = $conn->prepare("SELECT * FROM tbl_students WHERE email = :email");
	$stmt->bindParam(':email', $GLOBALS['email_address']);
	$stmt->execute();
	$result = $stmt->fetchAll();

    foreach($result as $row)
    {
    	$_SESSION['id'] = $row['id'];
    	$_SESSION['fname'] = $row['first_name'];
    	$_SESSION['lname'] = $row['last_name'];
    	$_SESSION['gender'] = $row['gender'];
    	$_SESSION['dep'] = $row['department'];
    	$_SESSION['class'] = $row['class'];
		
	}

	header("location:../../student");
					  
	}catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }


}
?>