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


$db_handle = new Dbconnection();


if(isset($_SESSION['id']) && isset($_SESSION['loggedin'])){
    header("Location: users/dashboard.php");
}

$msg = "";
$email = "";
$username = "";
$fname = "";

$phone = "";
$referrer = "";

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

$token = bin2hex(random_bytes(50));

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


$user = $db_handle->runBaseQuery("SELECT * FROM users WHERE email = '$email' AND password = '$password' "); 
  
  
  if($user)
  {
    $name = $user[0]['first_name']." ".$user[0]['surname'];
    $_SESSION['id'] = $user[0]['id'];
    
$code = mt_rand(1000,9999);
$time = time();

$message = '<!DOCTYPE html>
<html>
  <body style="font-family: Arial, sans-serif; color: #333;">
    <table width="100%" style="max-width: 600px; margin: auto; border-collapse: collapse;">
      <tr>
        <td style="padding: 20px;">
          <h2 style="color: #4CAF50;">OTP Code</h2>
          <p>Hello '.$name.',</p>
          <p>You requested for an OTP code. Copy the code below to verify your account.</p>
          
         
          
          <p style="font-size: 24px; font-weight: bold; color: #4CAF50;">'.$code.'</p>
          <p>This code will expire in 15 minutes.</p>
          <p>If you didn’t request this, you can safely ignore this email.</p>
          <p>Thanks,<br>Ovoh\'s Initiative Team</p>

          <hr style="margin-top: 30px;">
          <p style="font-size: 12px; color: #888;">
            Ovoh Initiative Inc., Warri, Delta State, Nigeria.
            <br>
            This is an automated email—please do not reply.
          </p>
        </td>
      </tr>
    </table>
  </body>
</html>';

$subject = "Your verification code is ".$code;

$db_handle->send_mail($email,$message,$subject);

$update = $db_handle->update("update users SET code = ?, time = ? WHERE email = ?","sis",array($code,$time,$email));

    
    header("location: authenticate.php");
 
        
        
  }else{
    $msg = "<div class='alert alert-danger mx-auto'>
            
    <strong><i class='fa fa-info-circle'></i> Passwords do not match, please check and try again </strong>
  </div>";
  }
  
}
?>
<!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">
    
    <!-- Favicon --> 
        <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">
                    <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">LOGIN</h2>
                                <p class="text-color">Sign into your account.</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>
                            <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="d-flex">
                               <!-- <div class="form-group mb-2 col-6 d-flex align-items-center">
                                    <input class="form-check-input mt-0" type="checkbox" value="" name="check_terms"
                                        id="check_terms" checked>
                                    <label class="form-check-label cursor-pointer mx-1" for="check_terms">
                                        <span class="fs-7 text-color">
                                            Remember Me
                                        </span>
                                    </label>
                                </div>-->
                                <div
                                    class="text-end  mb-2 col-6">
                                    <a href="forgot_password" class="fs-7 fw-600">
                                        Forgot Password ?
                                    </a>
                                </div>
                            </div>
                            <button class="btn btn-danger w-100 mt-2 mb-3 padding"
                                type="submit">Login</button>

                                                            <p class="fs-7 text-center mt-2">Don't have an account?
                                    <a href="sign-up"
                                        class=" fw-semibold text-decoration fw-600" style="color: #BA0C2F">Create Your Account</a>
                                </p>
                                                    </form>
                                            </div>
                </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>