<?php
session_start();
if (isset($_SESSION['usuario'])) {
    // Redirigir basado en el rol del usuario
    if ($_SESSION['rol'] == 1) {
        header('Location: views/superadmin/dashboard.php'); // Superadmin
    } elseif ($_SESSION['rol'] == 2) {
        header('Location: views/admin/dashboard.php'); // Admin
    } elseif ($_SESSION['rol'] == 3) {
        header('Location: views/docente/dashboard.php'); // Docente
    } else {
        echo "Rol desconocido.";
        session_destroy(); // Terminar la sesión si el rol es desconocido
    }
    exit;
}

$error_message = ''; // Inicializar la variable para evitar errores
?>

<!DOCTYPE html>
<html lang="es">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Inicio de Sesión</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: {
                            50: '#f0f9ff',
                            100: '#e0f2fe',
                            200: '#bae6fd',
                            300: '#7dd3fc',
                            400: '#38bdf8',
                            500: '#0ea5e9',
                            600: '#0284c7',
                            700: '#0369a1',
                            800: '#075985',
                            900: '#0c4a6e',
                            950: '#082f49',
                        },
                        secondary: {
                            50: '#f5f3ff',
                            100: '#ede9fe',
                            200: '#ddd6fe',
                            300: '#c4b5fd',
                            400: '#a78bfa',
                            500: '#8b5cf6',
                            600: '#7c3aed',
                            700: '#6d28d9',
                            800: '#5b21b6',
                            900: '#4c1d95',
                            950: '#2e1065',
                        },
                    },
                    animation: {
                        'float': 'float 6s ease-in-out infinite',
                        'pulse-slow': 'pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite',
                        'bounce-slow': 'bounce 3s infinite',
                        'spin-slow': 'spin 3s linear infinite',
                        'fade-in': 'fadeIn 0.5s ease-out forwards',
                        'slide-up': 'slideUp 0.5s ease-out forwards',
                        'slide-right': 'slideRight 0.5s ease-out forwards',
                        'zoom-in': 'zoomIn 0.5s ease-out forwards',
                    },
                    keyframes: {
                        float: {
                            '0%, 100%': { transform: 'translateY(0)' },
                            '50%': { transform: 'translateY(-10px)' },
                        },
                        fadeIn: {
                            '0%': { opacity: '0' },
                            '100%': { opacity: '1' },
                        },
                        slideUp: {
                            '0%': { transform: 'translateY(20px)', opacity: '0' },
                            '100%': { transform: 'translateY(0)', opacity: '1' },
                        },
                        slideRight: {
                            '0%': { transform: 'translateX(-20px)', opacity: '0' },
                            '100%': { transform: 'translateX(0)', opacity: '1' },
                        },
                        zoomIn: {
                            '0%': { transform: 'scale(0.95)', opacity: '0' },
                            '100%': { transform: 'scale(1)', opacity: '1' },
                        },
                    },
                    boxShadow: {
                        'neon': '0 0 5px rgba(14, 165, 233, 0.5), 0 0 20px rgba(14, 165, 233, 0.3)',
                        'neon-purple': '0 0 5px rgba(139, 92, 246, 0.5), 0 0 20px rgba(139, 92, 246, 0.3)',
                        'inner-glow': 'inset 0 0 15px 0 rgba(255, 255, 255, 0.5)',
                        'card-3d': '0 10px 30px -10px rgba(0, 0, 0, 0.3)',
                    }
                }
            }
        }
    </script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
    <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@300;400;500;600;700&family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Inter', sans-serif;
            background-size: 400% 400%;
            animation: gradientBG 15s ease infinite;
            height: 100vh;
            overflow: hidden;
        }
        
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Poppins', sans-serif;
        }
        
        @keyframes gradientBG {
            0% {
                background-position: 0% 50%;
            }
            50% {
                background-position: 100% 50%;
            }
            100% {
                background-position: 0% 50%;
            }
        }
        
        .login-card {
            backdrop-filter: blur(16px);
            background: rgba(255, 255, 255, 0.85);
            border: 1px solid rgba(255, 255, 255, 0.3);
            box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
            transition: all 0.3s ease;
        }
        
        .login-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px 0 rgba(31, 38, 135, 0.47);
        }
        
        .input-field {
            transition: all 0.3s ease;
            border: 2px solid transparent;
            background-color: rgba(255, 255, 255, 0.9);
        }
        
        .input-field:focus {
            transform: translateY(-2px);
            border-color: #0ea5e9;
            box-shadow: 0 0 0 4px rgba(14, 165, 233, 0.1);
        }
        
        .input-icon {
            transition: all 0.3s ease;
        }
        
        .input-field:focus + .input-icon {
            color: #0ea5e9;
            transform: scale(1.1);
        }
        
        .login-button {
            transition: all 0.3s ease;
            background-size: 200% auto;
            box-shadow: 0 4px 15px 0 rgba(14, 165, 233, 0.4);
        }
        
        .login-button:hover {
            background-position: right center;
            transform: translateY(-2px);
            box-shadow: 0 6px 20px 0 rgba(14, 165, 233, 0.6);
        }
        
        .login-button:active {
            transform: translateY(0);
        }
        
        .floating-shapes div {
            position: absolute;
            width: 60px;
            height: 60px;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            animation: float 8s infinite;
        }
        
        .floating-shapes div:nth-child(1) {
            top: 10%;
            left: 10%;
            width: 80px;
            height: 80px;
            animation-delay: 0s;
            background: linear-gradient(45deg, rgba(14, 165, 233, 0.2), rgba(139, 92, 246, 0.2));
        }
        
        .floating-shapes div:nth-child(2) {
            top: 20%;
            right: 15%;
            width: 100px;
            height: 100px;
            animation-delay: 1s;
            background: linear-gradient(45deg, rgba(139, 92, 246, 0.2), rgba(14, 165, 233, 0.2));
        }
        
        .floating-shapes div:nth-child(3) {
            bottom: 15%;
            left: 20%;
            width: 70px;
            height: 70px;
            animation-delay: 2s;
            background: linear-gradient(45deg, rgba(14, 165, 233, 0.2), rgba(139, 92, 246, 0.2));
        }
        
        .floating-shapes div:nth-child(4) {
            bottom: 25%;
            right: 10%;
            width: 120px;
            height: 120px;
            animation-delay: 3s;
            background: linear-gradient(45deg, rgba(139, 92, 246, 0.2), rgba(14, 165, 233, 0.2));
        }
        
        .floating-shapes div:nth-child(5) {
            top: 50%;
            left: 5%;
            width: 90px;
            height: 90px;
            animation-delay: 4s;
            background: linear-gradient(45deg, rgba(14, 165, 233, 0.2), rgba(139, 92, 246, 0.2));
        }
        
        .floating-shapes div:nth-child(6) {
            top: 40%;
            right: 5%;
            width: 60px;
            height: 60px;
            animation-delay: 5s;
            background: linear-gradient(45deg, rgba(139, 92, 246, 0.2), rgba(14, 165, 233, 0.2));
        }
        
        @keyframes float {
            0%, 100% {
                transform: translateY(0) rotate(0deg);
            }
            25% {
                transform: translateY(-20px) rotate(5deg);
            }
            50% {
                transform: translateY(0) rotate(10deg);
            }
            75% {
                transform: translateY(20px) rotate(5deg);
            }
        }
        
        .logo-container {
            animation: float 6s ease-in-out infinite;
        }
        
        .logo-glow {
            position: relative;
        }
        
        .logo-glow::after {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 100%;
            height: 100%;
            background: radial-gradient(circle, rgba(14, 165, 233, 0.4) 0%, rgba(14, 165, 233, 0) 70%);
            z-index: -1;
            opacity: 0;
            transition: opacity 0.5s ease;
        }
        
        .logo-container:hover .logo-glow::after {
            opacity: 1;
        }
        
        .error-message {
            animation: shake 0.5s cubic-bezier(.36,.07,.19,.97) both;
        }
        
        @keyframes shake {
            10%, 90% {
                transform: translate3d(-1px, 0, 0);
            }
            20%, 80% {
                transform: translate3d(2px, 0, 0);
            }
            30%, 50%, 70% {
                transform: translate3d(-4px, 0, 0);
            }
            40%, 60% {
                transform: translate3d(4px, 0, 0);
            }
        }
    </style>
