<!-- Path: app/Views/user/dashboard.view.php -->
<div class="bg-slate-50 min-h-[calc(100vh-64px)] py-10">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-navy-900">My Dashboard</h1>
            <p class="text-slate-600 mt-1">Manage your account and view your emergency report history.</p>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
            
            <!-- Left Column: Profile Settings -->
            <div class="lg:col-span-1">
                <div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
                    <div class="bg-navy-950 p-4 border-b border-slate-200 flex items-center gap-3">
                        <div class="w-12 h-12 bg-brandBlue-500 rounded-full flex items-center justify-center text-white text-xl font-bold">
                            {{ substr($user['full_name'], 0, 1) }}
                        </div>
                        <div>
                            <h2 class="text-lg font-bold text-white">{{ $user['full_name'] }}</h2>
                            <p class="text-slate-300 text-sm">Standard User</p>
                        </div>
                    </div>
                    
                    <div class="p-6">
                        <h3 class="font-bold text-navy-900 mb-4 border-b pb-2">Profile Settings</h3>
                        <form id="profileForm" class="space-y-4">
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">Full Name</label>
                                <input type="text" name="full_name" value="{{ $user['full_name'] }}" required class="block w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-brandBlue-500 focus:border-brandBlue-500">
                            </div>
                            
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">Email Address (Cannot change)</label>
                                <input type="email" value="{{ $user['email'] }}" disabled class="block w-full px-3 py-2 border border-slate-300 rounded-md bg-slate-100 text-slate-500 cursor-not-allowed">
                            </div>

                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">Phone Number</label>
                                <input type="tel" name="phone_number" value="{{ $user['phone_number'] }}" required class="block w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-brandBlue-500 focus:border-brandBlue-500">
                            </div>

                            <div class="pt-2 border-t mt-4">
                                <label class="block text-sm font-medium text-slate-700 mb-1">New Password (Optional)</label>
                                <input type="password" name="password" placeholder="Leave blank to keep current" class="block w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-brandBlue-500 focus:border-brandBlue-500">
                            </div>

                            <button type="submit" id="updateBtn" class="w-full bg-brandBlue-600 hover:bg-brandBlue-700 text-white font-bold py-2 px-4 rounded-md shadow-sm transition-colors mt-4">
                                Update Profile
                            </button>
                        </form>
                    </div>
                </div>
            </div>

            <!-- Right Column: Emergency History -->
            <div class="lg:col-span-2">
                <div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
                    <div class="p-6 border-b border-slate-200 flex justify-between items-center">
                        <h3 class="font-bold text-navy-900 text-lg"><i class="fas fa-history mr-2 text-brandBlue-500"></i> My Emergency Reports</h3>
                        <a href="{{APP_URL}}/emergency/report" class="text-sm bg-alert hover:bg-red-600 text-white px-3 py-1.5 rounded-md font-medium transition-colors">
                            <i class="fas fa-plus mr-1"></i> New Report
                        </a>
                    </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">Incident ID</th>
                                    <th class="p-4 font-semibold">Date / Time</th>
                                    <th class="p-4 font-semibold">Category</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-8 text-center text-slate-500">
                                            <i class="fas fa-clipboard-list text-3xl mb-3 text-slate-300 block"></i>
                                            You have not reported any emergencies yet.
                                        </td>
                                    </tr>
                                @else

                                    <!-- <pre>
                                        {!! print_r($incidents) !!}
                                    </pre> -->
                                    @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('M d, Y - h:i A', strtotime($incident['created_at'])) }}
                                            </td>
                                            <td class="p-4 font-medium text-slate-800">
                                                {{ $incident['category_name'] ?? 'General' }}
                                            </td>
                                            <td class="p-4">
                                                @if($incident['status'] == 'pending')
                                                    <span class="bg-yellow-100 text-yellow-800 text-xs px-2 py-1 rounded-full font-semibold">Pending</span>
                                                @elseif(in_array($incident['status'], ['dispatched', 'verified', 'accepted', 'en_route']))
                                                    <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded-full font-semibold">Active</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}}/emergency/track/{{ $incident['id'] }}" class="text-brandBlue-600 hover:text-brandBlue-800 font-medium text-sm border border-brandBlue-200 hover:bg-brandBlue-50 px-3 py-1.5 rounded transition-colors">
                                                    View / Track
                                                </a>
                                            </td>
                                        </tr>
                                    @endforeach
                                @endif
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>

@addScript
<script>
$(document).ready(function() {
    $('#profileForm').on('submit', function(e) {
        e.preventDefault();
        
        let $btn = $('#updateBtn');
        let originalText = $btn.html();
        
        $btn.html('<i class="fas fa-spinner fa-spin"></i> Updating...').prop('disabled', true);
        
        $.ajax({
            url: '{{APP_URL}}/profile/update',
            type: 'POST',
            data: $(this).serialize(),
            success: function(response) {
                if(response.status === 'success') {
                    showNotification('success', 'Success', response.message);
                    // Clear password field after successful update
                    $('input[name="password"]').val('');
                }
            },
            error: function(xhr) {
                let msg = 'Failed to update profile.';
                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', 'Update Failed', msg);
            },
            complete: function() {
                $btn.html(originalText).prop('disabled', false);
            }
        });
    });
});
</script>
@endAddScript