<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($title ?? 'Emergency Response Coordination System', ENT_QUOTES, 'UTF-8') ?></title>
    <meta name="description" content="<?= htmlspecialchars($meta_description ?? 'Rapid emergency reporting and dispatch system.', ENT_QUOTES, 'UTF-8') ?>">
    
    <!-- Google Fonts: Inter for clean, highly legible UI -->
    <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&display=swap" rel="stylesheet">
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/4.0.0/jquery.min.js"></script>

    <!-- FontAwesome for standard icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

    <!-- Tailwind CSS CDN -->
    <script src="https://cdn.tailwindcss.com"></script>
    <script>

        tailwind.config = {
            theme: {
                extend: {
                    fontFamily: {
                        sans: ['Inter', 'sans-serif'],
                    },
                    colors: {
                        navy: {
                            800: '#1e3a8a',
                            900: '#1e3a8a', // Deep professional navy
                            950: '#172554',
                        },
                        brandBlue: {
                            500: '#3b82f6', // Bright, actionable blue
                            600: '#2563eb',
                        },
                        alert: '#ef4444', // Red for emergencies
                    }
                }
            }
        }
    </script>

    <!-- SweetAlert2 CSS & JS for elegant notifications -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <!-- Coreflux Head Injection -->
    <?= htmlspecialchars(Core\View::renderHead(), ENT_QUOTES, 'UTF-8') ?>

    <style>
        
        body {
            font-family: 'Inter', sans-serif;
            background-color: #f8fafc; 
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }
        main {
            flex: 1;
        }
        
        ::-webkit-scrollbar {
            width: 8px;
        }
        ::-webkit-scrollbar-track {
            background: #f1f1f1; 
        }
        ::-webkit-scrollbar-thumb {
            background: #cbd5e1; 
            border-radius: 4px;
        }
        ::-webkit-scrollbar-thumb:hover {
            background: #94a3b8; 
        }
    </style>
</head>
<body class="text-slate-800 antialiased">

    <!-- Navbar -->
    <nav class="bg-navy-950 text-white shadow-lg sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-16">
                <!-- Logo & Brand -->
                <div class="flex items-center">
                    <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/" class="flex-shrink-0 flex items-center gap-2 group">
                        <div class="w-10 h-10 bg-alert text-white rounded-lg flex items-center justify-center font-bold text-xl shadow-md group-hover:scale-105 transition-transform">
                            <i class="fas fa-shield-alt"></i>
                        </div>
                        <span class="font-bold text-xl tracking-tight hidden sm:block">ERCS</span>
                    </a>
                </div>

                <!-- Desktop Menu -->
                <div class="hidden md:flex items-center space-x-4">
                    <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/" class="px-3 py-2 rounded-md text-sm font-medium hover:bg-navy-800 transition-colors">Home</a>
                    <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/about" class="px-3 py-2 rounded-md text-sm font-medium hover:bg-navy-800 transition-colors">About</a>
                    <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/contact" class="px-3 py-2 rounded-md text-sm font-medium hover:bg-navy-800 transition-colors">Contact</a>
                    
                    <!-- Primary Call to Action -->
                    <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/emergency/report" class="ml-4 px-4 py-2 bg-alert hover:bg-red-600 text-white font-semibold rounded-md shadow-md transition-colors flex items-center gap-2 pulse-animation">
                        <i class="fas fa-exclamation-triangle"></i> Report Emergency
                    </a>

                    <!-- Auth Links / Dynamic State -->
                    <?php if (Core\Auth::check()): ?>
                        <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/dashboard" class="ml-2 px-3 py-2 text-sm font-medium text-brandBlue-500 hover:text-white transition-colors">
                            <i class="fas fa-user-circle mr-1"></i> Dashboard
                        </a>
                    <?php else: ?>
                        <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/login" class="ml-2 px-3 py-2 text-sm font-medium text-slate-300 hover:text-white transition-colors">
                            Login
                        </a>
                    <?php endif; ?>
                </div>

                <!-- Mobile menu button -->
                <div class="md:hidden flex items-center">
                    <button id="mobile-menu-btn" class="text-slate-300 hover:text-white focus:outline-none">
                        <i class="fas fa-bars text-2xl"></i>
                    </button>
                </div>
            </div>
        </div>

        <!-- Mobile Menu (Hidden by default) -->
        <div id="mobile-menu" class="hidden md:hidden bg-navy-900 border-t border-navy-800 pb-4 shadow-inner">
            <div class="px-2 pt-2 space-y-1">
                <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/" class="block px-3 py-2 rounded-md text-base font-medium hover:bg-navy-800">Home</a>
                <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/about" class="block px-3 py-2 rounded-md text-base font-medium hover:bg-navy-800">About</a>
                <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/contact" class="block px-3 py-2 rounded-md text-base font-medium hover:bg-navy-800">Contact</a>
                
                <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/emergency/report" class="block mt-2 px-3 py-3 bg-alert text-white rounded-md text-base font-bold text-center shadow">
                    <i class="fas fa-exclamation-triangle mr-2"></i> Report Emergency
                </a>
                
                <div class="border-t border-navy-800 mt-2 pt-2">
                    <?php if (Core\Auth::check()): ?>
                        <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/dashboard" class="block px-3 py-2 rounded-md text-base font-medium text-brandBlue-500 hover:bg-navy-800">Dashboard</a>
                        <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/logout" class="block px-3 py-2 rounded-md text-base font-medium text-slate-300 hover:bg-navy-800">Logout</a>
                    <?php else: ?>
                        <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/login" class="block px-3 py-2 rounded-md text-base font-medium text-slate-300 hover:bg-navy-800">Login</a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </nav>

    <!-- Main Content Area where individual views are injected -->
    <main>
        <!-- Path: app/Views/auth/login.view.php -->
