{{-- 근태관리 테이블 (CRUD 포함, HTMX로 로드) --}}
@php
    use App\Models\HR\Attendance;
@endphp

<x-table-swipe>
    <table class="min-w-full">
        <thead class="bg-gray-50 border-b border-gray-200">
            <tr>
                <th class="px-4 py-3 text-center" style="width: 40px;">
                    <input type="checkbox" class="att-checkbox-all rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                </th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-600">날짜</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-600">사원</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-600">부서</th>
                <th class="px-6 py-3 text-center text-sm font-semibold text-gray-600">상태</th>
                <th class="px-6 py-3 text-center text-sm font-semibold text-gray-600">출근</th>
                <th class="px-6 py-3 text-center text-sm font-semibold text-gray-600">퇴근</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-600">비고</th>
                <th class="px-6 py-3 text-center text-sm font-semibold text-gray-600">작업</th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-100">
            @forelse($attendances as $attendance)
            @php
                $profile = $attendance->user?->tenantProfiles?->first();
                $department = $profile?->department;
                $displayName = $profile?->display_name ?? $attendance->user?->name ?? '-';
                $color = Attendance::STATUS_COLORS[$attendance->status] ?? 'gray';
                $label = Attendance::STATUS_MAP[$attendance->status] ?? $attendance->status;
                $checkIn = $attendance->check_in ? substr($attendance->check_in, 0, 5) : '-';
                $checkOut = $attendance->check_out ? substr($attendance->check_out, 0, 5) : '-';
            @endphp
            <tr class="hover:bg-gray-50 transition-colors">
                {{-- 체크박스 --}}
                <td class="px-4 py-4 text-center">
                    <input type="checkbox" class="att-checkbox rounded border-gray-300 text-blue-600 focus:ring-blue-500"
                           data-id="{{ $attendance->id }}">
                </td>

                {{-- 날짜 --}}
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                    {{ $attendance->base_date->format('m-d') }}
                    <span class="text-xs text-gray-400 ml-1">{{ ['일','월','화','수','목','금','토'][$attendance->base_date->dayOfWeek] }}</span>
                </td>

                {{-- 사원 --}}
                <td class="px-6 py-4 whitespace-nowrap">
                    <div class="flex items-center gap-2">
                        <div class="shrink-0 w-8 h-8 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center text-xs font-medium">
                            {{ mb_substr($displayName, 0, 1) }}
                        </div>
                        <span class="text-sm font-medium text-gray-900">{{ $displayName }}</span>
                    </div>
                </td>

                {{-- 부서 --}}
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                    {{ $department?->name ?? '-' }}
                </td>

                {{-- 상태 --}}
                <td class="px-6 py-4 whitespace-nowrap text-center">
                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-{{ $color }}-100 text-{{ $color }}-700">
                        {{ $label }}
                    </span>
                </td>

                {{-- 출근 --}}
                <td class="px-6 py-4 whitespace-nowrap text-center text-sm text-gray-500">
                    {{ $checkIn }}
                </td>

                {{-- 퇴근 --}}
                <td class="px-6 py-4 whitespace-nowrap text-center text-sm text-gray-500">
                    {{ $checkOut }}
                </td>

                {{-- 비고 --}}
                <td class="px-6 py-4 text-sm text-gray-500" style="max-width: 200px;">
                    <span class="truncate block">{{ $attendance->remarks ?? '' }}</span>
                </td>

                {{-- 작업 --}}
                <td class="px-6 py-4 whitespace-nowrap text-center">
                    <div class="flex items-center justify-center gap-2">
                        {{-- 수정 --}}
                        <button type="button"
                                onclick="openEditAttendanceModal({{ $attendance->id }}, {{ $attendance->user_id }}, '{{ $attendance->base_date->toDateString() }}', '{{ $attendance->status }}', '{{ $attendance->check_in ? substr($attendance->check_in, 0, 5) : '' }}', '{{ $attendance->check_out ? substr($attendance->check_out, 0, 5) : '' }}', '{{ addslashes($attendance->remarks ?? '') }}')"
                                class="text-blue-600 hover:text-blue-800" title="수정">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                            </svg>
                        </button>

                        {{-- 삭제 --}}
                        <button type="button"
                                hx-delete="{{ route('api.admin.hr.attendances.destroy', $attendance->id) }}"
                                hx-vals='{"view": "manage"}'
                                hx-headers='{"X-CSRF-TOKEN": "{{ csrf_token() }}"}'
                                hx-target="#manage-table"
                                hx-swap="innerHTML"
                                hx-confirm="{{ $displayName }}님의 {{ $attendance->base_date->format('m/d') }} 근태를 삭제하시겠습니까?"
                                class="text-red-600 hover:text-red-800" title="삭제">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                            </svg>
                        </button>
                    </div>
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="9" class="px-6 py-12 text-center">
                    <div class="flex flex-col items-center gap-2">
                        <svg class="w-12 h-12 text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <p class="text-gray-500">근태 기록이 없습니다.</p>
                    </div>
                </td>
            </tr>
            @endforelse
        </tbody>
    </table>
</x-table-swipe>

{{-- 페이지네이션 --}}
@if($attendances->hasPages())
<div class="px-6 py-4 border-t border-gray-200 bg-gray-50">
    {{ $attendances->links() }}
</div>
@endif
