<?php
require_once __DIR__ . '/app/helpers/csrf.php';
session_start();
$token = $_GET['token'] ?? '';
if ($token === '') {
    die('Invalid reset link.');
}
$csrfToken = generateCsrfToken();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Reset Password</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light d-flex align-items-center justify-content-center" style="height:100vh;">

<div class="card p-4 shadow" style="width:380px;">
    <h4 class="mb-3 text-center">Reset Password</h4>

    <input type="password" id="password" class="form-control mb-2" placeholder="New Password">
    <input type="password" id="confirm_password" class="form-control mb-3" placeholder="Confirm New Password">
    <button id="btnReset" class="btn btn-success w-100">Update Password</button>

    <div class="text-center mt-3">
        <a href="login.php">Back to Login</a>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
var csrfToken = <?= json_encode($csrfToken) ?>;
$('#btnReset').click(function () {
    $.post('password_reset_handler.php?action=reset', {
        token: <?= json_encode($token) ?>,
        password: $('#password').val(),
        confirm_password: $('#confirm_password').val(),
        _csrf_token: csrfToken
    }, function (res) {
        alert(res.message);
        if (res.status === 'success') {
            window.location = 'login.php';
        }
    }, 'json').fail(function () {
        alert('Unable to process request.');
    });
});
</script>

</body>
</html>
