<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

include('../config/config.php');

//collect the input details
$username = trim($_POST['email']);
$password = md5(trim($_POST['password']));
$campus = trim($_POST['campus']);
$institution = trim($_POST['institution']);


$sql = ("SELECT * FROM `userlogin` WHERE `UserRegNumberOrUsername`='$username' AND UserPassword='$password' AND InstitutionIDOrCampusID IN ('$institution', '$campus')");
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_assoc($result);
$row_cnt = mysqli_num_rows($result);

if ($row_cnt > 0) {

    $usertype = $row['UserType'];

    if ($usertype == 'teacher') {

        $UserID = $row['UserID'];

        session_start();
        $_SESSION['spgID'] = $UserID;
        $_SESSION['spgUserType'] = $usertype;
        echo $usertype = $row['UserType'];


    } elseif ($usertype == 'student') {
        $UserID = $row['UserID'];

        session_start();
        $_SESSION['spgID'] = $UserID;
        $_SESSION['spgUserType'] = $usertype;
        echo $usertype = $row['UserType'];

    } elseif ($usertype == 'schoolhead') {

        $UserID = $row['UserID'];

        session_start();
        $_SESSION['spgID'] = $UserID;
        $_SESSION['spgUserType'] = $usertype;
        echo $usertype = $row['UserType'];

    } elseif ($usertype == 'parent') {

        $UserID = $row['UserID'];

        session_start();
        $_SESSION['spgID'] = $UserID;
        $_SESSION['spgUserType'] = $usertype;

        echo $usertype = $row['UserType'];

    } elseif ($usertype == 'admin') {

        $UserID = $row['UserID'];

        session_start();
        $_SESSION['spgID'] = $UserID;
        $_SESSION['spgUserType'] = $usertype;

        echo $usertype = $row['UserType'];


    } elseif ($usertype == 'applicant') {


        $UserID = $row['UserID'];

        session_start();
        $_SESSION['spgID'] = $UserID;
        $_SESSION['spgUserType'] = $usertype;

        echo $usertype = $row['UserType'];

    } else {
        echo 'Invalid Login Details!';
    }
} else {
    echo 'Invalid Login Details!';
}


?>