<?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 Sungsang</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#38BDF8', // Sky blue - biru muda
                        secondary: '#2DD4BF', // Teal - hijau mint
                        accent: '#14B8A6', // Teal darker untuk hover
                        ink: '#0b1220'
                    },
                    boxShadow: {
                        soft: '0 20px 60px rgba(56, 189, 248, 0.15)',
                    }
                }
            }
        }
    </script>
</head>

<body class="min-h-screen bg-gray-50 relative overflow-hidden">
    <!-- Decorative blobs untuk mobile -->
    <div class="md:hidden absolute -top-24 -left-24 w-80 h-80 bg-sky-300/40 rounded-full blur-3xl animate-pulse"></div>
    <div class="md:hidden absolute top-1/3 -right-24 w-96 h-96 bg-teal-300/40 rounded-full blur-3xl animate-pulse" style="animation-delay: 1s;"></div>
    <div class="md:hidden absolute bottom-0 left-1/3 w-72 h-72 bg-sky-200/40 rounded-full blur-3xl animate-pulse" style="animation-delay: 2s;"></div>

    <!-- Layout Container -->
    <div class="min-h-screen md:grid md:grid-cols-2">
        <!-- Left Side - Branding (Desktop/Tablet Landscape) -->
        <div class="hidden md:flex md:flex-col md:justify-center md:items-center md:px-12 lg:px-16 relative overflow-hidden bg-gradient-to-br from-primary via-secondary to-primary">
            <!-- Decorative elements -->
            <div class="absolute top-0 right-0 w-96 h-96 bg-white/10 rounded-full blur-3xl"></div>
            <div class="absolute bottom-0 left-0 w-80 h-80 bg-white/10 rounded-full blur-3xl"></div>
            <div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-72 h-72 bg-white/5 rounded-full blur-3xl"></div>
            
            <div class="relative z-10 text-center md:text-left w-full max-w-md">
                <!-- Logo -->
                <div class="inline-flex items-center justify-center w-20 h-20 rounded-2xl bg-white/20 backdrop-blur border border-white/30 shadow-xl mb-8">
                    <svg width="40" height="40" viewBox="0 0 24 24" fill="none" class="text-white">
                        <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>

                <!-- Title -->
                <h1 class="text-5xl lg:text-6xl font-extrabold tracking-tight text-white mb-4">
                    Umbul Sungsang
                </h1>
                <p class="text-xl lg:text-2xl text-white/90 mb-8 font-medium">
                    Sistem Manajemen Wisata
                </p>
                
                
            </div>
        </div>

        <!-- Right Side - Login Form (Desktop/Tablet) atau Full (Mobile) -->
        <div class="flex items-center justify-center min-h-screen md:min-h-0 p-4 md:p-8 lg:p-12 bg-gradient-to-br from-sky-50 via-teal-50 to-sky-100 md:bg-white">
            <div class="w-full max-w-md">
                <!-- Mobile Header -->
                <div class="md:hidden text-center mb-8">
                    <div class="inline-flex items-center justify-center w-16 h-16 rounded-2xl bg-gradient-to-br from-primary/20 to-secondary/20 backdrop-blur border border-white/50 shadow-lg">
                        <svg width="32" height="32" 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-5 text-4xl font-extrabold tracking-tight">
                        <span class="bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent">Umbul Sungsang</span>
                    </h1>
                    <p class="mt-2 text-sm text-gray-600">Sistem Manajemen Wisata</p>
                </div>

                <!-- Login Card -->
                <div class="bg-white md:bg-transparent md:shadow-none backdrop-blur-xl border border-white/80 md:border-0 rounded-2xl shadow-soft md:shadow-none p-8 relative overflow-hidden">
                    <!-- Decorative gradient overlay untuk mobile -->
                    <div class="md:hidden absolute top-0 right-0 w-32 h-32 bg-gradient-to-br from-primary/10 to-secondary/10 rounded-full blur-2xl -mr-16 -mt-16"></div>
                    
                    <div class="relative">
                        <div class="flex items-center justify-between mb-6">
                            <h2 class="text-2xl font-semibold text-gray-800">Login</h2>
                            <span class="text-xs font-medium text-secondary bg-gradient-to-r from-primary/10 to-secondary/10 px-3 py-1.5 rounded-full border border-secondary/20">
                                Secure Access
                            </span>
                        </div>

                        <?php if ($error): ?>
                        <div class="mb-5 rounded-xl border border-red-200 bg-gradient-to-r from-red-50 to-red-50/50 px-4 py-3 text-red-700 shadow-sm">
                            <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-5">
                            <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
                                               focus:outline-none focus:ring-4 focus:ring-secondary/20 focus:border-secondary
                                               transition-all duration-200 hover:border-gray-300"
                                    >
                                </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
                                               focus:outline-none focus:ring-4 focus:ring-secondary/20 focus:border-secondary
                                               transition-all duration-200 hover:border-gray-300"
                                    >
                                </div>
                            </div>

                            <button
                                type="submit"
                                class="group w-full rounded-xl bg-gradient-to-r from-primary via-secondary to-primary text-white
                                       font-semibold py-3.5 px-4 shadow-lg shadow-secondary/30
                                       hover:shadow-xl hover:shadow-secondary/40 hover:scale-[1.02]
                                       active:scale-[0.98]
                                       focus:outline-none focus:ring-4 focus:ring-secondary/25
                                       transition-all duration-200"
                            >
                                <span class="inline-flex items-center justify-center gap-2">
                                    Masuk
                                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" class="opacity-90 group-hover:translate-x-1 transition-transform duration-200">
                                        <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>
                </div>

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