<?php
include('../includes/classes/Mail.php');
$IS_PAGE_PUBLIC = true;

include('../firstInc.php');
$emailInvalid = false;
if (isset($_POST['email'])) {
    $customers = new Members;
    $user = $customers->members_select_email($_POST['email']);
    if (mysqli_num_rows($user) == 1) {
        $ud = mysqli_fetch_assoc($user);
        $token = uniqid("", true);

        $fpd = new ForgotPwds();
        $fpd->set_m_id($ud['id']);
        $fpd->set_key($token);
        $fpd->set_date_requested(date("Y-m-d H:i:s"));
        $fpd->set_date_expiry(date("Y-m-d H:i:s", strtotime("+3 Hours")));
        // $fpd->set_used_on();

        $fpd->saveOrUpdate();

        $mailContent = "Hello ,<br>A request to reset your password in " . APP_SHORT_NAME . " has been made.<br>To reset your password follow the link <a href='" . URL . "/forgot_step_2.php?token=$token'>Reset Password</a><br>If the request was not made by you it is safe to ignore it. ";
        // echo $ud['email']."<hr>". SOFTWARE_NAME." | Reset Password"."<hr>".$mailContent;''
        $z = sendEmail($ud['email_id'],  APP_SHORT_NAME . " | Reset Password", $mailContent);
        // $ui_notif = 'Password Reset Link Shared Successfully';
        _uiNotif("Password Reset Link Shared Successfully", false);

    } else {
        $ui_notif = 'Invalid email';
        $ui_notif_type = 'danger';
        $emailInvalid = true;
    }
}

include('header.php');
?>

<div class="container-xxl" style="max-width: 500px; margin:20vh auto">
    <div class="authentication-wrapper authentication-basic container-p-y my-auto">
        <div class="authentication-inner">
            <!-- Register -->
            <div class="card">
                <div class="card-body">
                    <!-- Logo -->
                    <div class="app-brand justify-content-center">
                        <a href="index.html" class="app-brand-link gap-2">
                            <span class="app-brand-logo">
                                <img src="<?= LOGO ?>" alt="" srcset="" width="80px">
                            </span>
                        </a>
                    </div>
                    <!-- /Logo -->
                    <h4 class="mb-2">Forgot Password?</h4>

                    <p class="mb-4">Enter your email and we'll send you instructions to reset your password</p>
                    <form id="formAuthentication" class="mb-3" action="" method="POST">
                        <div class="mb-3">
                            <label for="email" class="form-label">Your Registered Email ID</label>
                            <input name="email" type="email" class="form-control <?= $emailInvalid ? "is-invalid" : "" ?>" id="email" placeholder="Enter your email" autofocus />
                        </div>
                        <div class="mb-3">
                            <button class="btn btn-primary d-grid w-100" type="submit">Send Reset Link</button>
                        </div>
                    </form>
                    <div class="text-center">
                        <a href="login.php" class="d-flex align-items-center justify-content-center">
                            <i class="bx bx-chevron-left scaleX-n1-rtl bx-sm"></i>
                            Back to login
                        </a>
                    </div>
                </div>
            </div>
            <!-- /Register -->
        </div>
    </div>
</div>

<!-- / Content -->

<?php
include('footer.php');
