<!DOCTYPE html>
<html lang="es" class="h-full bg-slate-950">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo isset($title) ? $title : 'ASISMA CRM - Iniciar Sesión'; ?></title>
    <link rel="icon" type="image/png" href="<?php echo APP_URL; ?>/public/assets/img/favicon.png">
    <!-- Tailwind CSS CDN -->
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        brand: {
                            50: '#eff6ff',
                            100: '#dbeafe',
                            200: '#bfdbfe',
                            300: '#93c5fd',
                            400: '#60a5fa',
                            500: '#3b82f6',
                            600: '#2563eb',
                            700: '#1d4ed8',
                            800: '#1e40af',
                            900: '#1e3a8a',
                            950: '#0f172a',
                        }
                    }
                }
            }
        }
    </script>
    <!-- Google Fonts - Inter -->
    <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=Outfit:wght@300;400;500;600;700;800&family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <!-- SweetAlert2 -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <!-- Lucide Icons -->
    <script src="https://unpkg.com/lucide@latest"></script>
    
    <style>
        body {
            font-family: 'Plus Jakarta Sans', 'Outfit', sans-serif;
        }
        .animated-bg {
            background: linear-gradient(-45deg, #0f172a, #1e3a8a, #020617, #172554);
            background-size: 400% 400%;
            animation: gradientBG 15s ease infinite;
        }
        @keyframes gradientBG {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }
    </style>
</head>
<body class="h-full flex overflow-hidden text-slate-100">

    <!-- SPLIT: LEFT SIDE (GORGEOUS HERO) -->
    <div class="hidden lg:flex lg:w-1/2 animated-bg relative items-center justify-center p-12 overflow-hidden border-r border-slate-800">
        <!-- Floating glassmorphism element -->
        <div class="absolute inset-0 bg-[radial-gradient(circle_at_top_right,rgba(37,99,235,0.15),transparent_45%)]"></div>
        <div class="absolute top-1/4 left-10 w-72 h-72 bg-blue-600/10 rounded-full blur-3xl filter animate-pulse"></div>
        <div class="absolute bottom-1/4 right-10 w-96 h-96 bg-indigo-600/10 rounded-full blur-3xl filter animate-pulse" style="animation-delay: 2s;"></div>
        
        <div class="relative z-10 max-w-lg text-left">
            <div class="flex items-center gap-3 mb-8">
                <div class="w-12 h-12 rounded-2xl bg-gradient-to-tr from-blue-600 to-indigo-500 flex items-center justify-center shadow-lg shadow-blue-500/30">
                    <i data-lucide="layers" class="w-6 h-6 text-white"></i>
                </div>
                <span class="text-3xl font-extrabold tracking-tight bg-gradient-to-r from-white via-slate-100 to-slate-400 bg-clip-text text-transparent">ASISMA<span class="text-blue-500">CRM</span></span>
            </div>
            
            <h1 class="text-4xl font-extrabold tracking-tight text-white mb-4 leading-tight sm:text-5xl">
                Control Total de tus Franquicias y Leads
            </h1>
            
            <p class="text-lg text-slate-300 mb-8 font-light">
                Plataforma de Gestión Comercial Multi-Franquicia. Conecta marcas, gestiona embudos de venta, automatiza asignaciones e integra canales de mensajería al instante.
            </p>
            
            <!-- Bullet points with Lucide Icons -->
            <div class="space-y-4">
                <div class="flex items-start gap-3">
                    <div class="mt-1 p-1 bg-blue-500/10 border border-blue-500/20 text-blue-400 rounded-lg">
                        <i data-lucide="check" class="w-4 h-4"></i>
                    </div>
                    <div>
                        <h4 class="font-semibold text-white">Distribución Inteligente (Cascada)</h4>
                        <p class="text-sm text-slate-400">Cola rotativa con tiempos límite para asignaciones eficientes de los leads.</p>
                    </div>
                </div>
                <div class="flex items-start gap-3">
                    <div class="mt-1 p-1 bg-blue-500/10 border border-blue-500/20 text-blue-400 rounded-lg">
                        <i data-lucide="check" class="w-4 h-4"></i>
                    </div>
                    <div>
                        <h4 class="font-semibold text-white">Seguridad & Aislamiento</h4>
                        <p class="text-sm text-slate-400">Cada franquiciado visualiza y gestiona estrictamente sus datos.</p>
                    </div>
                </div>
                <div class="flex items-start gap-3">
                    <div class="mt-1 p-1 bg-blue-500/10 border border-blue-500/20 text-blue-400 rounded-lg">
                        <i data-lucide="check" class="w-4 h-4"></i>
                    </div>
                    <div>
                        <h4 class="font-semibold text-white">WhatsApp Integrado</h4>
                        <p class="text-sm text-slate-400">Mensajería en tiempo real para seguimiento de leads.</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- SPLIT: RIGHT SIDE (LOGIN FORM) -->
    <div class="w-full lg:w-1/2 bg-slate-950 flex flex-col justify-center px-6 py-12 sm:px-12 lg:px-20 relative">
        <!-- Floating back glow -->
        <div class="absolute inset-0 bg-[radial-gradient(circle_at_bottom_left,rgba(59,130,246,0.08),transparent_50%)]"></div>
        
        <div class="mx-auto w-full max-w-md relative z-10">
            <!-- Mobile Logo -->
            <div class="flex lg:hidden items-center justify-center gap-2 mb-8">
                <div class="w-10 h-10 rounded-xl bg-gradient-to-tr from-blue-600 to-indigo-500 flex items-center justify-center shadow-lg">
                    <i data-lucide="layers" class="w-5 h-5 text-white"></i>
                </div>
                <span class="text-2xl font-extrabold text-white">ASISMA<span class="text-blue-500">CRM</span></span>
            </div>
            
            

            <!-- Glassmorphism form wrapper -->
            <div class="bg-slate-900/60 border border-slate-800/80 rounded-2xl p-8 shadow-2xl backdrop-blur-md">
                <form id="loginForm" class="space-y-6">
                    <!-- Token redirección -->
                    <input type="hidden" name="return_url" value="<?php echo htmlspecialchars(isset($_GET['return_url']) ? $_GET['return_url'] : 'dashboard'); ?>">

                    <!-- Email Input -->
                    <div>
                        <label for="email" class="block text-sm font-semibold text-slate-300 mb-1">Correo Electrónico</label>
                        <div class="relative">
                            <span class="absolute inset-y-0 left-0 pl-3.5 flex items-center text-slate-500">
                                <i data-lucide="mail" class="w-5 h-5"></i>
                            </span>
                            <input type="email" id="email" name="email" required 
                                   class="block w-full pl-11 pr-4 py-3 bg-slate-950 border border-slate-800 rounded-xl text-slate-100 placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all"
                                   placeholder="correo@ejemplo.com">
                        </div>
                    </div>

                    <!-- Password Input -->
                    <div>
                        <div class="flex justify-between items-center mb-1">
                            <label for="password" class="block text-sm font-semibold text-slate-300">Contraseña</label>
                            <a href="<?php echo APP_URL; ?>/password/olvide" class="text-xs text-blue-500 hover:text-blue-400 transition-colors">¿Olvidaste tu contraseña?</a>
                        </div>
                        <div class="relative">
                            <span class="absolute inset-y-0 left-0 pl-3.5 flex items-center text-slate-500">
                                <i data-lucide="lock" class="w-5 h-5"></i>
                            </span>
                            <input type="password" id="password" name="password" required 
                                   class="block w-full pl-11 pr-11 py-3 bg-slate-950 border border-slate-800 rounded-xl text-slate-100 placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all"
                                   placeholder="••••••••">
                            <button type="button" id="togglePassword" class="absolute inset-y-0 right-0 pr-3.5 flex items-center text-slate-500 hover:text-slate-300 transition-colors">
                                <i data-lucide="eye" class="w-5 h-5" id="eyeIcon"></i>
                            </button>
                        </div>
                    </div>

                    <!-- Recordarme checkbox -->
                    <div class="flex items-center">
                        <input type="checkbox" id="remember" name="remember" value="1" class="w-4 h-4 rounded bg-slate-950 border-slate-800 text-blue-600 focus:ring-blue-500 focus:ring-offset-slate-950">
                        <label for="remember" class="ml-2 block text-sm text-slate-400 select-none">Recordar dispositivo por 30 días</label>
                    </div>

                    <!-- Submit Button -->
                    <button type="submit" id="submitBtn"
                            class="w-full py-3.5 bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-500 hover:to-indigo-500 text-white font-semibold rounded-xl transition-all duration-300 shadow-lg shadow-blue-500/20 hover:shadow-blue-500/40 focus:outline-none focus:ring-2 focus:ring-blue-500 flex items-center justify-center gap-2">
                        <span>Ingresar a la Plataforma</span>
                        <i data-lucide="arrow-right" class="w-5 h-5 transition-transform group-hover:translate-x-1" id="btnIcon"></i>
                        <svg class="hidden animate-spin h-5 w-5 text-white" id="btnSpinner" fill="none" viewBox="0 0 24 24">
                            <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                            <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                        </svg>
                    </button>
                </form>
            </div>
            
            <!-- Bottom mobile indicator -->
            <div class="text-center mt-8 text-xs text-slate-600">
                Plataforma de uso exclusivo. © <?php echo date('Y'); ?> ASISMA CRM.
            </div>
        </div>
    </div>

    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            // Inicializar Lucide Icons
            lucide.createIcons();

            // Toggle Password Visibility
            $('#togglePassword').on('click', function() {
                const passField = $('#password');
                const isPassword = passField.attr('type') === 'password';
                passField.attr('type', isPassword ? 'text' : 'password');
                
                // Cambiar icono
                const icon = $('#eyeIcon');
                icon.attr('data-lucide', isPassword ? 'eye-off' : 'eye');
                lucide.createIcons();
            });

            // Ajax Authenticate Submission
            $('#loginForm').on('submit', function(e) {
                e.preventDefault();

                // Cambiar botón a estado de carga
                $('#submitBtn').attr('disabled', true).addClass('opacity-80');
                $('#btnIcon').addClass('hidden');
                $('#btnSpinner').removeClass('hidden');

                const formData = $(this).serialize();

                $.ajax({
                    url: '<?php echo APP_URL; ?>/authenticate',
                    type: 'POST',
                    data: formData,
                    dataType: 'json',
                    success: function(response) {
                        if (response.success) {
                            Swal.fire({
                                icon: 'success',
                                title: '¡Acceso Correcto!',
                                text: response.message,
                                background: '#1e293b',
                                color: '#f8fafc',
                                confirmButtonColor: '#3b82f6',
                                showConfirmButton: false,
                                timer: 1500
                            }).then(() => {
                                window.location.href = response.redirect;
                            });
                        } else {
                            Swal.fire({
                                icon: 'error',
                                title: 'Error de Acceso',
                                text: response.message,
                                background: '#1e293b',
                                color: '#f8fafc',
                                confirmButtonColor: '#ef4444'
                            });
                            resetButton();
                        }
                    },
                    error: function(xhr, status, error) {
                        let errMsg = 'Ocurrió un error inesperado al procesar la solicitud.';
                        if (xhr.responseJSON && xhr.responseJSON.message) {
                            errMsg = xhr.responseJSON.message;
                        }
                        Swal.fire({
                            icon: 'error',
                            title: 'Error de Servidor',
                            text: errMsg,
                            background: '#1e293b',
                            color: '#f8fafc',
                            confirmButtonColor: '#ef4444'
                        });
                        resetButton();
                    }
                });
            });

            function resetButton() {
                $('#submitBtn').attr('disabled', false).removeClass('opacity-80');
                $('#btnIcon').removeClass('hidden');
                $('#btnSpinner').addClass('hidden');
            }
        });
    </script>
</body>
</html>
