<?php
session_start();
require("../includes/db_connect.php");

if (isset($_SESSION['usr_email']) && isset($_SESSION['usr_accessToken']) && isset($_SESSION['usr_accountId']) && (!empty($_SESSION['usr_accountId']))) {
    header('Location: ../user/account');
    exit();
} else if (!isset($_GET['token']) || !isset($_GET['email'])) {
    header('Location: ../login');
    exit();
} else {

    $email = $_GET['email'];
    $token = $_GET['token'];

    $_SESSION['usr_rs_email'] = $email;

    $rstpd_sql = "SELECT * FROM password_reset WHERE email =? AND token=? AND email_confirmation=? LIMIT 1";
    $rstpd_sth = $conn->prepare($rstpd_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
    $rstpd_sth->execute([$email, $token, 0]);
    $password_reset = $rstpd_sth->fetchAll();

    if (count($password_reset) > 0) {

        foreach ($password_reset as $key => $value) {
            $date = $value['date'];

            $datetime1 = new DateTime(convertDate($date));

            $datetime2 = new DateTime(convertDate(date('Y-m-d H:i:s')));

            $difference = $datetime1->diff($datetime2);

            $minutes_lapsed =  $difference->i;

            if ($minutes_lapsed < 11) {

                $page_content = "<div class='uk-text-center'
                style='margin: 40px auto 0px;position: relative;box-sizing: border-box;padding-bottom: 50px;'>
                <div style='top: 120px;'>
                    <div
                        style='max-width: 450px;border-radius: 40px;box-shadow: rgb(0 128 143 / 25%) 0px 8px 12px 0px;background-color: rgb(255, 255, 255);padding-left: 24px;padding-right: 24px;padding-top: 32px;padding-bottom: 32px;'>

                        <!-- logo begin -->
                        <a class='uk-logo' href='index'>
                            <h4>New Password</h4>
                        </a>
                        <!-- logo end -->
                        <p class='uk-text-lead uk-margin-small-top uk-margin-medium-bottom'>Enter your new
                            password</p>
                        <div style='display: none;' id='feedbackPrompt'></div>
                        <!-- login form begin -->
                        <div class='uk-grid uk-form'>
                            <div class='uk-margin-small uk-width-1-1 uk-inline'>
                                <span class='uk-form-icon uk-form-icon-flip fas fa-lock1 fa-sm' style='pointer-events:unset;cursor:pointer;'>
                                    <i class='far fa-eye' id='togglePassword'></i>
                                </span>
                                <input class='uk-input uk-border-rounded' id='password' value=''
                                    type='password' placeholder='Password'>
                                <div class='invalid-feedback password_feedback'>Password is required</div>
                            </div>
                            <div class='uk-margin-small uk-width-1-1 uk-inline'>
                                <span class='uk-form-icon uk-form-icon-flip fas fa-lock1 fa-sm' style='pointer-events:unset;cursor:pointer;'>
                                    <i class='far fa-eye' id='toggleCPassword'></i>
                                </span>
                                <input class='uk-input uk-border-rounded' id='passwordconfirm' value=''
                                    type='password' placeholder='Confirm password'>
                                <div class='invalid-feedback password_feedback'>Confirm password is required
                                </div>
                            </div>
                            <div class='uk-margin-small uk-width-1-1'>
                                <button
                                    class='uk-button uk-width-1-1 uk-button-primary uk-border-rounded uk-float-left'
                                    type='submit' name='submit' id='set-password-btn'>Set Password
                                    <span style='display: none;'
                                        class='button_spinner spinner-border spinner-border-sm'
                                        role='status' aria-hidden='false'></button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>";
            } else {
                die("This link has expired.");
            }
        }
    } else {
        die("This link has expired.");
    }
}

function convertDate($date)
{
    $dt = str_replace('/', '-', $date);

    return date('Y-m-d H:i:s', strtotime($dt));
}

?>
<!DOCTYPE html>
<html lang="zxx" dir="ltr">

<head>
    <!-- Standard Meta -->
    <meta charset="utf-8">
    <title>New Password</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php require_once("../includes/header.php"); ?>
    <main>
        <!-- section content begin -->
        <div class="uk-section uk-padding-remove-vertical" style="background-color: #f7f7f7;">
            <div class="uk-container uk-container-expand">
                <div class="uk-width-expand@m uk-flex-middle">
                    <div class="uk-grid uk-flex-center">
                        <?php echo ($page_content); ?>
                    </div>
                </div>
            </div>
        </div>
        </div>
        </div>
        <!-- section content end -->
    </main>
    <?php require_once("../includes/footer.php"); ?>
    <script src="../js/new-password.js"></script>
    </body>

</html>