<?php
//clear any previous error messages
$errormsg = "";

// process the login
if (isset($_POST['signin'])) {
    // get the account details from database
    $username = $_POST['username'];
    $password = $_POST['password'];

    // attempt to process the login
    $con = mysqli_connect("localhost", "root", "", "gamerental");
    if ($con->connect_error > 0) {
        die('Could not connect: ' . $con->connect_error);
        exit(); // directs to error page if something is wrong
    }
    
    // generate the login onto the page
    $sqlcmd = "SELECT CAST(userid as CHAR) as userid, loginname, accesslevel 
                      from users
                      WHERE username = '" . $username . "' and password = '" . $password . "'";
    $sql = $con->prepare($sqlcmd);
    $sql->execute();        
    $sql->bind_result($userid, $loginname, $accesslevel);
    $sql->store_result();
    $sql->fetch();

    // Checks if password/username is correct, if not error message
    if ($sql->affected_rows == 0)
        $errormsg = "<b style='color:Red';>Username/Password incorrect please try again</b>";
    else {
        if ($accesslevel == 1)
            $errormsg = "<b style='color:Red';>Account is Locked, please try again</b>";
        else {
            // if we get here its a good login
            // check the access level 2 it will direct the user to the other page
            if ($accesslevel == 2)
                header('Location: http://localhost/gamerental/dashboard/index1.php');
                
            else {
                // if ait isnt 1 then it will direct them to the main page
                header('Location: http://localhost/gamerental/dashboard/index.php');
                exit();
            }
			

			
        }

            
			
    }
	
}	
	
	 

?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Sign in for Game Rental</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; Michelle Data Driven Website </p>
        </form>
    </body>
</html>
