<?php
require_once 'config.php';
require_once 'auth_helper.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $nama = trim($_POST['nama']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];

    if (empty($username) || empty($nama) || empty($password) || empty($confirm_password)) {
        $error = "Semua field wajib diisi!";
    } elseif ($password !== $confirm_password) {
        $error = "Konfirmasi password baru tidak cocok!";
    } else {
        try {
            // Check if username and nama_lengkap exist and match
            $check = $pdo->prepare("SELECT id_user FROM user WHERE username = :username AND nama_lengkap = :nama");
            $check->execute([':username' => $username, ':nama' => $nama]);

            if ($check->rowCount() > 0) {
                // Update Password
                $hashed_password = password_hash($password, PASSWORD_DEFAULT);
                $sql = "UPDATE user SET password = :pass WHERE username = :username";
                $stmt = $pdo->prepare($sql);
                $stmt->execute([
                    ':pass' => $hashed_password,
                    ':username' => $username
                ]);

                $success = "Password berhasil diubah! Silakan login dengan password baru.";
            } else {
                $error = "Username atau Nama Lengkap tidak sesuai atau tidak ditemukan!";
            }
        } catch (PDOException $e) {
            $error = "Terjadi kesalahan sistem: " . $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="id">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Lupa Password - Sistem Informasi KUB Katedral</title>
    <link rel="stylesheet" href="style.css?v=<?= time() ?>">
</head>

<body class="auth-page">
    <div class="register-card">
        <div class="header" style="background: var(--primary); padding: 15px 20px; border-radius: 12px; border-bottom: 3px solid var(--gold); margin-bottom: 30px; text-align: left;">
            <h2 style="color: white; margin: 0; display: flex; align-items: center; justify-content: flex-start; gap: 10px; font-size: 1.25rem;">
                <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="color: var(--gold-light);">
                    <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path>
                    <path d="m9 12 2 2 4-4"></path>
                </svg>
                Reset Password
            </h2>
        </div>

        <?php if ($error): ?>
            <div class="alert alert-error" style="background: #fee2e2; color: #991b1b; padding: 10px; border-radius: 6px; margin-bottom: 20px; border-left: 4px solid #ef4444;">
                <?= $error ?>
            </div>
        <?php endif; ?>

        <?php if ($success): ?>
            <div class="alert alert-success" style="background: #d1fae5; color: #065f46; padding: 10px; border-radius: 6px; margin-bottom: 20px; border-left: 4px solid #10b981;">
                <?= $success ?>
                <br><br>
                <a href="login.php" style="font-weight:bold; color:#065f46;">Klik disini untuk Login</a>
            </div>
        <?php else: ?>
            <div style="margin-bottom: 20px; font-size: 0.9rem; color: #666; text-align: center;">
                Masukkan <strong>Username</strong> dan <strong>Nama Lengkap</strong> yang sesuai dengan akun Anda untuk mereset password.
            </div>
            <form method="POST">
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="username" required placeholder="Masukkan Username Anda">
                </div>
                <div class="form-group">
                    <label>Nama Lengkap</label>
                    <input type="text" name="nama" required placeholder="Masukkan Nama Lengkap Anda">
                </div>
                <div class="form-group">
                    <label>Password Baru</label>
                    <input type="password" name="password" required placeholder="Masukkan Password Baru">
                </div>
                <div class="form-group">
                    <label>Konfirmasi Password Baru</label>
                    <input type="password" name="confirm_password" required placeholder="Masukkan Ulang Password Baru">
                </div>
                <button type="submit" class="btn-register">Reset Password</button>
            </form>
        <?php endif; ?>

        <div class="login-link" style="margin-top: 20px; text-align: center; font-size: 0.9rem;">
            Ingat password Anda? <a href="login.php">Login di sini</a>
        </div>
    </div>

</body>

</html>
