<?php
session_start(); // user email should not be longer 40 characters
date_default_timezone_set('Africa/Lagos');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once '../vendor/autoload.php';
require_once '../models/User.php';
require_once '../include/DatabaseConn.php';
require_once '../include/Constants.php';
require_once '../include/Helper.php';
require_once '../models/LoginRecord.php';
require_once '../models/USettings.php';
require_once '../include/GoogleAuthenticator.php';
require_once '../models/Settings.php';
require_once '../models/Admin.php';

$admin = Admin::loginAdmin($_SESSION["username"], $_SESSION["password"]);

if (is_null($admin)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ./login?" . RETURN_URL . "=" . $url);
    die();
}
if (isset($_SESSION[URL])) {

} else {
    header("location: login");
    die();
}

if (isset($_POST['code'])) {
    $db = new DatabaseConn();
    $link = $db->connect();
    $ga = new PHPGangsta_GoogleAuthenticator();
    $secret = $admin->getSecret();
    $checkResult = $ga->verifyCode($secret, $_POST['code'], 2);

    if (!$checkResult) {
        $error = "Invalid code entered";
    } else {
         $_SESSION[ADMIN] = $admin->getUsername();
        header("location: /clients/");
        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>Admin - Login 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="">Admin Google Autenticator</h1>

                            <form class="text-left" method="post" action="checkpoint<?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">Google Authenticator Code</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="code" type="text" class="form-control" >
                                    </div>

                                    <div class="d-sm-flex justify-content-between">
                                        <div class="field-wrapper">
                                            <button type="submit" class="btn btn-primary" value="">Continue</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>