<?php
// login.php
require_once 'config.php';
startSession();

// Redirect jika sudah login
if (isLoggedIn()) {
    if (hasRole('admin')) {
        header("Location: /fls3n2/admin/admin_dashboard/");
    } else {
        header("Location: /fls3n2/admin/juri_dashboard/");
    }
    exit;
}

$error = '';
$conn = connectDB();

// Proses login
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Ambil input dari POST dan ubah ke uppercase
    $username = strtoupper(sanitizeInput($_POST['username']));
    $password = strtoupper($_POST['password']); // Password juga uppercase

    $stmt = $conn->prepare("SELECT id, username, password, role, nama_lengkap, id_cabang_lomba, domisili FROM users WHERE username = ?");
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows == 1) {
        $user = $result->fetch_assoc();
        // Verifikasi password (tetap lowercase karena disimpan dengan hash)
        if (password_verify($password, $user['password'])) {
            // Set session
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['nama'] = $user['nama_lengkap'];
            $_SESSION['id_cabang_lomba'] = $user['id_cabang_lomba'];
            $_SESSION['domisili'] = $user['domisili'];

            // Redirect berdasarkan role
            if ($user['role'] === 'admin') {
                header("Location: /fls3n2/admin/admin_dashboard/");
            } else {
                header("Location: /fls3n2/admin/juri_dashboard/");
            }
            exit;
        } else {
            $error = "Password salah!";
        }
    } else {
        $error = "Username tidak ditemukan!";
    }

    $conn->close();
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="../assets/logoFLS3N.png" type="image/x-icon">
    <title>Login - Sistem Penilaian Karya Siswa</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
        
        :root {
            --primary-color: #8C52FF;
            --secondary-color: #5CE1E6;
            --accent-color: #FF00CC;
            --dark-color: #121212;
            --light-color: #f9fafb;
            --success-color: #00C853;
            --error-color: #FF3D71;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }
        
        body {
            background-color: var(--dark-color);
            overflow-x: hidden;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
        }
        
        /* Animated background */
        body::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: 
                radial-gradient(circle at 5% 20%, rgba(140, 82, 255, 0.2) 0%, transparent 20%),
                radial-gradient(circle at 90% 80%, rgba(92, 225, 230, 0.2) 0%, transparent 20%),
                radial-gradient(circle at 60% 30%, rgba(255, 0, 204, 0.15) 0%, transparent 20%);
            z-index: -1;
            opacity: 0.6;
        }
        
        .background-glow {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
            z-index: -1;
        }
        
        .glow {
            position: absolute;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
            border-radius: 50%;
            filter: blur(30px);
            opacity: 0.15;
        }
        
        .glow-1 {
            top: -100px;
            left: -100px;
            width: 300px;
            height: 300px;
            animation: float 15s infinite alternate;
        }
        
        .glow-2 {
            bottom: -150px;
            right: -100px;
            width: 500px;
            height: 500px;
            background: linear-gradient(90deg, var(--accent-color), var(--primary-color));
            animation: float 20s infinite alternate-reverse;
        }
        
        @keyframes float {
            0% {
                transform: translate(0, 0) scale(1);
            }
            50% {
                transform: translate(50px, 30px) scale(1.1);
            }
            100% {
                transform: translate(10px, -40px) scale(0.9);
            }
        }
        
        .container {
            position: relative;
            z-index: 10;
            padding: 0 15px;
        }
        
        .login-card {
            max-width: 450px;
            margin: 20px auto;
            background: rgba(24, 24, 27, 0.8);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
            border: 1px solid rgba(255, 255, 255, 0.1);
            animation: cardAppear 0.8s ease forwards;
            transform: translateY(20px);
            opacity: 0;
        }
        
        @keyframes cardAppear {
            to {
                transform: translateY(0);
                opacity: 1;
            }
        }
        
        .login-header {
            padding: 30px 30px 0;
            text-align: center;
        }
        
        .login-header .brand {
            margin-bottom: 10px;
            position: relative;
            display: inline-block;
        }
        
        .login-header .brand img {
            height: 70px;
            animation: pulse 3s infinite;
        }
        
        @keyframes pulse {
            0% {
                filter: drop-shadow(0 0 5px var(--primary-color));
            }
            50% {
                filter: drop-shadow(0 0 20px var(--secondary-color));
            }
            100% {
                filter: drop-shadow(0 0 5px var(--primary-color));
            }
        }
        
        .login-header h1 {
            color: white;
            font-size: 28px;
            font-weight: 700;
            margin: 15px 0 5px;
            background: linear-gradient(90deg, var(--secondary-color), var(--primary-color));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            letter-spacing: 1px;
        }
        
        .login-header p {
            color: rgba(255, 255, 255, 0.6);
            font-size: 16px;
            margin-bottom: 5px;
        }
        
        .login-body {
            padding: 20px 30px 30px;
        }
        
        .form-group {
            margin-bottom: 25px;
            position: relative;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 10px;
            color: rgba(255, 255, 255, 0.8);
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s;
        }
        
        .input-group {
            position: relative;
            border-radius: 12px;
            overflow: hidden;
        }
        
        .input-group::before {
            content: '';
            position: absolute;
            top: -2px;
            left: -2px;
            right: -2px;
            bottom: -2px;
            z-index: -1;
            border-radius: 14px;
            opacity: 0;
            transition: opacity 0.3s;
        }
        
        .input-group:focus-within::before {
            opacity: 1;
        }
        
        .input-group-prepend {
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 50px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: rgba(255, 255, 255, 0.6);
            z-index: 1;
        }
        
        .form-control {
            width: 100%;
            padding: 15px 15px 15px 50px;
            background: rgba(255, 255, 255, 0.08);
            border: none;
            border-radius: 12px;
            color: white;
            font-size: 16px;
            transition: all 0.3s;
        }
        
        .form-control:focus {
            background: rgba(255, 255, 255, 0.12);
            box-shadow: none;
            color: white;
            outline: none;
        }
        
        .form-control::placeholder {
            color: rgba(255, 255, 255, 0.4);
        }
        
        .btn-login {
            width: 100%;
            padding: 15px;
            background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
            border: none;
            border-radius: 12px;
            color: white;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            position: relative;
            overflow: hidden;
            letter-spacing: 1px;
        }
        
        .btn-login::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
            transition: all 0.5s;
        }
        
        .btn-login:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
        }
        
        .btn-login:hover::before {
            left: 100%;
        }
        
        .btn-login:active {
            transform: translateY(0);
        }
        
        .alert-error {
            padding: 15px;
            margin-bottom: 20px;
            background: rgba(255, 61, 113, 0.2);
            border-left: 4px solid var(--error-color);
            color: white;
            border-radius: 8px;
            animation: shake 0.5s ease;
        }
        
        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
            20%, 40%, 60%, 80% { transform: translateX(5px); }
        }
        
        .version-info {
            position: absolute;
            bottom: 20px;
            right: 20px;
            color: rgba(255, 255, 255, 0.3);
            font-size: 12px;
        }
        
        .particles {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -2;
            pointer-events: none;
        }
        
        .particle {
            position: absolute;
            width: 3px;
            height: 3px;
            background-color: rgba(255, 255, 255, 0.5);
            border-radius: 50%;
        }
        
        /* Responsive adjustments */
        @media (max-width: 576px) {
            .login-card {
                max-width: 100%;
                margin: 15px;
            }
            
            .login-header {
                padding: 20px 20px 0;
            }
            
            .login-body {
                padding: 15px 20px 25px;
            }
            
            .login-header h1 {
                font-size: 24px;
            }
            
            .login-header p {
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="background-glow">
        <div class="glow glow-1"></div>
        <div class="glow glow-2"></div>
    </div>
    
    <div id="particles" class="particles"></div>
    
    <div class="container">
        <div class="login-card">
            <div class="login-header">
                <div class="brand">
                    <img src="../assets/logoFLS3N.png" alt="Logo FLS3N">
                </div>
                <h1>Admin Portal</h1>
                <p>Sistem Penilaian Karya Siswa FLS3N</p>
            </div>
            
            <div class="login-body">
                <?php if (!empty($error)): ?>
                    <div class="alert-error">
                        <i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?>
                    </div>
                <?php endif; ?>
                
                <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
                    <div class="form-group">
                        <label for="username">Username</label>
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <i class="fas fa-user"></i>
                            </div>
                            <input type="text" class="form-control text-uppercase" id="username" name="username" placeholder="Masukkan username" required autocomplete="off" oninput="this.value = this.value.toUpperCase()">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="password">Password</label>
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <i class="fas fa-lock"></i>
                            </div>
                            <input type="password" class="form-control text-uppercase" id="password" name="password" placeholder="Masukkan password" required oninput="this.value = this.value.toUpperCase()">                        </div>
                    </div>
                    <button type="submit" class="btn-login">
                        <i class="fas fa-sign-in-alt me-2"></i> MASUK SISTEM
                    </button>
                </form>
            </div>
        </div>
    </div>
    
    <div class="version-info">
        <span>FLS3N © 2026 | v1.0.3</span>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Create animated background particles
        document.addEventListener('DOMContentLoaded', function() {
            const particlesContainer = document.getElementById('particles');
            const particleCount = 30;
            
            for (let i = 0; i < particleCount; i++) {
                createParticle();
            }
            
            function createParticle() {
                const particle = document.createElement('div');
                particle.classList.add('particle');
                
                // Random position
                const posX = Math.random() * window.innerWidth;
                const posY = Math.random() * window.innerHeight;
                
                // Random size
                const size = Math.random() * 3 + 1;
                
                // Random opacity
                const opacity = Math.random() * 0.5 + 0.1;
                
                // Random color
                const colors = ['#8C52FF', '#5CE1E6', '#FF00CC', '#FFFFFF'];
                const color = colors[Math.floor(Math.random() * colors.length)];
                
                // Set styles
                particle.style.left = posX + 'px';
                particle.style.top = posY + 'px';
                particle.style.width = size + 'px';
                particle.style.height = size + 'px';
                particle.style.opacity = opacity;
                particle.style.backgroundColor = color;
                
                // Set animation
                const duration = Math.random() * 15 + 10; // 10-25 seconds
                const delay = Math.random() * 5;
                
                particle.style.animation = `float ${duration}s infinite alternate-reverse`;
                particle.style.animationDelay = `-${delay}s`;
                
                // Add to container
                particlesContainer.appendChild(particle);
            }
            
            // Add floating effect to particles
            const style = document.createElement('style');
            style.textContent = `
                @keyframes float {
                    0% {
                        transform: translate(0, 0);
                    }
                    100% {
                        transform: translate(${Math.random() * 100 - 50}px, ${Math.random() * 100 - 50}px);
                    }
                }
            `;
            document.head.appendChild(style);
        });
    </script>
</body>
</html>