<?php
session_start();
require_once "SystemController/DbConnection.php";
require 'SystemController/CsrfProtection.php';
$csrf = new CsrfProtection();


$db_handle = new Dbconnection();



$msg = "";
$email = "";



if(isset($_POST['recover'])){
    
    if (!isset($_POST['csrf_token']) || !$csrf->validateToken($_POST['csrf_token'])) {
        die("CSRF token validation failed!");
    }

  $email = $db_handle->sanitize($_POST['email']);

 $userExist = $db_handle->runBaseQuery("SELECT * FROM users WHERE email = '$email'");

if($userExist){
$msg = "<div class='alert alert-success mx-auto'><i class='fa fa-info-circle'></i>A link to reset your password has been sent to your email address. Follow this link to reset your password.</div>";
          

$time = time();
$token = bin2hex(random_bytes(50));

$db_handle->insertRecord("INSERT into password_reset (email,hash,timeout) VALUES (?,?,?)", "ssi", array($email,$token, $time));
    $messag = '
<div style="font-family:Arial,sans-serif;background-color:#f2f2f2;padding:20px">
  <table width="100%" cellpadding="0" cellspacing="0" bgcolor="#ffffff" style="max-width:700px;margin:auto;border-radius:8px;overflow:hidden">
    <tr>
      <td align="center" style="padding:30px 0;background-color:#EAFFF5">
        <a href="https://ovohinitiative.com" target="_blank">
          <img src="https://app.ovohinitiative.com/assets/img/logo1.png" width="200" alt="Ovoh Initiative">
        </a>
      </td>
    </tr>
    <tr>
      <td style="padding:40px;text-align:left;color:#333333">
        <h1 style="font-size:24px;margin-bottom:20px">Hello!</h1>
        <p style="font-size:16px;line-height:24px">You are receiving this email because we received a password reset request for your account.</p>
        <div style="text-align:center;margin:40px 0">
          <a href="https://app.ovohinitiative.com/forgot_password?token='. $token.'&email='.$email.'" style="background-color:#BC27F6;color:#ffffff;padding:15px 30px;text-decoration:none;border-radius:5px;display:inline-block;font-weight:bold;font-size:16px">Reset Password</a>
        </div>
        <p style="font-size:16px;line-height:24px">This link will expire in 60 minutes.</p>
        <p style="font-size:16px;line-height:24px">If you did not request a password reset, no further action is required.</p>
        <p style="font-size:16px;line-height:24px;margin-top:40px">Regards,<br><strong>Ovoh Initiative</strong></p>
        <p style="font-size:14px;line-height:20px;margin-top:30px;color:#777">If the button doesn\'t work, copy and paste this URL in your browser:<br><a href="https://app.ovohinitiative.com/forgot_password?token='.$token .'&email='.$email.'">ovohinitiative.com/forgot-password?token='.$token .'&email='.$email.'</a></p>
      </td>
    </tr>
  </table>
</div>';

            $subject = "Reset Password Notification";
                  
$db_handle->send_mail($email,$messag,$subject);

}else{
 
     
        $msg = "<div class='alert alert-danger '><i class='fa fa-info-circle'></i>Email does not exist on our database. Please make sure the email account is correct!</div>";
}

}

if(isset($_POST['resetpassword'])){

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

$details = $db_handle->runBaseQuery("SELECT * FROM password_reset WHERE email = '$email' and hash = '$token'");

$time_requested = $details[0]['timeout'];
$current_time = time();

$time_difference = $current_time - $time_requested;
 $email = $db_handle->sanitize($_POST['email']);
  $password = $db_handle->sanitize($_POST['password']);
$confirm = $db_handle->sanitize($_POST['password2']);

if($time_difference > 3600 ){
  
        
        
        $msg = "<div class='alert alert-danger '><i class='fa fa-info-circle'></i>Sorry, your password reset link is expired. Please request for another one.</div>";
}else{

if(strlen($password) < 8){
  
        
       
        
    $msg = "<div class='alert alert-danger '><i class='fa fa-info-circle'></i>Password should consist of 8 or more characters!</div>";
    
}elseif($password != $confirm){

        
        
        
        $msg = "<div class='alert alert-danger '><i class='fa fa-info-circle'></i>Passwords do not match check and try again.</div>";
}else{
 $update = $db_handle->update('UPDATE users SET password = ? WHERE email = ?', "ss", array($password, $email));

 if($update){
 
        
        
        $msg = "<div class='alert alert-danger '><i class='fa fa-info-circle'></i>Password has been reset successfully.</div>";

  $url = "login";
   echo '<META HTTP-EQUIV="refresh" content="3; URL='.$url.'">';
 }
}
}


 


}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="csrf-token" content="ocjB56H6vKcQjnq7sifD2mLVYgNhCe19CBr3prCe" />
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="icon" href="assets/img/favicon.png" type="image/x-icon">
        <link rel="icon" href="assets/img/favicon.ico" sizes="any">
        <link rel="icon" href="assets/img/favicon.svg" type="image/svg+xml">
        <link rel="apple-touch-icon" href="assets/img/favicon.png">
    
    <title>Sign In | Ovoh Initiative</title>
    <meta name="description" content="Log in to Ovoh Initiative to access your savings, apply for loans, and manage your financial goals securely.">

    <!-- FontAwesome CSS -->
    
    <!-- FontAwesome CSS -->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="style.css"><!-- Custom CSS -->
     <link rel="stylesheet" href="responsive.css">
    <!-- Responsive CSS -->
    <!-- IF VERSION 2  -->
        <script src='https://www.google.com/recaptcha/api.js'></script> 
        <!-- IF VERSION 3  -->
        <style>
      :root {
          /* Color */
          --bs-primary: #000000;
          --bs-secondary: #c0a146;
      }
  </style>
