<div class="overflow-x-auto">
    <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">순번</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">URL</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>
                <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">
            @php
                $permissionTypes = ['view', 'create', 'update', 'delete', 'approve', 'export', 'manage'];
            @endphp
            @forelse($menus as $index => $menu)
            <tr class="menu-row"
                data-menu-id="{{ $menu->id }}"
                data-parent-id="{{ $menu->parent_id ?? '' }}"
                data-depth="{{ $menu->depth ?? 0 }}">
                <td class="px-3 py-2 whitespace-nowrap text-sm text-gray-900 text-center">
                    {{ $index + 1 }}
                </td>
                <td class="px-3 py-2 whitespace-nowrap">
                    <div class="flex items-center gap-1.5" style="padding-left: {{ (($menu->depth ?? 0) * 1.25) }}rem;">
                        {{-- 트리 구조 표시 --}}
                        @if(($menu->depth ?? 0) > 0)
                            <span class="text-gray-300 text-xs font-mono flex-shrink-0">└─</span>
                        @endif

                        {{-- 폴더/아이템 아이콘 (폴더는 클릭으로 접기/펼치기) --}}
                        @if($menu->has_children)
                            <button type="button"
                                    onclick="toggleChildren({{ $menu->id }})"
                                    class="toggle-btn flex items-center text-blue-500 hover:text-blue-700 focus:outline-none"
                                    data-menu-id="{{ $menu->id }}">
                                <svg class="w-4 h-4 transform transition-transform flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z" />
                                </svg>
                                <svg class="w-3 h-3 ml-0.5 transform transition-transform chevron-icon" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"/>
                                </svg>
                            </button>
                        @else
                            <svg class="w-4 h-4 text-gray-400 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                            </svg>
                        @endif

                        {{-- 메뉴명 --}}
                        <span class="text-sm {{ ($menu->depth ?? 0) === 0 ? 'font-semibold text-gray-900' : 'font-medium text-gray-700' }}">
                            {{ $menu->name }}
                        </span>
                    </div>
                </td>
                <td class="px-3 py-2 whitespace-nowrap text-sm text-gray-500">
                    <span class="truncate max-w-xs inline-block" title="{{ $menu->url }}">
                        {{ $menu->url }}
                    </span>
                </td>
                <td class="px-3 py-2 whitespace-nowrap text-sm text-gray-900 text-center">
                    {{ $menu->sort_order }}
                </td>
                @foreach($permissionTypes as $type)
                <td class="px-3 py-2 whitespace-nowrap text-center">
                    <input
                        type="checkbox"
                        {{ isset($permissions[$menu->id][$type]) && $permissions[$menu->id][$type] ? 'checked' : '' }}
                        class="h-5 w-5 rounded border-gray-300 text-primary focus:ring-primary cursor-pointer"
                        hx-post="/api/admin/department-permissions/toggle"
                        hx-swap="none"
                        hx-include="[name='department_id'],[name='guard_name']"
                        hx-vals='{"menu_id": {{ $menu->id }}, "permission_type": "{{ $type }}"}'
                    >
                </td>
                @endforeach
            </tr>
            @empty
            <tr>
                <td colspan="11" class="px-6 py-12 text-center text-gray-500">
                    활성화된 메뉴가 없습니다.
                </td>
            </tr>
            @endforelse
        </tbody>
    </table>
</div>
