<?php
require_once __DIR__ . '/../system/customer/auth.php';

$token = $_GET['token'] ?? $_POST['token'] ?? '';
$error = '';
$message = '';

if ($token === '') {
    $error = 'Token reset tidak ditemukan.';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf();

    $password = $_POST['password'] ?? '';
    $passwordConfirm = $_POST['password_confirm'] ?? '';

    if ($password !== $passwordConfirm) {
        $error = 'Konfirmasi password tidak sama.';
    } else {
        $result = customer_reset_password($pdo, $token, $password);

        if ($result['success']) {
            $message = $result['message'];
        } else {
            $error = $result['message'];
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Reset Password Customer</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-100 min-h-screen flex items-center justify-center px-4">
<div class="w-full max-w-md bg-white rounded-3xl shadow-xl p-8">
    <h1 class="text-3xl font-black text-green-700 text-center mb-2">Reset Password</h1>
    <p class="text-center text-slate-500 mb-6">Buat password baru.</p>

    <?php if ($error): ?>
        <div class="bg-red-100 border border-red-300 text-red-700 p-3 rounded-xl mb-4"><?= e($error) ?></div>
    <?php endif; ?>

    <?php if ($message): ?>
        <div class="bg-green-100 border border-green-300 text-green-700 p-3 rounded-xl mb-4"><?= e($message) ?></div>
        <a href="customer-login.php" class="block text-center bg-green-600 text-white font-bold py-3 rounded-xl">Login Sekarang</a>
    <?php else: ?>
        <form method="POST" class="space-y-4">
            <input type="hidden" name="csrf_token" value="<?= csrf_token() ?>">
            <input type="hidden" name="token" value="<?= e($token) ?>">
            <input type="password" name="password" required minlength="8" placeholder="Password baru" class="w-full border rounded-xl px-4 py-3">
            <input type="password" name="password_confirm" required minlength="8" placeholder="Ulangi password baru" class="w-full border rounded-xl px-4 py-3">
            <button class="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-3 rounded-xl">Reset Password</button>
        </form>
    <?php endif; ?>
</div>
</body>
</html>
