<?php
session_start(); // don't forget - if you are using sessions in your web app - you need session_start(), otherwise you will lose session data

// we are at the login for the app 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['signin'])) {
    // get the account details
    $username = $_POST['username'];
    $password = $_POST['password'];

    // attempt to process the login
    $con = mysqli_connect("localhost", "root", "", "MadisonsMusicDatabaseOfficial");
    if ($con->connect_error > 0) {
        die('Could not connect: ' . $con->connect_error);
        exit(); // you should redirect to some form of error page !!!!
    }
    
    // or by using prepared statements
    $sqlcmd = "SELECT CAST(userid as CHAR) as userid, loginname, accesslevel 
                      from users 
                      WHERE username = ? and password = ?";    
    $sql = $con->prepare($sqlcmd);
    $sql->bind_param("ss", $username, $password);
    $sql->execute();        
    $sql->bind_result($userid, $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 users table to when they logged in
                $sqlcmd = "UPDATE users set lastloggedin = NOW() WHERE userid = ?";
                $sql = $con->prepare($sqlcmd);
                $sql->bind_param("i", $userid);
                $sql->execute();
                // redirect to the dashboard
                header('Location: http://localhost/MusicDatabaseWebApp/dashboard/index.php');
                exit();
            }
        }
    }
}
?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Signin To Rentals V1.0</title>


        <!-- Bootstrap core CSS -->
        <link href="../assets/dist/css/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="signin.css" rel="stylesheet">
    </head>
    <body class="text-center">		
        <form class="form-signin" method="POST">
            <img class="mb-4" src="../assets/brand/bootstrap-solid.svg" alt="" width="72" height="72">
            <h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
            <input type="text" id="username" name="username" class="form-control" placeholder="Username" required autofocus>
            <input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
            <div id="errormessage"> <?php echo $errormsg ?> </div>
            <button class="btn btn-lg btn-primary btn-block" type="submit" name="signin" value = "signin" >Sign in</button>
            <p class="mt-5 mb-3 text-muted">&copy; PJ Computing 2021</p>
        </form>
    </body>
</html>
