<!-- 카테고리 테이블 -->
<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-4 py-3 text-center text-xs font-semibold text-gray-600 uppercase tracking-wider w-16">순서</th>
            <th class="px-4 py-3 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">코드</th>
            <th class="px-4 py-3 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">카테고리명</th>
            <th class="px-4 py-3 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">설명</th>
            <th class="px-4 py-3 text-center text-xs font-semibold text-gray-600 uppercase tracking-wider">수식 수</th>
            <th class="px-4 py-3 text-center text-xs font-semibold text-gray-600 uppercase tracking-wider">상태</th>
            <th class="px-4 py-3 text-center text-xs font-semibold text-gray-600 uppercase tracking-wider">액션</th>
        </tr>
    </thead>
    <tbody class="bg-white divide-y divide-gray-200">
        @forelse($categories as $category)
        <tr class="{{ $category->trashed() ? 'bg-red-50' : '' }} hover:bg-gray-50">
            <td class="px-4 py-3 whitespace-nowrap text-center">
                <span class="inline-flex items-center justify-center w-8 h-8 rounded-full bg-gray-100 text-sm font-medium text-gray-600">
                    {{ $category->sort_order }}
                </span>
            </td>
            <td class="px-4 py-3 whitespace-nowrap">
                <code class="px-2 py-1 bg-indigo-50 rounded text-sm font-mono text-indigo-600">{{ $category->code }}</code>
            </td>
            <td class="px-4 py-3 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900">{{ $category->name }}</div>
            </td>
            <td class="px-4 py-3">
                @if($category->description)
                <div class="text-sm text-gray-500 truncate max-w-xs">{{ $category->description }}</div>
                @else
                <span class="text-gray-400 text-sm">-</span>
                @endif
            </td>
            <td class="px-4 py-3 whitespace-nowrap text-center">
                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                    {{ $category->formulas_count ?? 0 }}개
                </span>
            </td>
            <td class="px-4 py-3 whitespace-nowrap text-center">
                @if($category->trashed())
                    <span class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-red-100 text-red-800">
                        삭제됨
                    </span>
                @elseif($category->is_active)
                    <button onclick="toggleActive({{ $category->id }})"
                            class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-green-100 text-green-800 hover:bg-green-200 cursor-pointer">
                        활성
                    </button>
                @else
                    <button onclick="toggleActive({{ $category->id }})"
                            class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-gray-100 text-gray-800 hover:bg-gray-200 cursor-pointer">
                        비활성
                    </button>
                @endif
            </td>
            <td class="px-4 py-3 whitespace-nowrap text-center">
                <div class="flex items-center justify-center gap-1">
                    @if($category->trashed())
                        <button onclick="confirmRestore({{ $category->id }}, '{{ $category->name }}')"
                                class="p-1.5 text-green-600 hover:bg-green-50 rounded" title="복원">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                            </svg>
                        </button>
                        @if(auth()->user()?->is_super_admin)
                        <button onclick="confirmForceDelete({{ $category->id }}, '{{ $category->name }}')"
                                class="p-1.5 text-red-600 hover:bg-red-50 rounded" title="영구삭제">
                            <svg class="w-4 h-4" 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>
                        @endif
                    @else
                        <a href="{{ route('quote-formulas.categories.edit', $category->id) }}"
                           class="p-1.5 text-indigo-600 hover:bg-indigo-50 rounded" title="수정">
                            <svg class="w-4 h-4" 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>
                        </a>
                        <button onclick="confirmDelete({{ $category->id }}, '{{ $category->name }}')"
                                class="p-1.5 text-red-600 hover:bg-red-50 rounded" title="삭제">
                            <svg class="w-4 h-4" 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>
                    @endif
                </div>
            </td>
        </tr>
        @empty
        <tr>
            <td colspan="7" class="px-6 py-12 text-center text-gray-500">
                <svg class="w-12 h-12 mx-auto text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z" />
                </svg>
                <p class="text-sm font-medium">등록된 카테고리가 없습니다.</p>
                <p class="text-xs mt-1">새 카테고리를 추가해주세요.</p>
            </td>
        </tr>
        @endforelse
    </tbody>
</table>
    </x-table-swipe>
</div>

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