@php
    $tenantId = session('selected_tenant_id');
    $isAllTenants = !$tenantId || $tenantId === 'all';
@endphp

<div class="bg-white rounded-lg shadow-sm overflow-hidden">
    <x-table-swipe>
    <table class="min-w-full divide-y divide-gray-200">
        <thead class="bg-gray-50">
            <tr>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap">ID</th>
                @if($isAllTenants)
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap">테넌트</th>
                @endif
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap">역할 이름</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap">Guard</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap">설명</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap">권한 수</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap">생성일</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap">액션</th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            @forelse($roles as $role)
            <tr>
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                    {{ $role->id }}
                </td>
                @if($isAllTenants)
                <td class="px-6 py-4 whitespace-nowrap">
                    @if($role->tenant)
                        <span class="px-2 py-1 text-xs font-medium bg-gray-100 text-gray-600 rounded cursor-pointer hover:bg-gray-200"
                              data-context-menu="tenant"
                              data-entity-id="{{ $role->tenant->id }}"
                              data-entity-name="{{ $role->tenant->company_name }}"
                              title="클릭하여 메뉴 열기">
                            {{ $role->tenant->company_name }}
                        </span>
                    @else
                        <span class="px-2 py-1 text-xs font-medium bg-gray-100 text-gray-400 rounded">미지정</span>
                    @endif
                </td>
                @endif
                <td class="px-6 py-4 whitespace-nowrap">
                    <div class="text-sm font-medium text-gray-900">{{ $role->name }}</div>
                </td>
                <td class="px-6 py-4 whitespace-nowrap text-center">
                    <span class="px-2 py-1 text-xs font-medium rounded {{ $role->guard_name === 'api' ? 'bg-blue-100 text-blue-700' : 'bg-green-100 text-green-700' }}">
                        {{ strtoupper($role->guard_name ?? '-') }}
                    </span>
                </td>
                <td class="px-6 py-4">
                    <div class="text-sm text-gray-500">{{ $role->description ?? '-' }}</div>
                </td>
                <td class="px-6 py-4 whitespace-nowrap text-sm text-center text-gray-900">
                    {{ $role->permissions_count ?? 0 }}
                </td>
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {{ $role->created_at?->format('Y-m-d') ?? '-' }}
                </td>
                <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                    <a href="{{ route('roles.edit', $role->id) }}"
                       class="text-blue-600 hover:text-blue-900 mr-3">
                        수정
                    </a>
                    <button onclick="confirmDelete({{ $role->id }}, '{{ $role->name }}')"
                            class="text-red-600 hover:text-red-900">
                        삭제
                    </button>
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="{{ $isAllTenants ? 8 : 7 }}" class="px-6 py-12 text-center text-gray-500">
                    등록된 역할이 없습니다.
                </td>
            </tr>
            @endforelse
        </tbody>
    </table>
    </x-table-swipe>
</div>

<!-- 페이지네이션 -->
@include('partials.pagination', [
    'paginator' => $roles,
    'target' => '#role-table',
    'includeForm' => '#filterForm'
])