<x-admin-layout>
    <div x-data="{ addEmployeeModalOpen: false }">
        <div class="mb-6 flex flex-col md:flex-row justify-between items-center gap-4">
            <div>
                <h2 class="text-2xl font-bold text-slate-800">الموظفين</h2>
                <p class="text-slate-500 text-sm">إدارة شؤون الموظفين والسجلات الوظيفية</p>
            </div>
            <button @click="addEmployeeModalOpen = true" class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-xl hover:bg-blue-700 transition-colors shadow-sm font-semibold">
                <x-icon name="Plus" class="h-4 w-4" />
                <span>إضافة موظف جديد</span>
            </button>
        </div>

        <div class="mb-6 rounded-2xl bg-white p-4 shadow-sm border border-slate-100">
            <form method="GET" action="{{ route('admin.hr.employees.index') }}" class="flex flex-col md:flex-row items-center gap-4">
                <div class="relative flex-1 w-full">
                    <x-icon name="Search" class="absolute right-3 top-1/2 -translate-y-1/2 h-5 w-5 text-gray-400" />
                    <input type="text" name="search" placeholder="ابحث باسم الموظف أو الرقم الوظيفي..." value="{{ request('search') }}" class="w-full rounded-xl border-gray-200 pr-10 focus:border-blue-500 focus:ring-blue-500">
                </div>
                <div class="w-full md:w-48">
                    <select name="department" class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500">
                        <option value="all">كل الأقسام</option>
                        @foreach($departments as $id => $name)
                            <option value="{{ $id }}" {{ request('department') == $id ? 'selected' : '' }}>{{ $name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="w-full md:w-48">
                    <select name="status" class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500">
                        <option value="all">كل الحالات</option>
                        <option value="ACTIVE" {{ request('status') == 'ACTIVE' ? 'selected' : '' }}>نشط</option>
                        <option value="ON_LEAVE" {{ request('status') == 'ON_LEAVE' ? 'selected' : '' }}>في إجازة</option>
                        <option value="TERMINATED" {{ request('status') == 'TERMINATED' ? 'selected' : '' }}>منتهي عقده</option>
                        <option value="SUSPENDED" {{ request('status') == 'SUSPENDED' ? 'selected' : '' }}>موقف</option>
                    </select>
                </div>
                <button type="submit" class="w-full md:w-auto inline-flex items-center justify-center rounded-xl bg-slate-800 px-6 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-slate-700 transition-all">
                    <x-icon name="Filter" class="ml-2 h-4 w-4" />
                    تصفية
                </button>
            </form>
        </div>

        <div class="overflow-hidden rounded-2xl bg-white shadow-sm border border-slate-100">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50/50">
                        <tr>
                            <th class="px-6 py-4 text-right text-xs font-bold text-gray-500 uppercase tracking-wider">الرقم الوظيفي</th>
                            <th class="px-6 py-4 text-right text-xs font-bold text-gray-500 uppercase tracking-wider">الاسم</th>
                            <th class="px-6 py-4 text-right text-xs font-bold text-gray-500 uppercase tracking-wider">القسم</th>
                            <th class="px-6 py-4 text-right text-xs font-bold text-gray-500 uppercase tracking-wider">المسمى الوظيفي</th>
                            <th class="px-6 py-4 text-right text-xs font-bold text-gray-500 uppercase tracking-wider">الحالة</th>
                            <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">إجراءات</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @forelse($employees as $emp)
                            <tr class="hover:bg-slate-50/50 transition-colors">
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-mono font-bold text-blue-600">
                                    {{ $emp->employee_number }}
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="flex items-center gap-3">
                                        <div class="h-10 w-10 rounded-full bg-slate-100 flex items-center justify-center text-slate-500 font-bold">
                                            {{ substr($emp->first_name, 0, 1) }}
                                        </div>
                                        <div>
                                            <div class="text-sm font-bold text-slate-900">{{ $emp->first_name }} {{ $emp->last_name }}</div>
                                            <div class="text-xs text-slate-500">{{ $emp->email }}</div>
                                        </div>
                                    </div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-600">
                                    {{ $emp->department->name ?? 'غير محدد' }}
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-600">
                                    {{ $emp->position->name ?? 'غير محدد' }}
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-3 py-1 inline-flex text-xs leading-5 font-bold rounded-full 
                                        {{ $emp->status === 'ACTIVE' ? 'bg-emerald-100 text-emerald-700' : 
                                           ($emp->status === 'ON_LEAVE' ? 'bg-blue-100 text-blue-700' : 
                                           ($emp->status === 'TERMINATED' ? 'bg-red-100 text-red-700' : 'bg-amber-100 text-amber-700')) }}">
                                        {{ $emp->status === 'ACTIVE' ? 'نشط' : 
                                           ($emp->status === 'ON_LEAVE' ? 'في إجازة' : 
                                           ($emp->status === 'TERMINATED' ? 'منتهي عقده' : 'موقف')) }}
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-left text-sm font-medium">
                                    <a href="{{ route('admin.hr.employees.show', $emp->id) }}" class="inline-flex items-center gap-2 px-3 py-1.5 bg-slate-100 text-slate-700 rounded-lg hover:bg-slate-200 transition-colors">
                                        <x-icon name="Eye" class="h-4 w-4" />
                                        <span>عرض</span>
                                    </a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="px-6 py-12 text-center text-slate-500">
                                    <div class="flex flex-col items-center justify-center">
                                        <div class="h-16 w-16 bg-slate-50 rounded-full flex items-center justify-center mb-4">
                                            <x-icon name="Users" class="h-8 w-8 text-slate-300" />
                                        </div>
                                        <p class="text-lg font-medium">لا يوجد موظفين</p>
                                        <p class="text-sm">لم يتم العثور على أي موظفين مطابقين للبحث.</p>
                                    </div>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="border-t border-gray-100 bg-gray-50/50 p-4">
                {{ $employees->withQueryString()->links() }}
            </div>
        </div>

        {{-- Add Employee Modal --}}
        <div x-show="addEmployeeModalOpen" class="fixed inset-0 z-50 overflow-y-auto" style="display: none;">
            <div class="fixed inset-0 bg-gray-900/50 backdrop-blur-sm transition-opacity" @click="addEmployeeModalOpen = false"></div>

            <div class="flex min-h-full items-center justify-center p-4">
                <div class="relative w-full max-w-2xl transform overflow-hidden rounded-2xl bg-white p-6 text-right shadow-xl transition-all">
                    <div class="flex items-center justify-between mb-6 border-b border-gray-100 pb-4">
                        <h3 class="text-xl font-bold text-slate-800">إضافة موظف جديد</h3>
                        <button @click="addEmployeeModalOpen = false" class="text-gray-400 hover:text-gray-500">
                            <x-icon name="X" class="h-6 w-6" />
                        </button>
                    </div>

                    <form method="POST" action="{{ route('admin.hr.employees.store') }}" class="space-y-6">
                        @csrf
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">الاسم الأول</label>
                                <input type="text" name="first_name" required class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">اسم العائلة</label>
                                <input type="text" name="last_name" required class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500">
                            </div>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">البريد الإلكتروني</label>
                                <input type="email" name="email" required class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">رقم الهاتف</label>
                                <input type="text" name="phone" class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500">
                            </div>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">القسم</label>
                                <select name="department_id" required class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500">
                                    <option value="">اختر القسم</option>
                                    @foreach($departments as $id => $name)
                                        <option value="{{ $id }}">{{ $name }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">تاريخ التعيين</label>
                                <input type="date" name="hire_date" required class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500">
                            </div>
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-1">الراتب الأساسي</label>
                            <div class="relative">
                                <input type="number" step="0.01" name="salary" class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500 pr-12">
                                <div class="absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none text-gray-500">
                                    ج.م
                                </div>
                            </div>
                        </div>

                        <div class="mt-6 flex justify-end gap-3 pt-4 border-t border-gray-100">
                            <button type="button" @click="addEmployeeModalOpen = false" class="px-4 py-2 bg-slate-100 text-slate-700 rounded-xl hover:bg-slate-200 font-medium transition-colors">
                                إلغاء
                            </button>
                            <button type="submit" class="px-6 py-2 bg-blue-600 text-white rounded-xl hover:bg-blue-700 font-semibold shadow-sm transition-colors">
                                حفظ البيانات
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-admin-layout>