<div class="bg-slate-50 min-h-[calc(100vh-64px)] flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
    <div class="max-w-md w-full bg-white rounded-xl shadow-lg border border-slate-200 overflow-hidden">
        
        <div class="bg-navy-950 p-6 text-center">
            <div class="w-16 h-16 bg-brandBlue-500 text-white rounded-full flex items-center justify-center text-3xl mx-auto shadow-md mb-4">
                <i class="fas fa-shield-alt"></i>
            </div>
            <h2 class="text-2xl font-bold text-white">Welcome Back</h2>
            <p class="text-slate-300 text-sm mt-1">Sign in to your ERCS account</p>
        </div>

        <div class="p-8">
            <form id="loginForm" class="space-y-6">
                <div>
                    <label for="email" class="block text-sm font-medium text-slate-700 mb-1">Email Address</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fas fa-envelope text-slate-400"></i>
                        </div>
                        <input type="email" name="email" id="email" required class="block w-full pl-10 pr-3 py-3 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-brandBlue-500 focus:border-brandBlue-500 transition-colors" placeholder="you@example.com">
                    </div>
                </div>

                <div>
                    <div class="flex items-center justify-between mb-1">
                        <label for="password" class="block text-sm font-medium text-slate-700">Password</label>
                        <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/forgot-password" class="text-sm font-medium text-brandBlue-600 hover:text-brandBlue-500">Forgot password?</a>
                    </div>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fas fa-lock text-slate-400"></i>
                        </div>
                        <input type="password" name="password" id="password" required class="block w-full pl-10 pr-3 py-3 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-brandBlue-500 focus:border-brandBlue-500 transition-colors" placeholder="••••••••">
                    </div>
                </div>

                <button type="submit" id="submitBtn" class="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-sm font-bold text-white bg-navy-900 hover:bg-navy-800 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-navy-900 transition-colors">
                    Sign In <i class="fas fa-sign-in-alt ml-2 mt-1"></i>
                </button>
            </form>

            <div class="mt-6 text-center text-sm text-slate-600">
                Don't have an account? 
                <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/register" class="font-medium text-brandBlue-600 hover:text-brandBlue-500">Create one now</a>
            </div>
        </div>
    </div>
