<?php include 'include/header.php';?>
<?php include 'include/navbar.php';?>

  <style>
    body {
      background: #f7f7f7;
    }
    .login-box {
      max-width: 400px;
      margin: 80px auto;
      padding: 30px;
      background: #fff;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
      border-radius: 10px;
    }
    .login-box h2 {
      margin-bottom: 25px;
    }
    .form-control:focus {
      box-shadow: none;
      border-color: #00796B;
    }
    .btn-warning {
      width: 100%;
    }
  </style>

<div class="login-box">

  <h2 class="text-center text-dark">Forget Password</h2>

<?php
if (isset($_POST['submit'])) {

    $recaptcha_response = $_POST['g-recaptcha-response'];

    // Verify reCAPTCHA
    $verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$recaptcha_secret}&response={$recaptcha_response}");
    $response = json_decode($verify);

    if (!$response->success) {
        echo "<p class='p-2 bg-danger text-white text-center rounded'>reCAPTCHA failed. Please try again.</p>";
        
    }else{

    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $check = mysqli_query($conn, "SELECT * FROM users WHERE email = '$email' AND status = 1");

    if (mysqli_num_rows($check) > 0) {
        $user = mysqli_fetch_assoc($check);
        $code = $user['verification_code'];
        $reset_link = "$domain/reset-password.php?code=$code";

        $subject = "Reset Your Password";
        $message = "
        <html>
          <body style='margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #f4f4f4;'>
            <table width='100%' cellpadding='0' cellspacing='0'>
              <tr>
                <td align='center' style='padding: 40px 0;'>
                  <table width='600' cellpadding='0' cellspacing='0' style='background-color: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 10px rgba(0,0,0,0.1); border: 2px solid #00796B;'>
                    <tr>
                      <td style='background-color: #00796B; padding: 20px; text-align: center; color: white; font-size: 24px; font-weight: bold;'>
                        $website_name
                      </td>
                    </tr>
                    <tr>
                      <td style='padding: 30px; color: #333333;'>
                        <h2 style='margin-top: 0;'>Reset Your Password</h2>
                        <p>Hello,</p>
                        <p>We received a request to reset your password. Click the button below to proceed.</p>
                        <div style='text-align: center; margin: 30px 0;'>
                          <a href='$reset_link' style='background-color: #00796B; color: #ffffff; padding: 12px 25px; text-decoration: none; border-radius: 5px; font-size: 16px;'>Reset Password</a>
                        </div>
                        <p>If you did not request this, you can safely ignore this email.</p>
                        <p>Thanks,<br>The $website_name Team</p>
                      </td>
                    </tr>
                    <tr>
                      <td style='background-color: #f1f1f1; padding: 15px; text-align: center; font-size: 13px; color: #888888;'>
                        &copy; " . date('Y') . " $website_name. All rights reserved.
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </body>
        </html>";

        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8\r\n";
        $headers .= "From: $website_name <$hosting_email>\r\n";

        mail($email, $subject, $message, $headers);

        echo "<p class='p-2 bg-success text-white text-center rounded'>Reset link sent to your email.</p>";
        

    } else {
        echo "<p class='p-2 bg-danger text-white text-center rounded'>Email not found or not verified.</p>";
        
    }
}
    
}
?>


  <form action="" method="POST" style="max-width: 400px; margin: auto;">
        <input type="email" name="email" placeholder="Enter your email" required class="form-control">
      <br>
         <div class="mb-3">
  <div class="g-recaptcha" data-sitekey="<?php echo $recaptcha_site;?>"></div>
</div>
        <button type="submit" name="submit" class="btn my-btn btn-block">Send Reset Link</button>
    </form>

</div>














 <?php include 'include/footer.php';?>