<?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';


if (isset($_SESSION[URL]) && isset($_SESSION[USERV])) {

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

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

    if (!$checkResult) {
        $error = "Invalid code entered";
    } else {
        $_SESSION[USER] = $user->getEmail();
        header("location: " . $_SESSION[URL]);
        die();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <?php require_once './partials/head.php'; ?>
    </head>
    <body>
        <style>
            @media only screen and (max-width: 768px) {
                .ll {
                    color: white !important;
                    background-color: #1452F5 !important;
                }
            }
        </style>
        <nav class="navbar navbar-expand-lg navbar-togglable navbar-dark bg-primary d-block">
             <div class="container">
                <a class="navbar-brand" href="index">
                    <img srcset="" class="sc-EHOje lkgfgG sc-cpmKsF iJpaCn" color="auto" sizes="" src="/logo.png" style="display: block;/*! height: 20px; */margin-left: 0px;width: auto;" width="auto" height="auto">
                    </a>


            </div>
        </nav>
        <main class="page-content" aria-label="Content">
            <section class="pt-4 pb-8 bg-gradient-primary">
                <div class="container">
                    <div class="row align-items-center justify-content-center justify-content-md-between">
                        <div class="col-12 col-lg-5 mb-4 text-center text-lg-left text-white">
                            <h1 class="display-3 font-weight-bold">Checkpoint!</h1>
                            <p class="text-white-75 lead mb-4">Continue your login</p>
                        </div>
                        <div class="col-12 col-lg-6 offset-md-1 text-center">
                            <div class="card shadow-lg">
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-12">
                                            <h3 class="font-weight-bold mb-1">Securtiy Checkpoint</h3>
                                        </div>
                                    </div>
                                    <form action="checkpoint" method="POST">
                                       <?php if (isset($error)) { ?>
                                        <p style="margin-bottom: 16px; color: red !important;" class="error" >Error: <?php echo "$error"; ?></p>
                                    <?php } ?>
                                        <div class="input-group mb-6">
                                            <input id="amount" name="code" type="text" class="form-control mt-4" placeholder="Google Authenticator Code*" >
                                        </div>
                                        <button type="submit" id="downloadapp"  class="btn btn-block btn-primary">
                                            Continue
                                        </button>
                                     
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </main>
        <?php require_once './partials/footer.php'; ?>
    </body>
</html>
