<?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();
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zxx">

<head>
    <?php require_once './partials/head.php'; ?>
    <style>
        #footer_section {
            padding-top: 135px;
        }
    </style>
</head>

<body>
<div class="sub-banner-section-outer position-relative">
    <header>
        <?php require_once './partials/header.php'; ?>
    </header>
      <section class="contact_info_section position-relative">
        <div class="container">
            <div class="row">
                <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                    <div class="banner-section-content">
                        <h1 class="text-white" data-aos="fade-up">Login</h1>
                        <p class="text-white" data-aos="fade-right">Don't have an account? <a href="register" style="color: #e73c3e">Register</a></p>
                  </div>
                </div>
                <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                    <div class="banner_image position-relative">
                        <div class="contact_info_form_content">
                    <form id="contactpage" style="height: 450px;" method="POST" action="login<?php echo ((isset($_GET[RETURN_URL])) ? "?" . RETURN_URL . "=" . $_GET[RETURN_URL] : ""); ?>">
                        <?php if (isset($error)) { ?>
                             <p class="error" style="color: red">Error: <?php echo "$error"; ?></p>
                        <?php } ?>
                        <div class="row">
                            <div class="col-lg-12 col-md-12 col-sm-12">
                                <div class="form-group mb-0">
                                <input type="email" value="<?php
                                        if (isset($_POST['email'])) {
                                            echo ($_POST['email']);
                                        }
                                        ?>" name="email" id="name" class="form-control" placeholder="Email">
                                </div>
                            </div>
                            <input type="hidden" name="return" value="<?php echo((isset($_GET[RETURN_URL])) ? $_GET[RETURN_URL] : ""); ?>"/>
                            <div class="col-lg-12 col-md-12 col-sm-12">
                                <div class="form-group mb-0">    
                                    <input  type="password" name="password" id="phonenum" class="form-control" placeholder="Password"> 
                                </div>
                            </div>
                          </div>
                        <div class="btn_wrapper">
                            <button type="submit" name="get_started" id="started" class="default-btn">Login <i class="fa-solid fa-angle-right"></i></button>
                        </div>
                        <p style="text-align: right;margin-top: 30px;text-decoration: underline;">
                            <a href="reset">Reset Password</a>
                        </p>
                    </form>
                </div>
                        <figure class="banner_image_bottom_shape mb-0 position-absolute top_bottom_shape">
                            <img src="images/sub_banner_image_bottom_shape.png" alt class="img-fluid">
                        </figure>
                    </div>
                </div>
            </div>
        </div>
    </section>
     
</div>

 <?php require_once './partials/footer.php'; ?>
</body>
</html>