<?php
include 'db.php';

$message = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $email = $_POST['email'];

    $stmt = $conn->prepare("SELECT id FROM users WHERE email=?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows === 1) {

        $token = bin2hex(random_bytes(32));
        $expire = date("Y-m-d H:i:s", strtotime("+1 hour"));

        $update = $conn->prepare("UPDATE users SET reset_token=?, token_expire=? WHERE email=?");
        $update->bind_param("sss", $token, $expire, $email);
        $update->execute();


       
        // SIMPLE MAIL (local server may not send)
        $resetLink = "https://sigallabs.af/reset_password.php?token=$token";
mail($email, "Password Reset", "Click this link to reset: $resetLink");


        $message = "✅ لینک بازیابی ارسال شد.";
    } else {
        $message = "❌ ایمیل پیدا نشد.";
    }
}
?>

<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>فراموشی رمز</title>
</head>
<body style="font-family:tahoma;direction:rtl;text-align:center;margin-top:100px;">

<h2>بازیابی رمز عبور</h2>

<form method="post">
    <input type="email" name="email" placeholder="ایمیل خود را وارد کنید" required>
    <br><br>
    <button type="submit">ارسال لینک</button>
</form>

<p><?php echo $message; ?></p>

</body>
</html>

