<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">ID</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">생성일</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">회사명</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">코드</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">상태</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">유형</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">연락처</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">사용자</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">부서</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">메뉴</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">역할</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider">사용량</th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider" colspan="2">관리</th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            @forelse($tenants as $tenant)
            <tr class="{{ $tenant->deleted_at ? 'bg-gray-100' : '' }} hover:bg-gray-50 cursor-pointer"
                onclick="TenantModal.open({{ $tenant->id }})"
                data-tenant-id="{{ $tenant->id }}">
                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                    {{ $tenant->id }}
                </td>
                <td class="px-3 py-2 whitespace-nowrap text-sm text-gray-500">
                    {{ $tenant->created_at?->format('ymd') ?? '-' }}
                </td>
                <td class="px-3 py-2 whitespace-nowrap">
                    <div class="text-sm font-medium text-gray-900 cursor-pointer hover:text-blue-600"
                         data-context-menu="tenant"
                         data-entity-id="{{ $tenant->id }}"
                         data-entity-name="{{ $tenant->company_name }}">
                        {{ $tenant->company_name }}
                    </div>
                    @if($tenant->ceo_name)
                    <div class="text-sm text-gray-500">대표: {{ $tenant->ceo_name }}</div>
                    @endif
                </td>
                <td class="px-3 py-2 whitespace-nowrap text-sm text-gray-900">
                    {{ $tenant->code }}
                </td>
                <td class="px-3 py-2 whitespace-nowrap">
                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full
                        {{ $tenant->status_badge_color === 'success' ? 'bg-green-100 text-green-800' : '' }}
                        {{ $tenant->status_badge_color === 'warning' ? 'bg-yellow-100 text-yellow-800' : '' }}
                        {{ $tenant->status_badge_color === 'error' ? 'bg-red-100 text-red-800' : '' }}">
                        {{ $tenant->status_label }}
                    </span>
                </td>
                <td class="px-3 py-2 whitespace-nowrap text-center">
                    @php
                        $typeLabels = ['STD' => '일반', 'TPL' => '템플릿', 'HQ' => '본사'];
                        $typeColors = [
                            'STD' => 'bg-gray-100 text-gray-800',
                            'TPL' => 'bg-purple-100 text-purple-800',
                            'HQ' => 'bg-blue-100 text-blue-800'
                        ];
                        $type = $tenant->tenant_type ?? 'STD';
                    @endphp
                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {{ $typeColors[$type] ?? 'bg-gray-100 text-gray-800' }}">
                        {{ $typeLabels[$type] ?? $type }}
                    </span>
                </td>
                <td class="px-3 py-2 whitespace-nowrap">
                    <div class="text-sm text-gray-900">{{ $tenant->email ?? '-' }}</div>
                    @if($tenant->phone)
                    <div class="text-xs text-gray-500">{{ $tenant->phone_formatted }}</div>
                    @endif
                </td>
                <td class="px-3 py-2 whitespace-nowrap text-sm text-center text-gray-900">
                    {{ $tenant->users_count ?? 0 }}
                </td>
                <td class="px-3 py-2 whitespace-nowrap text-sm text-center text-gray-900">
                    {{ $tenant->departments_count ?? 0 }}
                </td>
                <td class="px-3 py-2 whitespace-nowrap text-sm text-center text-gray-900">
                    {{ $tenant->menus_count ?? 0 }}
                </td>
                <td class="px-3 py-2 whitespace-nowrap text-sm text-center text-gray-900">
                    {{ $tenant->roles_count ?? 0 }}
                </td>
                <td class="px-3 py-2 whitespace-nowrap">
                    <div class="flex flex-col items-center">
                        <div class="w-24 bg-gray-200 rounded-full h-2 mb-1">
                            <div class="h-2 rounded-full
                                {{ $tenant->storage_badge_color === 'error' ? 'bg-red-500' : '' }}
                                {{ $tenant->storage_badge_color === 'warning' ? 'bg-yellow-500' : '' }}
                                {{ $tenant->storage_badge_color === 'success' ? 'bg-green-500' : '' }}"
                                 style="width: {{ min($tenant->storage_usage_percent, 100) }}%"></div>
                        </div>
                        <span class="text-xs text-gray-500">{{ $tenant->storage_used_formatted }}</span>
                    </div>
                </td>
                @if($tenant->deleted_at)
                {{-- 삭제된 항목 --}}
                <td class="px-2 py-2 whitespace-nowrap text-center text-sm font-medium" onclick="event.stopPropagation()">
                    <button onclick="confirmRestore({{ $tenant->id }}, '{{ $tenant->company_name }}')"
                            class="text-green-600 hover:text-green-900">
                        복원
                    </button>
                </td>
                <td class="px-2 py-2 whitespace-nowrap text-center text-sm font-medium" onclick="event.stopPropagation()">
                    @if(auth()->user()?->is_super_admin)
                    <button onclick="confirmForceDelete({{ $tenant->id }}, '{{ $tenant->company_name }}')"
                            class="text-red-600 hover:text-red-900">
                        영구삭제
                    </button>
                    @else
                    <span class="text-gray-400">-</span>
                    @endif
                </td>
                @else
                {{-- 활성 항목 --}}
                <td class="px-2 py-2 whitespace-nowrap text-center text-sm font-medium" onclick="event.stopPropagation()">
                    <a href="{{ route('tenants.edit', $tenant->id) }}"
                       class="text-blue-600 hover:text-blue-900">
                        수정
                    </a>
                </td>
                <td class="px-2 py-2 whitespace-nowrap text-center text-sm font-medium" onclick="event.stopPropagation()">
                    <button onclick="confirmDelete({{ $tenant->id }}, '{{ $tenant->company_name }}')"
                            class="text-red-600 hover:text-red-900">
                        삭제
                    </button>
                </td>
                @endif
            </tr>
            @empty
            <tr>
                <td colspan="14" class="px-6 py-12 text-center text-gray-500">
                    등록된 테넌트가 없습니다.
                </td>
            </tr>
            @endforelse
        </tbody>
    </table>
    </x-table-swipe>
</div>

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