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

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

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

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

            if ($check->rowCount() > 0) {
                $error = "Username sudah digunakan!";
            } else {
                // Register User (Default role: anggota_kub)
                $hashed_password = password_hash($password, PASSWORD_DEFAULT);
                $sql = "INSERT INTO user (username, password, role, nama_lengkap) VALUES (:user, :pass, 'anggota_kub', :nama)";
                $stmt = $pdo->prepare($sql);
                $stmt->execute([
                    ':user' => $username,
                    ':pass' => $hashed_password,
                    ':nama' => $nama
                ]);

                $success = "Registrasi berhasil! Silakan login.";
            }
        } 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>Registrasi - 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 20h9"></path>
                    <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
                </svg>
                Daftar Akun Baru
            </h2>
        </div>

        <?php if ($error): ?>
            <div class="alert alert-error">
                <?= $error ?>
            </div>
        <?php endif; ?>

        <?php if ($success): ?>
            <div class="alert alert-success">
                <?= $success ?>
                <br><br>
                <a href="login.php" style="font-weight:bold; color:#065f46;">Klik disini untuk Login</a>
            </div>
        <?php else: ?>
            <form method="POST">
                <div class="form-group">
                    <label>Nama Lengkap</label>
                    <input type="text" name="nama" required placeholder="Masukkan Nama Lengkap">
                </div>
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="username" required placeholder="Masukkan Username">
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" required placeholder="Masukkan Password">
                </div>
                <div class="form-group">
                    <label>Konfirmasi Password</label>
                    <input type="password" name="confirm_password" required placeholder="Masukkan Konfirmasi Password">
                </div>
                <button type="submit" class="btn-register">Daftar Sekarang</button>
            </form>
        <?php endif; ?>

        <div class="login-link">
            Sudah punya akun? <a href="login.php">Login di sini</a>
        </div>
    </div>

</body>

</html>
