<?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['user_rr'])) {
    header("location: login");
    die();
}


if (isset($_POST['password'])) {
    $db = new DatabaseConn();
    $link = $db->connect();
    $email = $_SESSION['user_rr'];
    if ($email == null || $email == FALSE) {
        header("location: login");
        die();
    }
    $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
    if ($password == null || $password == FALSE) {
        $error = 'Password is not valid';
    } else if (strlen($password) < 8) {
        $error = 'Password is weak';
    }

    $cpassword = filter_input(INPUT_POST, 'cpassword', FILTER_SANITIZE_STRING);
    if (strcmp($password, $cpassword) != 0) {
        $error = 'Password does not match';
    }
    $code = filter_input(INPUT_POST, 'code', FILTER_SANITIZE_STRING);
    if ($code == null || $code == FALSE) {
        $error = 'Password reset code is not valid';
    } else {
        $res = mysqli_query($link, "select token from `ptoken` where token = '$code' and `user` = '$email'");
        if (mysqli_num_rows($res) !== 1) {
            $error = 'Password reset code is not valid';
        }
    }

    if (!isset($error)) {
         $stmt = mysqli_prepare($link, "update users set password = ? where email = ?");
        $stmt->bind_param("ss", $password, $email);
        $stmt->execute();
        if ($stmt->affected_rows === 1) {
            $success = "Your password has been updated. <a href='login'>Click to login</a>";
            mysqli_query($link, "delete from ptoken where `user` = '$email'");
        } else {
            $error = "Choose a different password";
        }
    }
}
?>

<!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">Change Password</h1>
                        <p class="text-white" data-aos="fade-right">Enter code and new password to complete action</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 action="reset-password" style="height: 500px" method="POST" id="rgForm">
                                    <?php if (isset($error)) { ?>
                                        <p class="error" style="color: red">Error: <?php echo "$error"; ?></p>
                                    <?php } ?>
                                    <?php if (isset($success)) { ?>
                                        <p class="success" style="color: green"><?php echo "$success"; ?></p>
                                    <?php } ?>
                                    <div class="form-group account-step">
                                        <input type="text" placeholder="Enter Code" name="code" class="form-control" required="required">
                                    </div>
                                    <div class="form-group account-step">
                                        <input type="password" placeholder="New Password" value="" name="password" class="form-control" required="required">
                                    </div>
                                    <div class="form-group account-step">
                                       <input type="password" placeholder="Confirm Password" value="" name="cpassword" class="form-control" required="required">
                                    </div>
                                    <<div class="btn_wrapper">
                                        <button type="submit" name="get_started" id="started" class="default-btn">Update  <i class="fa-solid fa-angle-right"></i></button>
                                    </div>
                </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>