<div class="bg-white rounded-lg shadow-sm overflow-hidden">
    <table class="w-full">
        <thead class="bg-purple-50 border-b">
            <tr>
                <th class="px-2 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider w-10"></th>
                <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap">No.</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>
            </tr>
        </thead>
        <tbody id="menu-sortable" class="bg-white divide-y divide-gray-200">
            @forelse($menus as $menu)
                <tr class="menu-row {{ $menu->deleted_at ? 'bg-red-50' : 'hover:bg-gray-50' }}"
                    data-menu-id="{{ $menu->id }}"
                    data-parent-id="{{ $menu->parent_id ?? '' }}"
                    data-sort-order="{{ $menu->sort_order ?? 0 }}"
                    data-depth="{{ $menu->depth ?? 0 }}">
                    <td class="px-2 py-2 whitespace-nowrap text-center">
                        @if(!$menu->deleted_at)
                        <span class="drag-handle cursor-grab active:cursor-grabbing text-gray-400 hover:text-gray-600">
                            <svg class="w-4 h-4 inline-block" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 8h16M4 16h16" />
                            </svg>
                        </span>
                        @endif
                    </td>
                    <td class="px-3 py-2 whitespace-nowrap text-sm text-gray-900">
                        {{ $loop->iteration }}
                    </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-purple-500 hover:text-purple-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

                            <div class="flex items-center gap-1.5">
                                <span class="text-sm {{ ($menu->depth ?? 0) === 0 ? 'font-semibold text-gray-900' : 'font-medium text-gray-700' }}">
                                    {{ $menu->name }}
                                </span>
                                @if($menu->is_external)
                                    <span class="text-xs text-purple-600">(외부)</span>
                                @endif
                            </div>
                        </div>
                    </td>
                    <td class="px-3 py-2 whitespace-nowrap text-sm text-gray-500">
                        @if($menu->is_external && $menu->external_url)
                            <a href="{{ $menu->external_url }}" target="_blank" class="text-purple-600 hover:underline">
                                {{ Str::limit($menu->external_url, 30) }}
                            </a>
                        @elseif($menu->url)
                            {{ $menu->url }}
                        @else
                            -
                        @endif
                    </td>
                    <td class="px-3 py-2 whitespace-nowrap text-sm text-gray-500 text-center">
                        {{ $menu->sort_order ?? 0 }}
                    </td>
                    <td class="px-3 py-2 whitespace-nowrap text-center">
                        @if(!$menu->deleted_at)
                        <button type="button" onclick="toggleActive({{ $menu->id }}, this)"
                                class="relative inline-flex h-4 w-7 items-center rounded-full transition-colors focus:outline-none focus:ring-1 focus:ring-purple-500 focus:ring-offset-1 {{ $menu->is_active ? 'bg-purple-500' : 'bg-gray-400' }}">
                            <span class="inline-block h-3 w-3 transform rounded-full bg-white shadow-sm transition-transform {{ $menu->is_active ? 'translate-x-3.5' : 'translate-x-0.5' }}"></span>
                        </button>
                        @else
                        <span class="text-gray-400">-</span>
                        @endif
                    </td>
                    <td class="px-3 py-2 whitespace-nowrap text-center">
                        @if(!$menu->deleted_at)
                        <button type="button"
                                onclick="toggleHidden({{ $menu->id }}, this)"
                                class="relative inline-flex h-4 w-7 items-center rounded-full transition-colors focus:outline-none focus:ring-1 focus:ring-amber-500 focus:ring-offset-1 {{ $menu->hidden ? 'bg-amber-500' : 'bg-gray-400' }}">
                            <span class="inline-block h-3 w-3 transform rounded-full bg-white shadow-sm transition-transform {{ $menu->hidden ? 'translate-x-3.5' : 'translate-x-0.5' }}"></span>
                        </button>
                        @else
                        <span class="text-gray-400">-</span>
                        @endif
                    </td>
                    <td class="px-3 py-2 whitespace-nowrap text-center">
                        @php
                            // 게시판 연동 메뉴인지 확인 (URL 패턴: /customer-center/*, /boards/*)
                            $isBoardMenu = $menu->url && (str_starts_with($menu->url, '/customer-center/') || str_starts_with($menu->url, '/boards/'));
                        @endphp
                        @if($menu->deleted_at)
                            <div class="flex items-center justify-center gap-1">
                                <button onclick="confirmRestore({{ $menu->id }}, '{{ $menu->name }}')"
                                        class="inline-flex items-center px-2 py-1 text-xs font-medium rounded bg-green-100 text-green-700 hover:bg-green-200">
                                    복원
                                </button>
                                @if(!$isBoardMenu)
                                <button onclick="confirmForceDelete({{ $menu->id }}, '{{ $menu->name }}')"
                                        class="inline-flex items-center px-2 py-1 text-xs font-medium rounded bg-red-100 text-red-700 hover:bg-red-200">
                                    영구삭제
                                </button>
                                @endif
                            </div>
                        @elseif($isBoardMenu)
                            {{-- 게시판 연동 메뉴: 수정/삭제 불가 --}}
                            <span class="inline-flex items-center px-2 py-1 text-xs font-medium rounded bg-indigo-100 text-indigo-700" title="게시판 관리에서 수정/삭제해주세요">
                                📋 게시판
                            </span>
                        @else
                            <div class="flex items-center justify-center gap-1">
                                <a href="{{ route('menus.global.edit', $menu->id) }}"
                                   class="inline-flex items-center px-2 py-1 text-xs font-medium rounded bg-purple-100 text-purple-700 hover:bg-purple-200">
                                    수정
                                </a>
                                <button onclick="confirmDelete({{ $menu->id }}, '{{ $menu->name }}')"
                                        class="inline-flex items-center px-2 py-1 text-xs font-medium rounded bg-red-100 text-red-700 hover:bg-red-200">
                                    삭제
                                </button>
                            </div>
                        @endif
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="8" class="px-4 py-3 text-center text-gray-500">
                        기본 메뉴가 없습니다.
                    </td>
                </tr>
            @endforelse
        </tbody>
    </table>
</div>

@if(method_exists($menus, 'hasPages'))
@include('partials.pagination', [
    'paginator' => $menus,
    'target' => '#menu-table',
    'includeForm' => '#filterForm'
])
@endif