<?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($_POST['email'])) {
    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    if ($email == null || $email == FALSE) {
        $error = 'Email is not valid';
    }
    $password = filter_input(INPUT_POST, 'password');
    if ($password == null || $password == FALSE) {
        $error = 'Password is not valid';
    }
    if (!isset($error)) {
        $db = new DatabaseConn();
        $link = $db->connect();
        $user = User::verifyLoginCredentials(User::instantiate($email, $password), $link);
        if (is_null($user)) {
            $error = "Invalid login crendentials";
        } else if ($user->getStatus() == SUSPENDED) {
            $error = "Your account has been suspended. Contact support.";
        } else {
            if (strcmp(NOT_CONFIRMED, $user->getStatus()) == 0) {
                $_SESSION[USER_] = serialize($user);
                $_SESSION['gid'] = true;
                $error = "Verify your email to continue. <a href='gid'>Click to continue</a>";
            } else {
                $url = filter_input(INPUT_POST, 'return', FILTER_SANITIZE_URL);
                if ($url == null || $url == FALSE) {
                    $url = 'dashboard/';
                }
                LoginRecord::saveUserRecords($user, $link);


                $settings = USettings::getSystemSettings($link, $user->getEmail());
                if ($settings->getTwofa() && !is_null($user->getFa())) {
                    $_SESSION[USERV] = $user->getEmail();
                    $_SESSION[URL] = $url;
                    header("location: checkpoint");
                    die();
                }
                $_SESSION[USER] = $user->getEmail();
                header("location: " . $url);
                die();
            }
        }
    }
}
$title = 'Log In ' . APP_NAME;
?>

<!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">Welcome Back!</h1>
                            <p class="text-white-75 lead mb-4">Log in to the easiest and most secure crypto wallet.</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">Sign In</h3>
                                            <p class="text-gray-700 pb-4">Don't have an account? <a href="create-wallet">Sign up </a></p>
                                        </div>
                                    </div>
                                    <form action="login" 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"  value="<?php
                                               if (isset($_POST['email'])) {
                                                   echo ($_POST['email']);
                                               }
                                               ?>" name="email" type="email" class="form-control mt-4" placeholder="Email*" >
                                        </div>
                                        <div class="input-group mb-6">
                                              <input type="hidden" name="return" value="<?php echo((isset($_GET[RETURN_URL])) ? $_GET[RETURN_URL] : ""); ?>"/>
                                            <input id="amount" type="password" name="password" class="form-control mt-4" placeholder="Password*" >
                                        </div>
                                         <input type="hidden" name="register" style="display: none" />
                                        <button type="submit" id="downloadapp"  class="btn btn-block btn-primary">
                                            Log In
                                        </button>
                                     
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </main>
        <?php require_once './partials/footer.php'; ?>
    </body>
</html>
