{{-- 근태현황 테이블 (조회 전용, 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-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-4 py-3 text-center text-sm font-semibold text-gray-600">GPS</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-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>

                {{-- GPS --}}
                <td class="px-4 py-4 whitespace-nowrap text-center">
                    @php $gpsData = $attendance->json_details['gps_data'] ?? null; @endphp
                    @if($gpsData)
                    <button type="button" onclick='openGpsModal(@json($gpsData))'
                            class="text-emerald-600 hover:text-emerald-800" title="GPS 정보 보기">
                        <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="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/>
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/>
                        </svg>
                    </button>
                    @else
                    <span class="text-gray-300">
                        <svg class="w-5 h-5 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/>
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/>
                        </svg>
                    </span>
                    @endif
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="8" 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
