@if($definitions->isEmpty())
    <div class="p-8 text-center text-gray-500">
        <div class="mb-2">
            <svg class="w-12 h-12 mx-auto text-gray-300" 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>
        </div>
        <p>등록된 시스템 필드 정의가 없습니다.</p>
        <p class="text-xs mt-1">"+ 필드 정의 추가" 버튼을 클릭하여 새 필드를 추가하세요.</p>
    </div>
@else
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead class="bg-gray-50 border-b border-gray-200">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-16">순서</th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">소스 테이블</th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">필드 키</th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">필드명</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">타입</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">필수</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">기본시딩</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">옵션</th>
                    <th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">액션</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200">
                @php $currentSourceTable = null; @endphp
                @foreach($definitions as $def)
                    @if($currentSourceTable !== $def->source_table)
                        @php $currentSourceTable = $def->source_table; @endphp
                        <tr class="bg-indigo-50">
                            <td colspan="9" class="px-4 py-2">
                                <div class="flex items-center justify-between">
                                    <div>
                                        <span class="font-semibold text-indigo-700">{{ $def->source_table_label ?? $def->source_table }}</span>
                                        <code class="ml-2 text-xs font-mono text-indigo-600 bg-indigo-100 px-1.5 py-0.5 rounded">{{ $def->source_table }}</code>
                                    </div>
                                    <div class="flex items-center gap-2">
                                        <button onclick="syncTableFieldNames('{{ $def->source_table }}')"
                                                class="inline-flex items-center px-2 py-1 text-xs font-medium text-indigo-600 bg-indigo-100 hover:bg-indigo-200 rounded transition-colors"
                                                title="DB COMMENT에서 필드명 동기화">
                                            <svg class="w-3.5 h-3.5 mr-1" 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>
                                        <button onclick="deleteSourceTable('{{ $def->source_table }}', '{{ $def->source_table_label ?? $def->source_table }}')"
                                                class="inline-flex items-center px-2 py-1 text-xs font-medium text-red-600 bg-red-50 hover:bg-red-100 rounded transition-colors"
                                                title="테이블 삭제">
                                            <svg class="w-3.5 h-3.5 mr-1" 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>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    @endif
                    <tr class="hover:bg-gray-50">
                        <!-- 순서 -->
                        <td class="px-4 py-3 text-center">
                            <span class="text-sm text-gray-500">{{ $def->order_no }}</span>
                        </td>
                        <!-- 소스 테이블 -->
                        <td class="px-4 py-3">
                            <code class="text-xs font-mono text-gray-600">{{ $def->source_table }}</code>
                        </td>
                        <!-- 필드 키 -->
                        <td class="px-4 py-3">
                            <code class="text-xs font-mono text-gray-700 bg-gray-100 px-1.5 py-0.5 rounded">{{ $def->field_key }}</code>
                        </td>
                        <!-- 필드명 -->
                        <td class="px-4 py-3">
                            <div class="font-medium text-gray-900 text-sm">
                                {{ $def->field_name }}
                                @if($def->is_required)
                                    <span class="text-red-500">*</span>
                                @endif
                            </div>
                            @if($def->default_value)
                                <div class="text-xs text-gray-500">기본값: {{ $def->default_value }}</div>
                            @endif
                        </td>
                        <!-- 타입 -->
                        <td class="px-4 py-3 text-center">
                            @php
                                $typeLabels = [
                                    'textbox' => ['label' => '텍스트', 'color' => 'gray'],
                                    'number' => ['label' => '숫자', 'color' => 'blue'],
                                    'dropdown' => ['label' => '드롭다운', 'color' => 'purple'],
                                    'checkbox' => ['label' => '체크박스', 'color' => 'green'],
                                    'date' => ['label' => '날짜', 'color' => 'orange'],
                                    'textarea' => ['label' => '텍스트영역', 'color' => 'gray'],
                                ];
                                $typeInfo = $typeLabels[$def->field_type] ?? ['label' => $def->field_type, 'color' => 'gray'];
                            @endphp
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-{{ $typeInfo['color'] }}-100 text-{{ $typeInfo['color'] }}-700">
                                {{ $typeInfo['label'] }}
                            </span>
                        </td>
                        <!-- 필수 여부 -->
                        <td class="px-4 py-3 text-center">
                            @if($def->is_required)
                                <svg class="w-4 h-4 inline text-green-600" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd" />
                                </svg>
                            @else
                                <span class="text-gray-300">-</span>
                            @endif
                        </td>
                        <!-- 기본 시딩 여부 -->
                        <td class="px-4 py-3 text-center">
                            @if($def->is_seed_default)
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-700">
                                    시딩대상
                                </span>
                            @else
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-gray-100 text-gray-500">
                                    제외
                                </span>
                            @endif
                        </td>
                        <!-- 옵션 -->
                        <td class="px-4 py-3 text-center">
                            @if(!empty($def->options))
                                @php
                                    $optionsData = is_array($def->options) ? $def->options : json_decode($def->options, true);
                                    $optionCount = is_array($optionsData) ? count($optionsData) : 0;
                                @endphp
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-purple-100 text-purple-700">
                                    {{ $optionCount }}개
                                </span>
                            @else
                                <span class="text-gray-300">-</span>
                            @endif
                        </td>
                        <!-- 액션 -->
                        <td class="px-4 py-3 text-right">
                            <div class="flex justify-end gap-1">
                                <button onclick="openSysDefEditModal({{ json_encode($def) }})"
                                        class="text-blue-600 hover:text-blue-800 p-1" 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>
                                </button>
                                <button onclick="deleteSysDefField({{ $def->id }}, '{{ $def->field_name }}')"
                                        class="text-red-600 hover:text-red-800 p-1" 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>
                            </div>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>

    <!-- 요약 정보 -->
    <div class="px-4 py-3 bg-gray-50 border-t border-gray-200">
        <div class="flex flex-wrap items-center justify-between gap-2 text-sm text-gray-600">
            <div class="flex items-center gap-4">
                <span>총 {{ $definitions->count() }}개</span>
                @php
                    $groupedByTable = $definitions->groupBy('source_table');
                    $seedDefaultCount = $definitions->filter(fn($d) => $d->is_seed_default)->count();
                @endphp
                @foreach($groupedByTable as $table => $items)
                    <span class="text-indigo-600">{{ $table }}: {{ $items->count() }}개</span>
                @endforeach
            </div>
            <div class="flex items-center gap-4 text-xs">
                <span class="text-green-600">시딩대상: {{ $seedDefaultCount }}개</span>
                <span class="text-gray-500">제외: {{ $definitions->count() - $seedDefaultCount }}개</span>
            </div>
        </div>
    </div>
@endif