</head>
<body>
    
    <main>
            <section>
        <div class="row align-items-center g-0 w-100 h-100vh position-relative">
            <div class="col-md-5 d-md-block d-none">
                <div class="login-left-content">
                    <a href="https://ovohinitiative.com">
                        <img src="assets/img/logo.png" class="logo-img" alt="">
                    </a>
                </div>
            </div>
            <div class="col-md-7 overflow-hidden bg-white">
                <div class="login-right-content login-forgot-padding row">
                    
                    <?php
  if(isset($_GET['token'])){
$email = $_GET['email'];
    ?>
                    <div class="pb-0 px-0">
                        <div class="text-primary d-flex align-items-center justify-content-between">
                            <div>
                                <h2 class="fw-bold text-color title-text mb-2" style="color:#BA0C2F">Forgot Password</h2>
                                <p class="text-color">Enter your email to reset your password.</p>
                            </div>
                            <!-- FOR SMALL DEVICE TOP CATEGORIES -->
                                                    </div>
                        <form class="mt-3" method="POST" action="">
                            <?php echo $msg; ?>
                            <?php echo $csrf->csrfInput(); ?>                           <div class="form-group">
                                <label for="email" class="form-label">Email<span
                                        class="text-danger"> * </span></label>
                                <input type="email" class="form-control" name="email"
                                    placeholder="Email" value="<?php echo $email; ?>" id="email" required readonly>

                            </div>
                            <div class="form-group">
                                <label for="password" class="form-label">Password<span
                                        class="text-danger"> * </span></label>
                                <div class="form-control d-flex align-items-center gap-3">
                                    <input type="password" class="form-control border-0 p-0" name="password"
                                        placeholder="Password" id="password" required>
                                    <span>
                                        <a href="#"><i class="fa-light fa-eye-slash" id="eye"></i></a>
                                    </span>
                                </div>

                            </div>
                            
                            <div class="form-group">
                                <label for="password" class="form-label">Confirm Password<span
                                        class="text-danger"> * </span></label>
                                <div class="form-control d-flex align-items-center gap-3">
                                    <input type="password" class="form-control border-0 p-0" name="password2"
                                        placeholder="Password" id="password" required>
                                    <span>
                                        <a href="#"><i class="fa-light fa-eye-slash" id="eye"></i></a>
                                    </span>
                                </div>

                            </div>
                            
                            <button class="btn btn-danger w-100 mt-2 mb-3 padding"
                                type="submit" name="resetpassword">Reset Password</button>

                                                            <p class="fs-7 text-center mt-2">
                                    <a href="login"
                                        class=" fw-semibold text-decoration fw-600" style="color: #BA0C2F">Back to login</a>
                                </p>
                                                    </form>
                                            </div>
                                <?php }else{

  ?>            
                                            
                              <div class="pb-0 px-0">
                        <div class="text-primary d-flex align-items-center justify-content-between">
                            <div>
                                <h2 class="fw-bold text-color title-text mb-2" style="color:#BA0C2F">Forgot Password</h2>
                                <p class="text-color">Enter your email to reset your password.</p>
                            </div>
                            <!-- FOR SMALL DEVICE TOP CATEGORIES -->
                                                    </div>
                        <form class="mt-3" method="POST" action="">
                            <?php echo $msg; ?>
                            <?php echo $csrf->csrfInput(); ?>                           <div class="form-group">
                                <label for="email" class="form-label">Email<span
                                        class="text-danger"> * </span></label>
                                <input type="email" class="form-control" name="email"
                                    placeholder="Email" id="email" required>

                            </div>
                           
                            
                            <button class="btn btn-danger w-100 mt-2 mb-3 padding"
                                type="submit" name="recover">Send Password Reset Link</button>

                                                            <p class="fs-7 text-center mt-2">
                                    <a href="login"
                                        class=" fw-semibold text-decoration fw-600" style="color: #BA0C2F"> Back to login</a>
                                </p>
                                                    </form>
                                            </div>
                                            
                                             <?php } ?>
                                            
                                            
                                            
                </div>
            </div>
        </div>
    </section>
        </main>
    <script src="assets/js/jquery.min.js"></script><!-- jQuery JS -->
    <script src="assets/js/bootstrap.bundle.min.js"></script><!-- Bootstrap JS -->
    <script src="assets/js/toastr/toastr.min.js"></script><!-- Toastr JS -->
    <script>
        toastr.options = {
          "closeButton": true,
          "positionClass": "toast-top-right",
         
        }
                        $(window).on("load", function() {
          "use strict";
          $('#preloader').fadeOut('slow')
        });
        function myFunction() {
          "use strict";
          toastr.error("This operation was not performed due to demo mode");
          return false;
        }

        $('#name').on('blur', function() {
          "use strict";
          $('#slug').val($('#name').val().split(" ").join("-").toLowerCase());
        });
    </script>
            <script>
        function AdminFill(email, password) {
            $('#email').val(email);
            $('#password').val(password);
        }
        // password eye hide
        $(function() {
            $('#eye').click(function() {
                if ($(this).hasClass('fa-eye-slash')) {
                    $(this).removeClass('fa-eye-slash');
                    $(this).addClass('fa-eye');
                    $('#password').attr('type', 'text');
                } else {
                    $(this).removeClass('fa-eye');
                    $(this).addClass('fa-eye-slash');
                    $('#password').attr('type', 'password');
                }
            });
        });
    </script>
</body>
</html>