<?php
require_once "../config/database.php";
require_once "../includes/lang.php";
?>
<!DOCTYPE HTML>
<html lang="<?= htmlspecialchars(currentLang()) ?>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= htmlspecialchars(t("lost_password")) ?> - Konzona Hub</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="assets/css/style.css" type="text/css">
<link rel="stylesheet" href="assets/css/owl.carousel.min.css" type="text/css">
<link href="assets/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/colors/purple.css">
</head>
<body>
<section class="primary-bg">
<div class="container">
<div id="login_signup">
<div class="form_wrap_m">
<div class="white_box">
<h3><?= htmlspecialchars(t("lost_password")) ?></h3>

<form id="lostPasswordForm">
  <div class="form-group">
    <input type="email" id="email" class="form-control" placeholder="<?= htmlspecialchars(t("email_address")) ?>" required>
  </div>
  <div class="form-group">
    <input type="submit" class="btn btn-block" value="<?= htmlspecialchars(t("submit")) ?>">
  </div>
</form>

<p><a href="index.php"><?= htmlspecialchars(t("back_to_sign_in")) ?></a></p>
<div class="back_home"><a href="index.php" class="btn outline-btn btn-sm"><i class="fa fa-angle-double-left"></i> <?= htmlspecialchars(t("back_to_home")) ?></a></div>
<div id="msg" style="margin-top:10px;"></div>

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

<script>
const msg = document.getElementById("msg");

document.getElementById("lostPasswordForm").addEventListener("submit", async function(e){
  e.preventDefault();
  const email = document.getElementById("email").value.trim();

  if (!email) {
    msg.style.color = "red";
    msg.textContent = "<?= htmlspecialchars(t('email_required')) ?>";
    return;
  }

  msg.style.color = "blue";
  msg.textContent = "<?= htmlspecialchars(t('sending')) ?>...";

  try {
    const r = await fetch("../api/password_reset_admin.php", {
      method: "POST",
      headers: {"Content-Type": "application/json"},
      credentials: "same-origin",
      body: JSON.stringify({ action: "request", email })
    });

    const text = await r.text();
    let j = {};
    try {
      j = JSON.parse(text);
    } catch (err) {
      j = { success: false, error: text || "invalid_response" };
    }

    if (j.success) {
      msg.style.color = "green";
      msg.textContent = "Admin will grant your password soon.";
      document.getElementById("email").value = "";
    } else {
      msg.style.color = "red";
      msg.textContent = j.error || "Error";
    }
  } catch (e) {
    msg.style.color = "red";
    msg.textContent = "Error sending request";
  }
});
</script>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/owl.carousel.min.js"></script>
<script src="assets/js/interface.js"></script>
</body>
</html>
