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

// Already logged in? Redirect
if (isLoggedIn()) {
    header('Location: ' . ($_SESSION['role'] === 'admin' ? url('/admin/dashboard.php') : url('/member/katalog.php')));
    exit;
}

$error = '';

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

    if (empty($email) || empty($password)) {
        $error = 'Email dan password wajib diisi.';
    } else {
        $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ? AND role = 'member' LIMIT 1");
        $stmt->execute([$email]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['nama']    = $user['nama'];
            $_SESSION['email']   = $user['email'];
            $_SESSION['role']    = $user['role'];

            header('Location: ' . url('/member/katalog.php'));
            exit;
        } else {
            $error = 'Email atau password salah, atau Anda bukan Member.';
        }
    }
}

$flash = getFlash();
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Member — 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">Masuk ke akun Member Anda</p>
    </div>

    <!-- Card -->
    <div class="bg-surface border border-border rounded-2xl p-8">
        <?php if ($flash): ?>
            <div class="mb-4 px-4 py-3 rounded-lg border text-sm font-medium <?= $flash['type'] === 'success' ? 'bg-volt/10 border-volt/30 text-volt' : 'bg-red-500/10 border-red-500/30 text-red-400' ?>">
                <?= htmlspecialchars($flash['message']) ?>
            </div>
        <?php endif; ?>

        <?php if ($error): ?>
            <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">
                <?= htmlspecialchars($error) ?>
            </div>
        <?php endif; ?>

        <form method="POST" action="" class="space-y-5">
            <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 autofocus>
            </div>

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

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

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

    <!-- Demo credentials -->
    <div class="mt-4 text-center text-xs text-muted/60">
        <p>Demo Member: andi@mail.com / member123</p>
        <p class="mt-2"><a href="<?= BASE_URL ?>/login_admin.php" class="hover:text-white underline">Login sebagai Admin</a></p>
    </div>
</div>

</body>
</html>
