<?php

session_start();

require_once "config.php";


$message="";



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


$email=trim($_POST['email']);



$stmt=$pdo->prepare("

SELECT *

FROM users

WHERE email=? AND status=1

LIMIT 1

");


$stmt->execute([$email]);


$user=$stmt->fetch();



if($user){



$token=bin2hex(random_bytes(32));


$expiry=date(
"Y-m-d H:i:s",
strtotime("+30 minutes")
);





$stmt=$pdo->prepare("

INSERT INTO password_resets

(user_id,token,expires_at)

VALUES(?,?,?)

");


$stmt->execute([

$user['id'],

$token,

$expiry

]);





$link="http://localhost/expenses/reset_password.php?token=".$token;





// EMAIL SEND HERE

// Replace with SMTP mailer

mail(

$email,

"Password Reset",

"Click this link to reset password:\n".$link

);




$message="Password reset link sent to your email.";



}else{


$message="Email address not found";


}



}



?>

<!DOCTYPE html>

<html>

<head>

<title>
Forgot Password
</title>


<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">


<style>


body{

font-family:Poppins;

background:#f1f5f9;

height:100vh;

display:flex;

align-items:center;

justify-content:center;

}


.box{

background:white;

padding:30px;

border-radius:15px;

width:350px;

box-shadow:0 10px 30px #0002;

}


input{

width:100%;

padding:12px;

border-radius:10px;

border:1px solid #ddd;

margin:15px 0;

}


button{

width:100%;

padding:12px;

background:#059669;

color:white;

border:0;

border-radius:10px;

}


.message{

background:#ecfdf5;

padding:10px;

border-radius:10px;

color:#065f46;

}



</style>


</head>


<body>



<div class="box">


<h2>
Forgot Password
</h2>


<p>
Enter your email address
</p>



<?php if($message){ ?>

<div class="message">

<?=$message?>

</div>


<?php } ?>



<form method="POST">


<input

type="email"

name="email"

placeholder="Email Address"

required>



<button type="submit">

<i class="fa fa-envelope"></i>

Send Reset Link

</button>


<a href="javascript:history.back()" 
style="
display:block;
margin-top:15px;
text-align:center;
background:#64748b;
color:white;
padding:12px;
border-radius:10px;
text-decoration:none;
">

<i class="fa fa-arrow-left"></i>

Back

</a>


</form>


</div>


</body>

</html>