</head>

<body class="bg-gradient-to-br from-primary-600 via-primary-800 to-secondary-800 flex items-center justify-center min-h-screen p-4">
    <!-- Floating shapes background -->
    <div class="floating-shapes">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
    
    <!-- Login Card -->
    <div class="login-card p-8 rounded-2xl w-full max-w-md animate-zoom-in">
        <div class="text-center mb-8">
            <div class="logo-container inline-block mb-4">
                <div class="logo-glow">
                    <img src="public/images/logo.png" alt="Logo de la Institución" class="w-28 h-28 object-contain mx-auto">
                </div>
            </div>
            <h2 class="text-3xl font-bold text-gray-800 mb-2 animate-slide-up" style="animation-delay: 0.1s;">Bienvenido</h2>
            <p class="text-gray-600 animate-slide-up" style="animation-delay: 0.2s;">Ingresa tus credenciales para acceder al sistema</p>
        </div>
        
        <form action="login.php" method="post" class="space-y-6">
            <div class="animate-slide-up" style="animation-delay: 0.3s;">
                <label for="username" class="block text-sm font-medium text-gray-700 mb-1">Nombre de Usuario</label>
                <div class="relative">
                    <input type="text" id="username" name="username" required
                        class="input-field w-full px-4 py-3 pl-12 rounded-xl shadow-sm focus:outline-none"
                        placeholder="Ingresa tu usuario">
                    <i class="fas fa-user input-icon absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                </div>
            </div>
            
            <div class="animate-slide-up" style="animation-delay: 0.4s;">
                <label for="contraseña" class="block text-sm font-medium text-gray-700 mb-1">Contraseña</label>
                <div class="relative">
                    <input type="password" id="contraseña" name="contraseña" required
                        class="input-field w-full px-4 py-3 pl-12 rounded-xl shadow-sm focus:outline-none"
                        placeholder="Ingresa tu contraseña">
                    <i class="fas fa-lock input-icon absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                </div>
            </div>
            
            <div class="animate-slide-up" style="animation-delay: 0.5s;">
                <button type="submit"
                    class="login-button w-full bg-gradient-to-r from-primary-500 to-secondary-500 text-white py-3 px-4 rounded-xl font-medium flex items-center justify-center">
                    <span>Iniciar Sesión</span>
                    <i class="fas fa-arrow-right ml-2"></i>
                </button>
            </div>
            
            <?php if ($error_message): ?>
                <div class="error-message bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded-lg animate-fade-in" style="animation-delay: 0.6s;">
                    <div class="flex items-center">
                        <i class="fas fa-exclamation-circle mr-2"></i>
                        <p><?php echo $error_message; ?></p>
                    </div>
                </div>
            <?php endif; ?>
        </form>
        
        <div class="mt-8 text-center animate-slide-up" style="animation-delay: 0.7s;">
            <a href="#" class="text-primary-600 hover:text-primary-800 transition-colors duration-300 font-medium inline-flex items-center">
                <i class="fas fa-key mr-2 text-xs"></i>
                <span>¿Olvidaste tu contraseña?</span>
            </a>
        </div>
        
        <div class="mt-8 pt-6 border-t border-gray-200 text-center text-gray-500 text-sm animate-slide-up" style="animation-delay: 0.8s;">
            <p>&copy; <?php echo date('Y'); ?> Instituto de Educacion Superior</p>
        </div>
    </div>
</body>

</html>