<?php
session_start();
require_once 'includes/auth.php';
require_once 'includes/functions.php';

// Jika sudah login, redirect ke dashboard
if (isLoggedIn()) {
    header('Location: index.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = clean($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    
    if (empty($username) || empty($password)) {
        $error = 'Username dan password harus diisi';
    } else {
        $result = loginUser($username, $password);
        
        if ($result['success']) {
            header('Location: index.php');
            exit;
        } else {
            $error = $result['message'];
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Umbul Tirtomarto</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#0ea5e9',
                        secondary: '#0284c7',
                        ink: '#0b1220'
                    },
                    boxShadow: {
                        soft: '0 20px 60px rgba(2, 132, 199, 0.18)',
                    }
                }
            }
        }
    </script>
</head>

<body class="min-h-screen bg-gradient-to-br from-sky-50 via-blue-50 to-indigo-100 flex items-center justify-center relative overflow-hidden">
    <!-- Decorative blobs -->
    <div class="absolute -top-24 -left-24 w-80 h-80 bg-sky-200/60 rounded-full blur-3xl"></div>
    <div class="absolute top-1/3 -right-24 w-96 h-96 bg-indigo-200/60 rounded-full blur-3xl"></div>
    <div class="absolute bottom-0 left-1/3 w-72 h-72 bg-blue-200/50 rounded-full blur-3xl"></div>

    <div class="relative max-w-md w-full mx-4">
        <!-- Header -->
        <div class="text-center mb-8">
            <div class="inline-flex items-center justify-center w-14 h-14 rounded-2xl bg-white/70 backdrop-blur border border-white shadow-sm">
                <svg width="28" height="28" viewBox="0 0 24 24" fill="none" class="text-primary">
                    <path d="M3 12c2.5-6 15.5-6 18 0" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                    <path d="M6 12c1.7-4 10.3-4 12 0" stroke="currentColor" stroke-width="2" stroke-linecap="round" opacity="0.7"/>
                    <path d="M9 12c1-2 5-2 6 0" stroke="currentColor" stroke-width="2" stroke-linecap="round" opacity="0.45"/>
                    <path d="M12 12v7" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                    <path d="M9 19h6" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                </svg>
            </div>

            <h1 class="mt-4 text-4xl font-extrabold tracking-tight text-ink">
                Umbul <span class="text-primary">Tirtomarto</span>
            </h1>
        </div>

        <!-- Card -->
        <div class="bg-white/70 backdrop-blur-xl border border-white/70 rounded-2xl shadow-soft p-8">
            <div class="flex items-center justify-between mb-6">
                <h2 class="text-2xl font-semibold text-ink">Login</h2>
                <span class="text-xs font-medium text-primary bg-primary/10 px-3 py-1 rounded-full border border-primary/20">
                    Secure Access
                </span>
            </div>

            <?php if ($error): ?>
            <div class="mb-5 rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-red-700">
                <div class="flex gap-3">
                    <div class="mt-0.5">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" class="text-red-600">
                            <path d="M12 9v4" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                            <path d="M12 17h.01" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                            <path d="M10.3 3.7 2.4 19a2 2 0 0 0 1.8 3h15.6a2 2 0 0 0 1.8-3L13.7 3.7a2 2 0 0 0-3.4 0Z" stroke="currentColor" stroke-width="2"/>
                        </svg>
                    </div>
                    <p class="text-sm leading-relaxed">
                        <?php echo htmlspecialchars($error); ?>
                    </p>
                </div>
            </div>
            <?php endif; ?>

            <form method="POST" action="" class="space-y-4">
                <div>
                    <label for="username" class="block text-sm font-medium text-gray-700 mb-2">
                        Username
                    </label>
                    <div class="relative">
                        <span class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 text-gray-400">
                            <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
                                <path d="M20 21a8 8 0 1 0-16 0" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                                <path d="M12 13a4 4 0 1 0 0-8 4 4 0 0 0 0 8Z" stroke="currentColor" stroke-width="2"/>
                            </svg>
                        </span>
                        <input
                            type="text"
                            id="username"
                            name="username"
                            required
                            placeholder="Masukkan username"
                            value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>"
                            class="w-full pl-10 pr-4 py-3 rounded-xl border border-gray-200 bg-white/80
                                   focus:outline-none focus:ring-4 focus:ring-primary/20 focus:border-primary
                                   transition"
                        >
                    </div>
                </div>

                <div>
                    <label for="password" class="block text-sm font-medium text-gray-700 mb-2">
                        Password
                    </label>
                    <div class="relative">
                        <span class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 text-gray-400">
                            <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
                                <path d="M17 11V8a5 5 0 0 0-10 0v3" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                                <path d="M7 11h10a2 2 0 0 1 2 2v7a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2Z" stroke="currentColor" stroke-width="2"/>
                            </svg>
                        </span>
                        <input
                            type="password"
                            id="password"
                            name="password"
                            required
                            placeholder="Masukkan password"
                            class="w-full pl-10 pr-4 py-3 rounded-xl border border-gray-200 bg-white/80
                                   focus:outline-none focus:ring-4 focus:ring-primary/20 focus:border-primary
                                   transition"
                        >
                    </div>
                </div>

                <button
                    type="submit"
                    class="group w-full rounded-xl bg-gradient-to-r from-primary to-secondary text-white
                           font-semibold py-3 px-4 shadow-lg shadow-sky-200/60
                           hover:brightness-110 active:brightness-95
                           focus:outline-none focus:ring-4 focus:ring-primary/25
                           transition"
                >
                    <span class="inline-flex items-center justify-center gap-2">
                        Login
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" class="opacity-90 group-hover:translate-x-0.5 transition">
                            <path d="M5 12h13" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                            <path d="M12 5l7 7-7 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                        </svg>
                    </span>
                </button>
            </form>
        </div>

        <!-- Footer -->
        <div class="text-center mt-6 text-gray-600 text-sm">
            <p>&copy; <?php echo date('Y'); ?> Wahana Kreasi Nusantara</p>
        </div>
    </div>
</body>
</html>
