<?php
$pageName  = "Forgot Password";
include($_SERVER['DOCUMENT_ROOT'] . "/auth/header.php");

if (@$_SESSION['acct_email']) {
    header("Location:./account/dashboard.php");
}



if (isset($_POST['send-link'])) {
    $email = inputValidation($_POST['email']);
    $log = "SELECT * FROM users WHERE acct_email =:email";
    $stmt = $conn->prepare($log);
    $stmt->execute([
        'email' => $email
    ]);

    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    $validAcct_email = filter_var($email, FILTER_SANITIZE_EMAIL);

    if (!filter_var($validAcct_email, FILTER_VALIDATE_EMAIL)) {
        toast_alert("error", "Invalid email address please type a valid email address!");
    } elseif ($user['acct_email'] == "") {
        toast_alert("error", "No user is registered with this email address!");
    } else {

        $reset_token = bin2hex(random_bytes(16));
        date_default_timezone_set('Asia/kolkata');
        $date = date("Y-m-d");

        $sql = "UPDATE users SET resettoken=:reset_token,resettokenexp=:date WHERE acct_email=:email";
        $addUp = $conn->prepare($sql);
        $addUp->execute([
            'reset_token' => $reset_token,
            'date' => $date,
            'email' => $email
        ]);

        if (true) {
            
            // $full_name = $user['firstname'] . " " . $user['lastname'];
            // $APP_NAME = WEB_TITLE;
            // $APP_URL = WEB_URL;
            // $SITE_ADDRESS = $page['url_address'];
            // $APP_NUMBER = WEB_PHONE;
            // $APP_EMAIL = WEB_EMAIL;
            // $user_email = $user['acct_email'];
            // $user_acctno = $user['acct_no'];
            // $message = $sendMail->ForgotMsg($full_name, $email, $user_acctno, $reset_token, $APP_NAME, $APP_URL, $SITE_ADDRESS);
            // // User Email
            // $subject = "Password Reset" . "-" . $APP_NAME;
            // $email_message->send_mail($user_email, $message, $subject);

            toast_alert("success", "Password reset link sent to email", "Thanks!");
        } else {
            toast_alert("error", "Sorry Something Went Wrong !");
        }
    }
}


?>
<div class="container-fluid h-100 awrp anuphan">
    <form class="bg-sm-none px-lg-10 mt-5 p-0 mx-auto shadow-none anuphan post-form mx-auto border-none" method="POST" enctype="multipart/form-data" action="">
        <h3 class="anuphan-3 mt-4">
            Forgot Password
        </h3>
        <div class="form-wrapper">
            <p class="m-0 xsmall grey-color-2 mb-1">Email address</p>
            <input type="email" name="email" placeholder="Enter Email" required>
        </div>
       
        <button type="submit" class="bold-text mt-4 py-3 anuphan-2" name="send-link">
            Request Link
        </button>
        <div class="loader justify-content-center lpb mt-4 w-100 btn-grey add-radius-sm" style="height: 52px; display: none;">
            <span class="bar" style="border-radius: 0px;"></span>
            <span class="bar" style="border-radius: 0px;"></span>
            <span class="bar" style="border-radius: 0px;"></span>
            <span class="bar" style="border-radius: 0px;"></span>
        </div>

       
        <p class="m-0 mt-2">
            <a href="./login.php" class="primary-color anuphan-2">Remebered password?</a>
        </p>
    </form>
</div>


<?php

include_once($_SERVER['DOCUMENT_ROOT'] . "/auth/footer.php");

?>