<?php
session_start();
require_once("public/connection.php");
require_once("public/functions.php");
require_once("public/school_config.php");
require_once("public/alert.php");
?>
<?php
	//////////////// DEFAULT NULL VALUES ///////////////
	$error = array();
	$date = date('d/M/Y');
	$select = "--select--";
	$year = date('Y');
?>
<?php
// Start session once at the top of the script
session_start();
    // Query for Users to login
    if(isset($_POST['login_btn'])){
        $login_id = inject_checker($connection, $_POST['login_id']);
        $login_password = inject_checker($connection, $_POST['login_password']);
        
        if(empty($login_id)){
            $msg = "Username or Registration Number is required";
            $alert_type = "warning";
        }
        elseif(empty($login_password)){
            $msg = "Password is required";
            $alert_type = "error";
        } else {
            // Check Admin credentials
            $query = "SELECT * FROM `users` WHERE `email` = '{$login_id}' AND `password` = '{$login_password}'";
            $run_query = mysqli_query($connection, $query);

            if(mysqli_num_rows($run_query) == 1){
                session_start();
                $result = mysqli_fetch_assoc($run_query);
                $_SESSION['admin'] = $result['id'];
                header("Location:resources/view/modules.php");
                exit();
            }

            // Check Student credentials
            $query = "SELECT * FROM `students` WHERE `reg_number` = '{$login_id}' AND `gen_password` = '{$login_password}'";
            $run_query = mysqli_query($connection, $query);

            if(mysqli_num_rows($run_query) == 1){
                session_start();
                $result = mysqli_fetch_assoc($run_query);
                $_SESSION['student'] = $result['id'];
                header("Location:resources/view/student_dashboard.php");
                exit();
            }
			
		// Check Accountant Credentials
			$query = " SELECT * FROM `accountants` WHERE `username` = '{$login_id}' AND `password` = '{$login_password}' ";
			$run_query = mysqli_query($connection, $query);
			
			if(mysqli_num_rows($run_query) == 1){
				session_start();
				$result = mysqli_fetch_assoc($run_query);
				$_SESSION['accountant'] = $result['id'];				
				header("Location:resources/view/accountant_dashboard.php");
				exit();
			}

            // Check Principal credentials
            $query = "SELECT * FROM `principal` WHERE `username` = '{$login_id}' AND `password` = '{$login_password}'";
            $run_query = mysqli_query($connection, $query);

            if(mysqli_num_rows($run_query) == 1){
                session_start();
                $result = mysqli_fetch_assoc($run_query);
                $_SESSION['principal'] = $result['id'];
                header("Location:resources/view/principal_dashboard.php");
                exit();
            }

           // Check Teacher credentials
			$query = "SELECT * FROM `teachers` WHERE `username` = '{$login_id}' AND `password` = '{$login_password}'";
			$run_query = mysqli_query($connection, $query);

			if (mysqli_num_rows($run_query) == 1) {
				$result = mysqli_fetch_assoc($run_query);
				$status = $result['status'];

				if ($status === 'Active') {
					// Start session and redirect if status is 'Active'
					session_start();
					$_SESSION['teacher'] = $result['id'];
					header("Location:resources/view/teacher_dashboard.php");
					exit();
				} elseif ($status === 'Suspend') {
					// Show error message if status is 'Suspend'
					$msg = "Your account is suspended!";					
				} elseif ($status === 'On Leave') {
					// Show error message if status is 'Suspend'
					$msg = "You are currently on a leave!";				
				}
			} else {
				// If no match found
				$msg = "Login failed! Incorrect login details.";
				$alert_type = "error";
			}

        }
    }
?>

