<?php
require('connection.inc.php');
require('security.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (isset($_SESSION['user_id'])) {
    // If not logged in, redirect to the login page (index.php)
    header('Location: index.php');
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    include_once 'csrf.php';
    if (!validate_csrf_token($_POST['csrf_token'])) {
        die('Invalid CSRF token!');
    }
    $email = $_POST['email'];
    $email = mysqli_real_escape_string($conn, $email);

    // Check if email exists in the database
    $sql = "SELECT * FROM users WHERE email = '$email' AND deleted_at IS NULL AND role_id = 3 LIMIT 1 ";
    $result = $conn->query($sql);

    if ($result->num_rows == 1) {
        // Generate OTP
        $otp = rand(100000, 999999); // Generate a 6-digit OTP

        // Store OTP in session for verification later
        $_SESSION['otp'] = $otp;
        $_SESSION['otp_email'] = $email; // Store email for OTP sending

// Email subject and message
$subject = "Your One-Time Password (OTP) for Secure Access";

$message = "
    <html>
    <head>
        <title>Your OTP Code</title>
    </head>
    <body style='font-family: Arial, sans-serif;'>
        <div style='max-width: 600px; margin: auto; padding: 20px; border: 1px solid #ddd; border-radius: 8px;'>
            <h2 style='text-align: center; color: #333;'>Loveberi</h2>
            <p style='color: #555;'>
                Dear Valued User,
            </p>
            <p style='color: #555;'>
                We have received a request to access your account. For security purposes, please use the One-Time Password (OTP) below to proceed:
            </p>
            <p style='text-align: center; font-size: 24px; font-weight: bold; color: #333;'>
                $otp
            </p>
            <p style='color: #555;'>
                This OTP is valid for 10 minutes. If you did not make this request, please ignore this email or contact our support team immediately.
            </p>
            <p style='color: #555;'>
                Thank you,<br>
                The Loveberi Team
            </p>
            <div style='text-align: center; margin-top: 20px;'>
                <a href='https://Loveberi.com' style='display: inline-block; padding: 10px 20px; background-color: #007BFF; color: white; text-decoration: none; border-radius: 4px;'>Visit Our Website</a>
            </div>
        </div>
    </body>
    </html>
";

// Additional headers
$headers = "From: Loveberi <info@Loveberi.com>\r\n" .
           "Reply-To: info@Loveberi.com\r\n" .
           "MIME-Version: 1.0\r\n" .
           "Content-Type: text/html; charset=UTF-8\r\n";

         unset($_SESSION['csrf_token']);
        // Send OTP to user's email
        if (mail($email, $subject, $message, $headers)) {
            echo "
                <script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
                <script>
                 document.addEventListener('DOMContentLoaded', function() {
                    Swal.fire({
                        icon: 'success',
                        title: 'Success',
                        text: 'OTP has been sent to your email.',
                    }).then(() => {
                        window.location.href = 'verify_otp.php';
                    });
                    });
                </script>
            ";
        } else {
            echo "
                <script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
                <script>
                 document.addEventListener('DOMContentLoaded', function() {
                    Swal.fire({
                        icon: 'error',
                        title: 'Error',
                        text: 'Failed to send the OTP. Please try again.',
                    });
                    });
                </script>
            ";
        }
    } else {
        echo "
            <script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
            <script>
             document.addEventListener('DOMContentLoaded', function() {
                Swal.fire({
                    icon: 'error',
                    title: 'Error',
                    text: 'No user found with this email address.',
                });
                });
            </script>
        ";
    }
}

?>
<!doctype html>
<html class="no-js" lang="zxx">
   

