<?php
/**
 * Register Page — FITZONE Gym Booking System
 */
session_start();
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/includes/auth.php';

if (isLoggedIn()) {
    header('Location: ' . url('/member/katalog.php'));
    exit;
}

$errors = [];

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

    // Validasi
    if (empty($nama))     $errors[] = 'Nama wajib diisi.';
    if (empty($email))    $errors[] = 'Email wajib diisi.';
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Format email tidak valid.';
    if (strlen($password) < 8) $errors[] = 'Password minimal 8 karakter.';
    if ($password !== $confirm) $errors[] = 'Konfirmasi password tidak cocok.';

    // Cek email unik
    if (empty($errors)) {
        $stmt = $pdo->prepare('SELECT id FROM users WHERE email = ? LIMIT 1');
        $stmt->execute([$email]);
        if ($stmt->fetch()) {
            $errors[] = 'Email sudah terdaftar.';
        }
    }

    // Simpan
    if (empty($errors)) {
        $hash = password_hash($password, PASSWORD_BCRYPT);
        $stmt = $pdo->prepare('INSERT INTO users (nama, email, password, role) VALUES (?, ?, ?, ?)');
        $stmt->execute([$nama, $email, $hash, 'member']);

        setFlash('success', 'Registrasi berhasil! Silakan login.');
        header('Location: ' . url('/login_member.php'));
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar — FITZONE GYM</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700;900&display=swap" rel="stylesheet">
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: { volt: '#D4F900', dark: '#0D0D0D', surface: '#1A1A1A', border: '#2a2a2a', muted: '#888888' },
                    fontFamily: { inter: ['Inter', 'sans-serif'] }
                }
            }
        }
    </script>
    <link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/style.css">
</head>
<body class="bg-dark text-white font-inter min-h-screen flex items-center justify-center px-4">

<div class="w-full max-w-md" style="opacity:0;animation:fadeUp 0.5s ease 0.1s forwards;">
    <!-- Logo -->
    <div class="text-center mb-8">
        <a href="<?= BASE_URL ?>/index.php" class="inline-block text-4xl font-black tracking-tight">
            <span class="text-white">FIT</span><span class="text-volt">ZONE</span>
        </a>
        <p class="text-muted text-sm mt-2">Buat akun baru</p>
    </div>

    <!-- Card -->
    <div class="bg-surface border border-border rounded-2xl p-8">
        <?php if ($errors): ?>
            <div class="mb-4 px-4 py-3 rounded-lg bg-red-500/10 border border-red-500/30 text-red-400 text-sm font-medium space-y-1">
                <?php foreach ($errors as $err): ?>
                    <p>• <?= htmlspecialchars($err) ?></p>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <form method="POST" action="" class="space-y-5">
            <div>
                <label class="form-label" for="nama">Nama Lengkap</label>
                <input type="text" id="nama" name="nama" class="form-input" placeholder="Nama lengkap" value="<?= htmlspecialchars($_POST['nama'] ?? '') ?>" required>
            </div>

            <div>
                <label class="form-label" for="email">Email</label>
                <input type="email" id="email" name="email" class="form-input" placeholder="nama@email.com" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>" required>
            </div>

            <div>
                <label class="form-label" for="password">Password</label>
                <input type="password" id="password" name="password" class="form-input" placeholder="Minimal 8 karakter" required>
            </div>

            <div>
                <label class="form-label" for="confirm_password">Konfirmasi Password</label>
                <input type="password" id="confirm_password" name="confirm_password" class="form-input" placeholder="Ulangi password" required>
            </div>

            <button type="submit" class="btn-volt w-full text-center text-base tracking-wide" id="btn-register">
                Daftar
            </button>
        </form>

        <div class="mt-6 text-center text-sm text-muted">
            Sudah punya akun? 
            <a href="<?= BASE_URL ?>/login_member.php" class="text-volt font-bold hover:underline">Login</a>
        </div>
    </div>
</div>

</body>
</html>
