<?php
require __DIR__ . '/includes/functions.php';
$currentPage = 'account';
$pageTitle = 'Reset Password | Furnify';
$customerId = isset($_GET['c']) ? (int) $_GET['c'] : (isset($_POST['customer_id']) ? (int) $_POST['customer_id'] : 0);
$token = isset($_GET['t']) ? (string) $_GET['t'] : (isset($_POST['token']) ? (string) $_POST['token'] : '');
$errors = array();
$success = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!verify_csrf_token((string) (isset($_POST['csrf_token']) ? $_POST['csrf_token'] : ''))) {
        $errors[] = 'Security token expired. Please try again.';
    }
    $new = isset($_POST['new_password']) ? (string) $_POST['new_password'] : '';
    $confirm = isset($_POST['confirm_password']) ? (string) $_POST['confirm_password'] : '';
    if ($new !== $confirm) {
        $errors[] = 'Password confirmation does not match.';
    }
    if (!$errors) {
        $result = customer_reset_password_with_token($customerId, $token, $new);
        if ($result['ok']) { $success = true; } else { $errors[] = $result['error']; }
    }
}
require __DIR__ . '/includes/header.php';
?>
<section class="page-section"><div class="container narrow"><div class="premium-panel p-4 p-lg-5"><p class="eyebrow">Customer account</p><h1>Reset password</h1><?php if ($success): ?><div class="alert alert-success">Password reset successful. You are now logged in.</div><a href="customer-dashboard.php" class="btn btn-primary">Go to Dashboard</a><?php else: ?><?php foreach ($errors as $error): ?><div class="alert alert-danger"><?= e($error) ?></div><?php endforeach; ?><form method="post" class="row g-3"><input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>"><input type="hidden" name="customer_id" value="<?= (int) $customerId ?>"><input type="hidden" name="token" value="<?= e($token) ?>"><div class="col-12"><label class="form-label">New password</label><input class="form-control" type="password" name="new_password" required minlength="8"></div><div class="col-12"><label class="form-label">Confirm new password</label><input class="form-control" type="password" name="confirm_password" required minlength="8"></div><div class="col-12"><button class="btn btn-primary w-100" type="submit">Reset password</button></div></form><?php endif; ?></div></div></section>
<?php require __DIR__ . '/includes/footer.php'; ?>