<head>
      <meta charset="utf-8">
      <meta http-equiv="x-ua-compatible" content="ie=edge">
      <title>Love berri</title>
      <meta name="description" content="">
      <meta name="viewport" content="width=device-width, initial-scale=1">

      <!-- Place favicon.ico in the root directory -->
      <link rel="shortcut icon" type="image/x-icon" href="assets/img/logo/favicon.png">

      <!-- CSS here -->
      <link rel="stylesheet" href="assets/css/bootstrap.min.css">
      <link rel="stylesheet" href="assets/css/animate.css">
      <link rel="stylesheet" href="assets/css/swiper-bundle.css">
      <link rel="stylesheet" href="assets/css/slick.css">
      <link rel="stylesheet" href="assets/css/magnific-popup.css">
      <link rel="stylesheet" href="assets/css/spacing.css">
      <link rel="stylesheet" href="assets/css/meanmenu.css">
      <link rel="stylesheet" href="assets/css/nice-select.css">
      <link rel="stylesheet" href="assets/css/fontawesome.min.css">
      <link rel="stylesheet" href="assets/css/icon-dukamarket.css">
      <link rel="stylesheet" href="assets/css/jquery-ui.css">
      <link rel="stylesheet" href="assets/css/main.css">
   </head>
   <body>


      <!-- Scroll-top -->
      <button class="scroll-top scroll-to-target" data-target="html">
         <i class="icon-chevrons-up"></i>
      </button>
      <!-- Scroll-top-end-->


      <!-- header-area-start -->
      <?php 
      include 'header.php'
      ?>
      <!-- header-area-end -->

      <main>

         <!-- breadcrumb-area-start -->
         <div class="breadcrumb__area pt-5 pb-5">
            <div class="container">
               <div class="row">
                  <div class="col-lg-12">
                     <div class="tp-breadcrumb__content">
                        <div class="tp-breadcrumb__list">
                           <span class="tp-breadcrumb__active"><a href="index.html">Home</a></span>
                           <span class="dvdr">/</span>
                           <span>Forgot Password</span>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
         <!-- breadcrumb-area-end -->
<style>
    .tptrack__input-wrapper {
   position: relative;
   width: 100%;
}

.tptrack__input-group {
   display: flex;
   align-items: center;
   background-color: #fff;
   border: 1px solid #ddd;
   border-radius: 8px;
   padding: 0.6rem 1rem;
   box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.tptrack__input-icon {
   color: #999;
   font-size: 16px;
   margin-right: 10px;
}

.tptrack__input {
   border: none;
   outline: none;
   width: 100%;
   font-size: 16px;
   background: transparent;
   padding: 0;
   color: #333;
}

.tptrack__input::placeholder {
   color: #aaa;
}

@media (max-width: 576px) {
   .tptrack__input-group {
      padding: 0.5rem 0.75rem;
   }

   .tptrack__input-icon {
      font-size: 14px;
   }

   .tptrack__input {
      font-size: 14px;
   }
}

</style>
         <!-- track-area-start -->
         <section class="track-area pb-40">
   <div class="container">
      <div class="row justify-content-center">
         <div class="col-lg-6 col-sm-12">
            <div class="tptrack__product mb-40">
               <div class="tptrack__content grey-bg">
                  <div class="tptrack__item d-flex mb-20">
                     <div class="tptrack__item-icon">
                        <i class="fal fa-unlock-alt"></i>
                     </div>
                     <div class="tptrack__item-content">
                        <h4 class="tptrack__item-title">Forgot Password</h4>
                        <p>Enter your registered email address to receive a password reset link.</p>
                     </div>
                  </div>
                  <form method="POST" action="#">
                    <?php include_once 'csrf.php'; ?>
                    <input type="hidden" name="csrf_token" value="<?php echo generate_csrf_token(); ?>">
                    <div class="tptrack__input-wrapper mb-20">
                       <div class="tptrack__input-group">
                          <span class="tptrack__input-icon"><i class="fal fa-envelope"></i></span>
                          <input type="email" name="email" class="tptrack__input" placeholder="Email address" required>
                       </div>
                    </div>

                     <div class="tptrack__btn mb-20">
                        <button type="submit" name="submit" class="tptrack__submition active">
                           Send OTP <i class="fal fa-paper-plane"></i>
                        </button>
                     </div>
                  </form>
                  
                  <div class="tpsign__account">
                     <a href="log-in.php"><i class="fal fa-long-arrow-left"></i> Back to Login</a>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</section>

         <!-- track-area-end -->

         
         <!-- feature-area-start -->
     <?php 
     include 'footer.php'
     ?>
         <!-- feature-area-end -->

      </main>

      <!-- footer-area-start -->
  
      <!-- footer-area-end -->
      

      <!-- JS here -->
      <script src="assets/js/jquery.js"></script>
      <script src="assets/js/waypoints.js"></script>
      <script src="assets/js/bootstrap.bundle.min.js"></script>
      <script src="assets/js/swiper-bundle.js"></script>
      <script src="assets/js/nice-select.js"></script>
      <script src="assets/js/slick.js"></script>
      <script src="assets/js/magnific-popup.js"></script>
      <script src="assets/js/counterup.js"></script>
      <script src="assets/js/wow.js"></script>
      <script src="assets/js/isotope-pkgd.js"></script>
      <script src="assets/js/imagesloaded-pkgd.js"></script>
      <script src="assets/js/countdown.js"></script>
      <script src="assets/js/ajax-form.js"></script>
      <script src="assets/js/jquery-ui.js"></script>
      <script src="assets/js/meanmenu.js"></script>
      <script src="assets/js/main.js"></script>
   </body>

</html>
