<!-- 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">
                    {{ $responder['organization_name'] ?? 'Responder Unit' }} - 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">
                        @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>
                        @else
                            @foreach($incidents as $incident)
                                <tr class="hover:bg-slate-50 transition-colors">
                                    <td class="p-4 font-mono font-medium text-navy-900">
                                        #{{ str_pad($incident['id'], 5, '0', STR_PAD_LEFT) }}
                                    </td>
                                    <td class="p-4 text-slate-600">
                                        {{ date('H:i A', strtotime($incident['created_at'])) }}
                                    </td>
                                    <td class="p-4">
                                        <div class="font-medium text-slate-900">{{ $incident['victim_name'] }}</div>
                                        <div class="text-xs text-slate-500">{{ $incident['victim_phone'] }}</div>
                                    </td>
                                    <td class="p-4">
                                        @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>
                                        @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>
                                        @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>
                                        @elseif($incident['status'] == 'resolved')
                                            <span class="bg-green-100 text-green-800 text-xs px-2 py-1 rounded-full font-semibold">Resolved</span>
                                        @else
                                            <span class="bg-slate-100 text-slate-800 text-xs px-2 py-1 rounded-full font-semibold">{{ ucfirst($incident['status']) }}</span>
                                        @endif
                                    </td>
                                    <td class="p-4 text-right">
                                        <a href="{{APP_URL}}/responder/incident/{{ $incident['id'] }}" 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>
                            @endforeach
                        @endif
                    </tbody>
                </table>
            </div>
        </div>

    </div>
</div>

@addScript
<script>
$(document).ready(function() {
    // Continuous GPS Tracking for Responders
    if (navigator.geolocation) {
        // Send coordinates every 10 seconds while on the dashboard
        setInterval(function() {
            navigator.geolocation.getCurrentPosition(
                function(position) {
                    $.ajax({
                        url: '{{APP_URL}}/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>
@endAddScript