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

    if (!isset($error)) {
        $db = new DatabaseConn();
        $link = $db->connect();
        $user = User::getCurrentUser($link, $email);
        if (is_null($user)) {
            $error = "Email does not exist";
        } 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 = "Your email has not been verified since you created an account with us. <a href='gid'>Click to continue</a>";
                } else {
                    $response = User::sendPasswordVerification($user, $link);
                    if ($response) {
                        $_SESSION["user_rr"] = $user->getEmail();
                        header("location: reset-password");
                        die();
                    } else {
                        $error = "We could not send password reset mail to the email provided";
                    }
                }
            }
        }
    }
}
?>

<!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">Reset</h1>
                        <p class="text-white" data-aos="fade-right">Enter email to reset password </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="reset">
                        <?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" class="form-control" placeholder="Enter Email" required="required">
                                </div>
                            </div>
                            
                          </div>
                        <div class="btn_wrapper">
                            <button type="submit" name="get_started" id="started" class="default-btn">Reset <i class="fa-solid fa-angle-right"></i></button>
                        </div>
                       
                    </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>