<?php

// Initialises variables
$username = $password = '';

// Checks if username is empty
if (empty($_POST['username'])) {
    $value = ['username' => 'Please complete all fields'];
    Session::set('errors', $value);
    header('Location: ../public/screens/login.php');
    exit;
}
$username = $_POST['username'];


// Check is password is empty
if (empty($_POST['password'])) {
    $value = ['password' => 'Please complete all fields'];
    Session::set('errors', $value);
    header('Location: ../public/screens/login.php');
    exit;
}
$password = $_POST['password'];

// Creates user Object
$user = new User($username, $password);

// Checks for valid username and password
if (!$user->isUsernameValid() || !$user->isPasswordValid()) {
    $value = ['username' => 'Invalid Credentials'];
    Session::set('errors', $value);
    header('Location: ../public/screens/login.php');
    exit;
}

// Saves user array on session
$user = [
    'username' => $user->getUsename(),
    'userLevelId' => $user->getUserLevelId(),
    'userLevel' => $user->getUserLevel()
];
Session::set('user', $user);

header('Location: ../public/screens/home.php');
exit;