<?php
session_start(); // starts a session

// destroy the session if there is one present
if (isset($_SESSION)) 
{
    session_unset();
    session_destroy();		
}

//clear any previous error messages
$errormsg = "";

// process the login
if (isset($_POST['login'])) {
    // get the account details
    $Username = $_POST['Username'];
    $UserPassword = $_POST['UserPassword'];

    // attempt to process the login
    $con = mysqli_connect("localhost", "root", "", "PannotinaCoDB");
    if ($con->connect_error > 0) {
        die('Could not connect: ' . $con->connect_error);
        exit();
    }
    
    // use prepared statements. anti-sql injection
    $sqlcmd = "SELECT CAST(LoginUserID as CHAR) as LoginUserID, LoginName, AccessLevel 
                      from LoginUsers 
                      WHERE Username = ? and UserPassword = ?";    
    $sql = $con->prepare($sqlcmd);
    $sql->bind_param("ss", $Username, $UserPassword);
    $sql->execute();        
    $sql->bind_result($LoginUserID, $LoginName, $AccessLevel);
    $sql->store_result();
    $sql->fetch();
    
    // check if we have only 1 record 
    if ($sql->affected_rows == 0)
        $errormsg = "<b style='color:Red';>Username/Password incorrect</b>";
    else {
        // check if we have > 1 record 
        if ($sql->affected_rows > 1)
            $errormsg = "<b>Error: 0x123fe1abc has occurred. Contact Administrator</b>";
        else {
            // if we get here its a good login
            // check the access level 0 - is the user, 2 account is locked
            if ($AccessLevel == 2)
                $errormsg = "<b>Unable to login - Account is locked</b>";
            else {
                // keep track of key data - it is used throughout the webapp
                $_SESSION['LoginName'] = $LoginName;
                $_SESSION['AccessLevel'] = $AccessLevel;
                $_SESSION['LoggedIn'] = 1;
                
                // we have a login update the LoginUsers table to when they logged in
                $sqlcmd = "UPDATE LoginUsers set LastLoggedIn = NOW() WHERE LoginUserID = ?";
                $sql = $con->prepare($sqlcmd);
                $sql->bind_param("i", $LoginUserID);
                $sql->execute();
                // redirect to the dashboard
                header('Location: http://localhost/PannotinaCoDB/dashboard/index.php');
                exit();
            }
        }
    }
}
?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Sign In to Pannotina Co Database</title>


        <!-- Bootstrap core CSS -->
        <link href="../styles/bootstrap.min.css" rel="stylesheet">



        <style>
            .bd-placeholder-img {
                font-size: 1.125rem;
                text-anchor: middle;
                -webkit-user-select: none;
                -moz-user-select: none;
                -ms-user-select: none;
                user-select: none;
            }

            @media (min-width: 768px) {
                .bd-placeholder-img-lg {
                    font-size: 3.5rem;
                }
            }
        </style>

        <!-- Custom styles for this template -->
        <link href="login.css" rel="stylesheet">
    </head>
    <body class="text-center">		
        <form class="form-signin" method="POST">
            <img class="mb-4" src="../assets/logo/Logo.png" alt="" width="200" height="200" style="border-radius: 50%;">
            <h1 class="h3 mb-3 font-weight-normal" style="font-size: 23pt;">Please Sign In</h1>
            <input type="text" id="Username" style="border-color: #325838;" name="Username" class="form-control" placeholder="Username" required autofocus>
            <input type="password" id="UserPassword" style="border-color: #325838;" name="UserPassword" class="form-control" placeholder="Password" required>
            <div id="errormessage"> <?php echo $errormsg ?> </div>
            <button class="btn btn-lg btn-primary btn-block" style="background-color: #325838; border-color: #50732C;" type="submit" name="login" value = "login" >Sign in</button>
            <p class="mt-5 mb-3 text-muted">Pannotina Co &copy; 2025</p>
        </form>
    </body>
</html>
