<?php
session_start();
require_once '../include/Constants.php';
require_once '../include/DatabaseConn.php';
require_once '../models/Admin.php';


if (isset($_POST['username'])) {
    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    if ($username == null || $username == FALSE) {
        $error = 'Username is not valid';
    }
    $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
    if ($password == null || $password == FALSE) {
        $error = 'Password is not valid';
    }
    $url = filter_input(INPUT_POST, 'return', FILTER_SANITIZE_URL);
    if ($url == null || $url == FALSE) {
        $url = './';
    }

    if (!isset($error)) {
        $admin = Admin::loginAdmin($username, $password);
        if (is_null($admin)) {
            $error = "Invalid login crendentials";
        } else {
                if ($admin->getTwo() && !is_null($admin->getSecret())) {
                   $_SESSION[URL] = $url;
                   $_SESSION["password"] = $password;
                   $_SESSION["username"] = $username;
                    header("location: checkpoint");
                    die();
                }
            
            $_SESSION[ADMIN] = $admin->getUsername();
            header("location: " . $url);
            die();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no">
        <title>Clients Page</title>
        <link rel="icon" type="image/x-icon" href="assets/img/favicon.ico"/>
        <!-- BEGIN GLOBAL MANDATORY STYLES -->
        <link href="https://fonts.googleapis.com/css?family=Nunito:400,600,700" rel="stylesheet">
        <link href="cork/ltr/demo5/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
        <link href="cork/ltr/demo5/assets/css/plugins.css" rel="stylesheet" type="text/css"/>
        <link href="../css/form-2.css" rel="stylesheet" type="text/css"/>
        <link href="cork/ltr/demo5/assets/css/forms/theme-checkbox-radio.css" rel="stylesheet" type="text/css"/>
        <link href="cork/ltr/demo5/assets/css/forms/switches.css" rel="stylesheet" type="text/css"/>
    </head>
    <body class="form">


        <div class="form-container outer">
            <div class="form-form">
                <div class="form-form-wrap">
                    <div class="form-container">
                        <div class="form-content">

                            <h1 class="">Client Sign In</h1>

                            <form class="text-left" method="post" action="login<?php echo ((isset($_GET[RETURN_URL])) ? "?" . RETURN_URL . "=" . $_GET[RETURN_URL] : ""); ?>">
                                <?php if (isset($error)) { ?>
                                    <span style="color: red"><?php echo "$error"; ?></span>
                                <?php }
                                ?>
                                <div class="form">

                                    <div id="username-field" class="field-wrapper input">
                                        <label for="username">USERNAME</label>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-user"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>
                                        <input id="username" name="username" type="text" class="form-control" >
                                    </div>

                                    <div id="password-field" class="field-wrapper input mb-2">
                                        <div class="d-flex justify-content-between">
                                            <label for="password">PASSWORD</label>
                                            <input type="hidden" name="return" value="<?php echo((isset($_GET[RETURN_URL])) ? $_GET[RETURN_URL] : ""); ?>"/>
                                        </div>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-lock"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
                                        <input id="password" name="password" type="password" class="form-control" >
                                    </div>
                                    <div class="d-sm-flex justify-content-between">
                                        <div class="field-wrapper">
                                            <button type="submit" class="btn btn-primary" value="">Log In</button>
                                        </div>
                                    </div>
                                </div>
                            </form>

                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script src="cork/ltr/demo5/assets/js/libs/jquery-3.1.1.min.js" type="text/javascript"></script>
        <script src="cork/ltr/demo5/bootstrap/js/popper.min.js" type="text/javascript"></script>
        <script src="cork/ltr/demo5/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
        <!-- BEGIN GLOBAL MANDATORY SCRIPTS -->

    </body>
</html>