<?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
    $LoginUsername = $_POST['LoginUsername'];
    $LoginPassword = $_POST['LoginPassword'];

    // attempt to process the login
    $con = mysqli_connect("localhost", "root", "", "GCSOwls");
    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(LoginID as CHAR) as LoginID, LoginUsername
                      from loginAccounts 
                      WHERE LoginUsername = ? and LoginPassword = ?";    
    $sql = $con->prepare($sqlcmd);
    $sql->bind_param("ss", $LoginUsername, $LoginPassword);
    $sql->execute();        
    $sql->bind_result($LoginID, $LoginUsername);
    $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 {
                // redirect to the dashboard
                header('Location: http://localhost/databaseAssignment/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="LoginUsername" name="LoginUsername" class="form-control" placeholder="Username" required autofocus>
            <input type="password" id="LoginPassword" name="LoginPassword" 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; Rhys Hughes 2023</p>
        </form>
    </body>
</html>
