<div class="bg-white rounded-lg shadow-sm overflow-hidden">
    <x-table-swipe>
    <table class="min-w-full">
        <thead class="bg-gray-50 border-b">
            <tr>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-700 uppercase tracking-wider">ID</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-700 uppercase tracking-wider">이름</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-700 uppercase tracking-wider">테넌트</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-700 uppercase tracking-wider">이메일</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-700 uppercase tracking-wider">부서</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-700 uppercase tracking-wider">역할</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-700 uppercase tracking-wider">상태</th>
                <th class="px-6 py-3 text-left text-sm font-semibold text-gray-700 uppercase tracking-wider">작업</th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            @forelse($users as $user)
                <tr class="{{ $user->deleted_at ? 'bg-gray-100' : '' }} hover:bg-gray-50 cursor-pointer"
                    onclick="UserModal.open({{ $user->id }})"
                    data-user-id="{{ $user->id }}">
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                        {{ $user->user_id ?? '-' }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="text-sm font-medium text-gray-900">
                            {{ $user->name }}
                        </div>
                        @if($user->is_super_admin && auth()->user()?->is_super_admin)
                            <span class="text-xs text-red-600 font-semibold">슈퍼 관리자</span>
                        @endif
                    </td>
                    <td class="px-6 py-4 text-sm text-gray-500">
                        @if($user->tenants && $user->tenants->count() > 0)
                            <div class="flex flex-wrap gap-1">
                                @foreach($user->tenants as $tenant)
                                    <span class="px-1.5 py-0.5 text-xs rounded cursor-pointer hover:ring-2 hover:ring-indigo-300 {{ $tenant->pivot->is_default ? 'bg-indigo-100 text-indigo-700' : 'bg-gray-100 text-gray-600' }}"
                                          data-context-menu="tenant"
                                          data-entity-id="{{ $tenant->id }}"
                                          data-entity-name="{{ $tenant->company_name }}"
                                          title="클릭하여 메뉴 열기">
                                        {{ $tenant->company_name }}
                                    </span>
                                @endforeach
                            </div>
                        @else
                            <span class="text-gray-400">-</span>
                        @endif
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        {{ $user->email }}
                    </td>
                    <td class="px-6 py-4 text-sm text-gray-500">
                        @if($user->departmentUsers && $user->departmentUsers->count() > 0)
                            <div class="flex flex-wrap gap-1">
                                @foreach($user->departmentUsers as $du)
                                    <span class="px-1.5 py-0.5 text-xs rounded {{ $du->is_primary ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }}">
                                        {{ $du->department?->name ?? '-' }}
                                    </span>
                                @endforeach
                            </div>
                        @else
                            <span class="text-gray-400">-</span>
                        @endif
                    </td>
                    <td class="px-6 py-4 text-sm text-gray-500">
                        @if($user->userRoles && $user->userRoles->count() > 0)
                            <div class="flex flex-wrap gap-1">
                                @foreach($user->userRoles as $ur)
                                    <div class="px-2 py-1 rounded {{ $ur->role?->guard_name === 'web' ? 'bg-blue-50 border border-blue-200' : 'bg-purple-50 border border-purple-200' }}">
                                        <div class="text-xs font-medium {{ $ur->role?->guard_name === 'web' ? 'text-blue-700' : 'text-purple-700' }}">
                                            {{ $ur->role?->name ?? '-' }}
                                        </div>
                                        @if($ur->role?->description)
                                            <div class="text-[10px] text-gray-500 leading-tight">{{ $ur->role->description }}</div>
                                        @endif
                                    </div>
                                @endforeach
                            </div>
                        @else
                            <span class="text-gray-400">-</span>
                        @endif
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        @if($user->is_active)
                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                활성
                            </span>
                        @else
                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">
                                비활성
                            </span>
                        @endif
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium" onclick="event.stopPropagation()">
                        @php
                            // 슈퍼관리자 보호: 일반관리자가 슈퍼관리자를 수정/삭제할 수 없음
                            $canModify = ! $user->is_super_admin || auth()->user()?->is_super_admin;
                        @endphp
                        @if($user->deleted_at)
                            <!-- 삭제된 항목 - 복원은 일반관리자도 가능, 영구삭제는 슈퍼관리자만 -->
                            @if($canModify)
                            <button onclick="confirmRestore({{ $user->id }}, @js($user->name))"
                                    class="text-green-600 hover:text-green-900 mr-3">
                                복원
                            </button>
                            @endif
                            @if(auth()->user()?->is_super_admin)
                            <button onclick="confirmForceDelete({{ $user->id }}, @js($user->name))"
                                    class="text-red-600 hover:text-red-900">
                                영구삭제
                            </button>
                            @endif
                            @if(!$canModify && !auth()->user()?->is_super_admin)
                            <span class="text-gray-400 text-xs">삭제됨</span>
                            @endif
                        @elseif($canModify)
                            <!-- 활성 항목 (수정 가능한 경우만) -->
                            <button onclick="openDevSite({{ $user->id }}, @js($user->name))"
                                    class="text-emerald-600 hover:text-emerald-900 mr-3"
                                    title="DEV 사이트에 이 사용자로 로그인">
                                DEV 접속
                            </button>
                            <a href="{{ route('users.edit', $user->id) }}"
                               onclick="event.stopPropagation()"
                               class="text-blue-600 hover:text-blue-900 mr-3">
                                수정
                            </a>
                            <button onclick="confirmDelete({{ $user->id }}, @js($user->name))" class="text-red-600 hover:text-red-900">
                                삭제
                            </button>
                        @else
                            <!-- 슈퍼관리자 - 일반관리자는 수정/삭제 불가 -->
                            <span class="text-gray-400 text-xs">수정 불가</span>
                        @endif
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="8" class="px-6 py-4 text-center text-gray-500">
                        사용자가 없습니다.
                    </td>
                </tr>
            @endforelse
        </tbody>
    </table>
    </x-table-swipe>
</div>

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