<!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/responder/dashboard.view.php -->
<div class="bg-slate-50 min-h-[calc(100vh-64px)] py-8">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        
        <!-- Header -->
        <div class="bg-navy-950 rounded-xl p-6 shadow-md mb-8 text-white flex flex-col md:flex-row justify-between items-center gap-4">
            <div>
                <h1 class="text-2xl font-bold flex items-center gap-2">
                    <i class="fas fa-satellite-dish text-brandBlue-400"></i> Dispatch Terminal
                </h1>
                <p class="text-slate-300 text-sm mt-1">
                    <?= htmlspecialchars($responder['organization_name'] ?? 'Responder Unit', ENT_QUOTES, 'UTF-8') ?> - Active Duty
                </p>
            </div>
            <div class="flex items-center gap-3 bg-navy-900 px-4 py-2 rounded-lg border border-navy-800">
                <span class="relative flex h-3 w-3">
                  <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
                  <span class="relative inline-flex rounded-full h-3 w-3 bg-green-500"></span>
                </span>
                <span class="text-sm font-semibold text-green-400" id="gps-status-indicator">GPS Active</span>
            </div>
        </div>

        <!-- Incidents List -->
        <div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
            <div class="p-6 border-b border-slate-200 bg-slate-50">
                <h3 class="font-bold text-navy-900 text-lg">Assigned Emergencies</h3>
            </div>
            
            <div class="overflow-x-auto">
                <table class="w-full text-left border-collapse">
                    <thead>
                        <tr class="bg-slate-50 text-slate-600 text-sm uppercase tracking-wider border-b border-slate-200">
                            <th class="p-4 font-semibold">ID</th>
                            <th class="p-4 font-semibold">Time</th>
                            <th class="p-4 font-semibold">Victim</th>
                            <th class="p-4 font-semibold">Status</th>
                            <th class="p-4 font-semibold text-right">Action</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100 text-sm">
                        <?php if (empty($incidents)): ?>
                            <tr>
                                <td colspan="5" class="p-10 text-center text-slate-500">
                                    <i class="fas fa-shield-alt text-4xl mb-3 text-slate-300 block"></i>
                                    No active dispatches at this time. Stay alert.
                                </td>
                            </tr>
                        <?php else: ?><?php foreach ($incidents as $incident): ?>
                                <tr class="hover:bg-slate-50 transition-colors">
                                    <td class="p-4 font-mono font-medium text-navy-900">

                                    </td>
                                    <td class="p-4 text-slate-600">
                                        <?= htmlspecialchars(date('H:i A', strtotime($incident['created_at'])), ENT_QUOTES, 'UTF-8') ?>
                                    </td>
                                    <td class="p-4">
                                        <div class="font-medium text-slate-900"><?= htmlspecialchars($incident['victim_name'], ENT_QUOTES, 'UTF-8') ?></div>
                                        <div class="text-xs text-slate-500"><?= htmlspecialchars($incident['victim_phone'], ENT_QUOTES, 'UTF-8') ?></div>
                                    </td>
                                    <td class="p-4">
                                        <?php if ($incident['status'] == 'dispatched'): ?>
                                            <span class="bg-red-100 text-red-800 text-xs px-2 py-1 rounded-full font-bold animate-pulse">ACTION REQUIRED</span>
                                        <?php elseif ($incident['status'] == 'accepted'): ?>
                                            <span class="bg-yellow-100 text-yellow-800 text-xs px-2 py-1 rounded-full font-semibold">Accepted - Awaiting Move</span>
                                        <?php elseif ($incident['status'] == 'en_route'): ?>
                                            <span class="bg-brandBlue-100 text-brandBlue-800 text-xs px-2 py-1 rounded-full font-semibold">En Route</span>
                                        <?php elseif ($incident['status'] == 'resolved'): ?>
                                            <span class="bg-green-100 text-green-800 text-xs px-2 py-1 rounded-full font-semibold">Resolved</span>
                                        <?php else: ?>
                                            <span class="bg-slate-100 text-slate-800 text-xs px-2 py-1 rounded-full font-semibold"><?= htmlspecialchars(ucfirst($incident['status']), ENT_QUOTES, 'UTF-8') ?></span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="p-4 text-right">
                                        <a href="<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/responder/incident/<?= htmlspecialchars($incident['id'], ENT_QUOTES, 'UTF-8') ?>" class="text-brandBlue-600 hover:text-white font-medium text-sm border border-brandBlue-600 hover:bg-brandBlue-600 px-4 py-2 rounded transition-colors inline-block">
                                            Open Dispatch
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?><?php endif; ?>
                    </tbody>
                </table>
            </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() {

    if (navigator.geolocation) {

        setInterval(function() {
            navigator.geolocation.getCurrentPosition(
                function(position) {
                    $.ajax({
                        url: '<?= htmlspecialchars(APP_URL, ENT_QUOTES, 'UTF-8') ?>/responder/api/location/update',
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify({
                            latitude: position.coords.latitude,
                            longitude: position.coords.longitude
                        }),
                        global: false, // Don't trigger global error alerts on background pings
                        success: function() {
                            $('#gps-status-indicator').text('GPS Active').removeClass('text-red-400').addClass('text-green-400');
                        },
                        error: function() {
                            $('#gps-status-indicator').text('Connection Lost').removeClass('text-green-400').addClass('text-red-400');
                        }
                    });
                },
                function(error) {
                    $('#gps-status-indicator').text('GPS Denied').removeClass('text-green-400').addClass('text-red-400');
                },
                { enableHighAccuracy: true }
            );
        }, 10000); // 10 seconds
    } else {
        $('#gps-status-indicator').text('GPS Unsupported').removeClass('text-green-400').addClass('text-red-400');
    }
});
</script>
</body>
</html>