<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Employee Details Report') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900">
                    <div class="flex justify-between items-center mb-6">
                        <h3 class="text-lg font-bold">Employee Details</h3>
                        <div class="flex gap-2">
                            <a href="{{ route('reports.employees', request()->all() + ['export' => 'print']) }}" target="_blank" class="inline-flex items-center px-4 py-2 bg-gray-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-500 transition">Print</a>
                            <a href="{{ route('reports.employees', request()->all() + ['export' => 'pdf']) }}" class="inline-flex items-center px-4 py-2 bg-red-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-red-500 transition">PDF</a>
                            <a href="{{ route('reports.employees', request()->all() + ['export' => 'excel']) }}" class="inline-flex items-center px-4 py-2 bg-green-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-green-500 transition">Excel</a>
                        </div>
                    </div>
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Name</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Code</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Department</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Position</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Iqama</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Iqama Expiry</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Passport</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Manager</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Status</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            @foreach($employees as $employee)
                                @php
                                    $expiry = $employee->iqama_expiry_date;
                                    $statusColor = 'bg-gray-100 text-gray-800';
                                    $statusText = 'Far';
                                    if ($expiry) {
                                        if ($expiry->isPast()) {
                                            $statusColor = 'bg-red-100 text-red-800'; $statusText = 'Expired';
                                        } elseif ($expiry->diffInDays(now()) <= 30) {
                                            $statusColor = 'bg-yellow-100 text-yellow-800'; $statusText = 'Near';
                                        } else {
                                            $statusColor = 'bg-green-100 text-green-800'; $statusText = 'Far';
                                        }
                                    }
                                @endphp
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ $employee->first_name }} {{ $employee->last_name }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ $employee->employee_code }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ optional($employee->department)->name }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ optional($employee->designation)->name }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ $employee->iqama_number }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {{ $statusColor }}">
                                            {{ $expiry ? $expiry->format('Y-m-d') : 'N/A' }} ({{ $statusText }})
                                        </span>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ $employee->passport_number }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ $employee->manager ? ($employee->manager->first_name . ' ' . $employee->manager->last_name) : '' }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full 
                                            {{ $employee->status === 'active' ? 'bg-green-100 text-green-800' : 
                                               ($employee->status === 'inactive' ? 'bg-gray-100 text-gray-800' : 
                                               ($employee->status === 'on_leave' ? 'bg-blue-100 text-blue-800' : 'bg-red-100 text-red-800')) }}">
                                            {{ ucfirst($employee->status) }}
                                        </span>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                    <div class="mt-4">
                        {{ $employees->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
