<?php
include_once("layout/header.php");
require_once("include/userClass.php");
require_once("include/loginFunction.php");

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

if (isset($_GET['email']) && isset($_GET['reset_token'])) {
    date_default_timezone_set('Asia/kolkata');
    $date = date("Y-m-d");
    $email = $_GET['email'];
    $reset_token = $_GET['reset_token'];

    $log2 = "SELECT * FROM users WHERE acct_email=:email";
    $stmt = $conn->prepare($log2);
    $stmt->execute([
        'email' => $email
    ]);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);


} else {
    toast_alert("error", "Sorry Something Went Wrong !");
}

if (isset($_POST['update'])) {
    $new_password = inputValidation($_POST['new_password']);
    $new_password2 = password_hash((string)$new_password, PASSWORD_BCRYPT);

    $sql2 = "UPDATE users SET acct_password=:acct_password,resettoken=:resettoken,resettokenexp=:resettokenexp WHERE acct_email=:email";
    $passwordUpdate = $conn->prepare($sql2);
    $passwordUpdate->execute([
        'acct_password' => $new_password2,
        'resettoken' => NULL,
        'resettokenexp' => NULL,
        'email' => $email
    ]);

    if (true) {
        if ($page['twillio_status'] == '1') {
            $number = $row['acct_phone'];
            $messageText = "Security Alert: Password Changed";
            $sendSms->sendSmsCode($number, $messageText);
        }
        
        // if ($page['padiwise_sms'] == '1') {
        //     $messageText = "Security Alert: Password Changed";
        //     $recipient = $row['acct_phone'];

        //     $responseBody = send_bulk_sms(array(
        //         'sender_name'=>get_setting('display_name'),
        //         'recipient'=>$recipient,
        //         'reference'=>date('Y').uniqid().rand(1,9),
        //         'message'=>$messageText
        //     ));
        // }

        $msg1 = "
            <div class='alert alert-warning'>
            
            <script type='text/javascript'>
                 
                    function Redirect() {
                    window.location='./login.php';
                    }
                    document.write ('');
                    setTimeout('Redirect()', 4000);
                 
                    </script>
                    
            <center><img src='./assets/images/loading.gif' width='180px'  /></center>
            
            
            <center>	<strong style='color:black;'>Your Password Change Successfully, Please Wait while we redirect you...
                   </strong></center>
              </div>
            ";
    } else {
        toast_alert("error", "Sorry Something Went Wrong !");
    }
}

?>

<div class="form-container outer">
    <div class="form-form">
        <div class="form-form-wrap">
            <div class="form-container">
                <div class="form-content">
                    
                     <?php if (isset($msg1)) echo $msg1; ?>

                    <h1 class="">New Password</h1>
                   
                    <p class="">Enter your new password below.</p>
                 <!--   <img src="./assets/settings/<?=$page['image']?>" class="navbar-logo" alt="logo" width="20%"> -->

                    <form class="text-left" method="POST">
                        <div class="form">

                           <div id="password-field" class="field-wrapper input mb-2">
                                <div class="d-flex justify-content-between">
                                    <label for="password">New Password</label>
                                </div>
                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-lock"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
                                <input name="new_password" minlength="6" maxlength="60" type="password" class="form-control" placeholder="Password">
                                <input type="hidden" name="email" class="form-control" value='.$email.'>
                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" id="toggle-password" class="feather feather-eye"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path><circle cx="12" cy="12" r="3"></circle></svg>
                            </div>
                            
                            
                            
                            <div class="d-sm-flex justify-content-between">
                                <div class="field-wrapper">
                                    <button type="submit" class="btn btn-primary" name="update" >Update Password</button>
                                </div>
                            </div>
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>

<?php

include_once("layout/footer.php");

?>