</div>


    </main>

    <!-- Footer -->
    <footer class="bg-slate-900 text-slate-300 py-10 mt-auto border-t border-slate-800">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
                <div>
                    <h3 class="text-white text-lg font-bold mb-4 flex items-center gap-2">
                        <i class="fas fa-shield-alt text-alert"></i> ERCS
                    </h3>
                    <p class="text-sm text-slate-400">
                        Rapid emergency reporting and intelligent responder coordination. Saving lives through instant communication and real-time location tracking.
                    </p>
                </div>
                <div>
                    <h3 class="text-white text-lg font-bold mb-4">Quick Links</h3>
                    <ul class="space-y-2 text-sm">
                        <li><a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/emergency/report" class="hover:text-white transition-colors text-alert font-semibold">Report Emergency</a></li>
                        <li><a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/about" class="hover:text-white transition-colors">How it Works</a></li>
                        <li><a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/login" class="hover:text-white transition-colors">Responder Login</a></li>
                    </ul>
                </div>
                <div>
                    <h3 class="text-white text-lg font-bold mb-4">Emergency Contacts</h3>
                    <ul class="space-y-2 text-sm text-slate-400">
                        <li><i class="fas fa-phone-alt mr-2 text-brandBlue-500"></i> Toll-Free: 112</li>
                        <li><i class="fas fa-ambulance mr-2 text-brandBlue-500"></i> Medical: 911</li>
                        <li><i class="fas fa-fire mr-2 text-brandBlue-500"></i> Fire Service: 101</li>
                    </ul>
                </div>
            </div>
            <div class="border-t border-slate-800 mt-8 pt-8 text-sm text-center text-slate-500">
                &copy; <?= htmlspecialchars(date('Y'), ENT_QUOTES, 'UTF-8') ?> Emergency Response Coordination System. All rights reserved.
            </div>
        </div>
    </footer>

    <!-- jQuery for seamless AJAX requests (Chat, Location updates) -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

    <!-- Global Layout Scripts -->
    <script>
        $(document).ready(function() {

            $('#mobile-menu-btn').on('click', function() {
                $('#mobile-menu').slideToggle('fast');
            });

            $.ajaxSetup({
                error: function(jqXHR, textStatus, errorThrown) {
                    let errorMessage = "An unexpected error occurred.";
                    if (jqXHR.responseJSON && jqXHR.responseJSON.message) {
                        errorMessage = jqXHR.responseJSON.message;
                    }


                    if (this.global !== false) {
                        Swal.fire({
                            icon: 'error',
                            title: 'System Error',
                            text: errorMessage,
                            confirmButtonColor: '#1e3a8a'
                        });
                    }
                }
            });
        });

        function showNotification(type, title, message) {
            Swal.fire({
                icon: type, // 'success', 'error', 'warning', 'info'
                title: title,
                text: message,
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 4000,
                timerProgressBar: true,
                didOpen: (toast) => {
                    toast.addEventListener('mouseenter', Swal.stopTimer)
                    toast.addEventListener('mouseleave', Swal.resumeTimer)
                }
            });
        }
    </script>

    <!-- Coreflux Injected View Scripts (e.g. Google Maps, Chat scripts added via @addScript) -->
    <?= htmlspecialchars(Core\View::renderScripts(), ENT_QUOTES, 'UTF-8') ?>
<script>
$(document).ready(function() {
    $('#loginForm').on('submit', function(e) {
        e.preventDefault();
        
        let $btn = $('#submitBtn');
        let originalText = $btn.html();
        
        $btn.html('<i class="fas fa-spinner fa-spin"></i> Authenticating...').prop('disabled', true);
        
        $.ajax({
            url: '<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/login',
            type: 'POST',
            data: $(this).serialize(),
            success: function(response) {
                if(response.status === 'success') {
                    showNotification('success', 'Success', response.message);
                    setTimeout(function() {
                        window.location.href = '<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>' + response.redirect;
                    }, 1000);
                }
            },
            error: function(xhr) {
                let msg = 'Invalid credentials.';
                if(xhr.responseJSON && xhr.responseJSON.message) {
                    msg = xhr.responseJSON.message;
                } else if (xhr.responseJSON && xhr.responseJSON.errors) {
                    msg = Object.values(xhr.responseJSON.errors)[0][0]; 
                }
                showNotification('error', 'Login Failed', msg);
                $btn.html(originalText).prop('disabled', false);
            }
        });
    });
});
</script>
</body>
</html>