@extends('layouts.app')

@section('title', '공통코드 관리')

@section('content')
<div class="flex flex-col h-full">
    <!-- 페이지 헤더 -->
    <div class="flex flex-col sm:flex-row sm:justify-between sm:items-center gap-4 mb-6 flex-shrink-0">
        <div>
            <h1 class="text-2xl font-bold text-gray-800">공통코드 관리</h1>
            <p class="text-sm text-gray-500 mt-1">
                @if($tenant)
                    <span class="inline-flex items-center gap-1">
                        <span class="px-2 py-0.5 bg-blue-100 text-blue-700 rounded text-xs font-medium">{{ $tenant->company_name }}</span>
                        @if($isHQ)
                            <span class="px-2 py-0.5 bg-purple-100 text-purple-700 rounded text-xs font-medium">본사</span>
                        @endif
                        코드를 관리합니다.
                    </span>
                @else
                    테넌트별 공통코드를 관리합니다.
                @endif
            </p>
        </div>
        <div class="flex items-center gap-2">
            <a href="{{ route('common-codes.sync.index') }}"
               class="px-4 py-2 bg-gray-600 hover:bg-gray-700 text-white font-medium rounded-lg transition-colors flex items-center gap-2 text-sm">
                <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="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4" />
                </svg>
                동기화
            </a>
            @if($tenant)
                <button type="button"
                        onclick="openAddModal()"
                        class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition-colors flex items-center gap-2 text-sm">
                    <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="M12 4v16m8-8H4" />
                    </svg>
                    코드 추가
                </button>
            @endif
        </div>
    </div>

    <!-- 테넌트 미선택 경고 -->
    @if(!$tenant)
        <div class="mb-4 bg-yellow-50 border border-yellow-200 text-yellow-700 px-4 py-3 rounded-lg flex items-center gap-2">
            <svg class="w-5 h-5 text-yellow-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
            </svg>
            헤더에서 테넌트를 선택해주세요.
        </div>
    @else
        <!-- 성공/에러 메시지 -->
        @if(session('success'))
            <div class="mb-4 bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-lg flex items-center gap-2">
                <svg class="w-5 h-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
                {{ session('success') }}
            </div>
        @endif
        @if(session('error'))
            <div class="mb-4 bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg flex items-center gap-2">
                <svg class="w-5 h-5 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
                {{ session('error') }}
            </div>
        @endif

        <!-- 메인 레이아웃: 좌측 탭 + 우측 콘텐츠 -->
        <div class="flex gap-4 flex-1 min-h-0">
            <!-- 좌측: 코드 그룹 탭 (세로) -->
            <div class="w-44 flex-shrink-0 bg-white rounded-lg shadow-sm overflow-hidden flex flex-col">
                <div class="px-2 py-1.5 bg-gray-50 border-b border-gray-200 flex items-center justify-between flex-shrink-0">
                    <span class="text-[10px] font-semibold text-gray-500 uppercase tracking-wider">코드 그룹</span>
                    <button type="button" onclick="openGroupModal()"
                            class="w-5 h-5 flex items-center justify-center text-gray-400 hover:text-blue-600 hover:bg-blue-50 rounded transition-colors"
                            title="그룹 추가">
                        <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                        </svg>
                    </button>
                </div>
                <!-- 스코프 필터 -->
                <div class="px-1.5 py-1 border-b border-gray-200 flex flex-wrap gap-0.5 flex-shrink-0">
                    <button type="button" onclick="filterGroups('all')"
                            class="group-filter-btn active text-[10px] px-1.5 py-0.5 rounded font-medium transition-colors bg-gray-700 text-white" data-scope="all">전체</button>
                    <button type="button" onclick="filterGroups('global-tenant')"
                            class="group-filter-btn text-[10px] px-1.5 py-0.5 rounded font-medium transition-colors bg-gray-100 text-gray-600 hover:bg-gray-200" data-scope="global-tenant">글로벌+테넌트</button>
                    <button type="button" onclick="filterGroups('global-based')"
                            class="group-filter-btn text-[10px] px-1.5 py-0.5 rounded font-medium transition-colors bg-gray-100 text-gray-600 hover:bg-gray-200" data-scope="global-based">글로벌</button>
                    <button type="button" onclick="filterGroups('tenant-based')"
                            class="group-filter-btn text-[10px] px-1.5 py-0.5 rounded font-medium transition-colors bg-gray-100 text-gray-600 hover:bg-gray-200" data-scope="tenant-based">테넌트</button>
                </div>
                <nav class="overflow-y-auto flex-1 min-h-0" aria-label="Tabs">
                    @foreach($codeGroups as $group => $label)
                        @php $scope = $groupScopes[$group] ?? 'custom'; @endphp
                        <a href="{{ route('common-codes.index', ['group' => $group]) }}"
                           data-scope="{{ $scope }}"
                           class="group-item block px-2 py-1.5 border-l-3 transition-colors
                                  {{ $selectedGroup === $group
                                      ? 'border-l-blue-500 bg-blue-50 text-blue-700'
                                      : 'border-l-transparent text-gray-600 hover:bg-gray-50 hover:text-gray-900' }}">
                            <span class="flex items-center gap-1">
                                <span class="block text-xs font-medium truncate flex-1">{{ $label }}</span>
                                @if($scope === 'global')
                                    <span class="w-1.5 h-1.5 rounded-full bg-purple-400 flex-shrink-0" title="글로벌"></span>
                                @elseif($scope === 'both')
                                    <span class="w-1.5 h-1.5 rounded-full bg-green-400 flex-shrink-0" title="글로벌+테넌트"></span>
                                @elseif($scope === 'tenant')
                                    <span class="w-1.5 h-1.5 rounded-full bg-blue-400 flex-shrink-0" title="테넌트"></span>
                                @else
                                    <span class="w-1.5 h-1.5 rounded-full bg-gray-300 flex-shrink-0" title="빈 그룹"></span>
                                @endif
                            </span>
                            <span class="block text-[10px] font-mono {{ $selectedGroup === $group ? 'text-blue-400' : 'text-gray-400' }} truncate">{{ $group }}</span>
                        </a>
                    @endforeach
                </nav>
            </div>

            <!-- 우측: 코드 목록 -->
            <div class="flex-1 grid grid-cols-1 lg:grid-cols-2 gap-4 min-w-0">
            <!-- 글로벌 코드 -->
            <div class="bg-white rounded-lg shadow-sm">
                <div class="px-4 py-3 border-b border-gray-200 flex items-center justify-between">
                    <div class="flex items-center gap-2">
                        <span class="w-6 h-6 bg-purple-100 rounded flex items-center justify-center">
                            <svg class="w-4 h-4 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                            </svg>
                        </span>
                        <h3 class="font-semibold text-gray-800">글로벌 코드</h3>
                        <span class="text-xs text-gray-500">({{ $globalCodes->count() }})</span>
                    </div>
                    <div class="flex items-center gap-2">
                        @if($globalCodes->count() > 0)
                            <button type="button"
                                    id="bulkCopyBtn"
                                    onclick="bulkCopy()"
                                    class="hidden px-3 py-1.5 bg-green-600 hover:bg-green-700 text-white text-xs font-medium rounded-lg transition-colors flex items-center gap-1">
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
                                </svg>
                                <span id="bulkCopyCount">0</span>개 선택 복사
                            </button>
                        @endif
                        @if(!$isHQ)
                            <span class="text-xs text-gray-400">본사만 편집 가능</span>
                        @endif
                    </div>
                </div>
                <div class="overflow-x-auto">
                    <table class="w-full text-sm">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-3 py-2 text-center w-10">
                                    <input type="checkbox" id="selectAllGlobal"
                                           onchange="toggleSelectAll(this)"
                                           class="w-4 h-4 text-green-600 border-gray-300 rounded focus:ring-green-500">
                                </th>
                                <th class="px-3 py-2 text-left text-xs font-medium text-gray-500">코드</th>
                                <th class="px-3 py-2 text-left text-xs font-medium text-gray-500">이름</th>
                                <th class="px-3 py-2 text-center text-xs font-medium text-gray-500 w-16">순서</th>
                                <th class="px-3 py-2 text-center text-xs font-medium text-gray-500 w-16">활성</th>
                                <th class="px-3 py-2 text-center text-xs font-medium text-gray-500 w-24">액션</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-100">
                            @forelse($globalCodes as $code)
                                @php $existsInTenant = in_array($code->code, $tenantCodeKeys); @endphp
                                <tr class="hover:bg-gray-50 {{ !$code->is_active ? 'opacity-50' : '' }}">
                                    <td class="px-3 py-2 text-center">
                                        <input type="checkbox" name="global_code_ids[]" value="{{ $code->id }}"
                                               onchange="updateBulkCopyButton()"
                                               class="global-code-checkbox w-4 h-4 rounded focus:ring-green-500 {{ $existsInTenant ? 'bg-gray-200 border-gray-300 cursor-not-allowed' : 'text-green-600 border-gray-300' }}"
                                               {{ $existsInTenant ? 'disabled' : '' }}>
                                    </td>
                                    <td class="px-3 py-2">
                                        <span class="font-mono text-xs bg-gray-100 px-1.5 py-0.5 rounded">{{ $code->code }}</span>
                                    </td>
                                    <td class="px-3 py-2">{{ $code->name }}</td>
                                    <td class="px-3 py-2 text-center text-gray-500">{{ $code->sort_order }}</td>
                                    <td class="px-3 py-2 text-center">
                                        @if($isHQ)
                                            <button type="button"
                                                    onclick="toggleActive({{ $code->id }})"
                                                    class="relative inline-flex h-4 w-7 items-center rounded-full transition-colors {{ $code->is_active ? 'bg-green-500' : 'bg-gray-300' }}">
                                                <span class="inline-block h-3 w-3 transform rounded-full bg-white transition-transform {{ $code->is_active ? 'translate-x-3' : 'translate-x-0.5' }}"></span>
                                            </button>
                                        @else
                                            <span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs {{ $code->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-500' }}">
                                                {{ $code->is_active ? 'ON' : 'OFF' }}
                                            </span>
                                        @endif
                                    </td>
                                    <td class="px-3 py-2 text-center">
                                        <div class="flex items-center justify-center gap-1">
                                            @if($isHQ)
                                                <button type="button"
                                                        onclick="openEditModal({{ json_encode($code) }})"
                                                        class="p-1 text-gray-400 hover:text-blue-600 transition-colors"
                                                        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>
                                            @endif
                                            <button type="button"
                                                    onclick="copyCode({{ $code->id }})"
                                                    class="p-1 text-gray-400 hover:text-green-600 transition-colors"
                                                    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="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
                                                </svg>
                                            </button>
                                            @if($isHQ)
                                                <button type="button"
                                                        onclick="deleteCode({{ $code->id }}, '{{ $code->code }}')"
                                                        class="p-1 text-gray-400 hover:text-red-600 transition-colors"
                                                        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="6" class="px-3 py-8 text-center text-gray-400">
                                        글로벌 코드가 없습니다.
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- 테넌트 코드 -->
            <div class="bg-white rounded-lg shadow-sm">
                <div class="px-4 py-3 border-b border-gray-200 flex items-center justify-between">
                    <div class="flex items-center gap-2">
                        <span class="w-6 h-6 bg-blue-100 rounded flex items-center justify-center">
                            <svg class="w-4 h-4 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
                            </svg>
                        </span>
                        <h3 class="font-semibold text-gray-800">테넌트 코드</h3>
                        <span class="text-xs text-gray-500">({{ $tenantCodes->count() }})</span>
                    </div>
                    <div class="flex items-center gap-2">
                        @if($tenantCodes->count() > 0 && ($isHQ || auth()->user()?->isSuperAdmin()))
                            <button type="button"
                                    id="bulkPromoteBtn"
                                    onclick="bulkPromote()"
                                    class="hidden px-3 py-1.5 bg-purple-600 hover:bg-purple-700 text-white text-xs font-medium rounded-lg transition-colors flex items-center gap-1">
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                                </svg>
                                <span id="bulkPromoteCount">0</span>개 글로벌로 복사
                            </button>
                        @endif
                    </div>
                </div>
                <div class="overflow-x-auto">
                    <table class="w-full text-sm">
                        <thead class="bg-gray-50">
                            <tr>
                                @if($isHQ || auth()->user()?->isSuperAdmin())
                                    <th class="px-3 py-2 text-center w-10">
                                        <input type="checkbox" id="selectAllTenant"
                                               onchange="toggleSelectAllTenant(this)"
                                               class="w-4 h-4 text-purple-600 border-gray-300 rounded focus:ring-purple-500">
                                    </th>
                                @endif
                                <th class="px-3 py-2 text-left text-xs font-medium text-gray-500">코드</th>
                                <th class="px-3 py-2 text-left text-xs font-medium text-gray-500">이름</th>
                                <th class="px-3 py-2 text-center text-xs font-medium text-gray-500 w-16">순서</th>
                                <th class="px-3 py-2 text-center text-xs font-medium text-gray-500 w-16">활성</th>
                                <th class="px-3 py-2 text-center text-xs font-medium text-gray-500 w-24">액션</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-100">
                            @forelse($tenantCodes as $code)
                                @php $existsInGlobal = in_array($code->code, $globalCodeKeys); @endphp
                                <tr class="hover:bg-gray-50 {{ !$code->is_active ? 'opacity-50' : '' }}">
                                    @if($isHQ || auth()->user()?->isSuperAdmin())
                                        <td class="px-3 py-2 text-center">
                                            <input type="checkbox" name="tenant_code_ids[]" value="{{ $code->id }}"
                                                   onchange="updateBulkPromoteButton()"
                                                   class="tenant-code-checkbox w-4 h-4 rounded focus:ring-purple-500 {{ $existsInGlobal ? 'bg-gray-200 border-gray-300 cursor-not-allowed' : 'text-purple-600 border-gray-300' }}"
                                                   {{ $existsInGlobal ? 'disabled' : '' }}>
                                        </td>
                                    @endif
                                    <td class="px-3 py-2">
                                        <span class="font-mono text-xs bg-blue-50 text-blue-700 px-1.5 py-0.5 rounded">{{ $code->code }}</span>
                                    </td>
                                    <td class="px-3 py-2">{{ $code->name }}</td>
                                    <td class="px-3 py-2 text-center text-gray-500">{{ $code->sort_order }}</td>
                                    <td class="px-3 py-2 text-center">
                                        <button type="button"
                                                onclick="toggleActive({{ $code->id }})"
                                                class="relative inline-flex h-4 w-7 items-center rounded-full transition-colors {{ $code->is_active ? 'bg-green-500' : 'bg-gray-300' }}">
                                            <span class="inline-block h-3 w-3 transform rounded-full bg-white transition-transform {{ $code->is_active ? 'translate-x-3' : 'translate-x-0.5' }}"></span>
                                        </button>
                                    </td>
                                    <td class="px-3 py-2 text-center">
                                        <div class="flex items-center justify-center gap-1">
                                            <button type="button"
                                                    onclick="openEditModal({{ json_encode($code) }})"
                                                    class="p-1 text-gray-400 hover:text-blue-600 transition-colors"
                                                    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 type="button"
                                                    onclick="deleteCode({{ $code->id }}, '{{ $code->code }}')"
                                                    class="p-1 text-gray-400 hover:text-red-600 transition-colors"
                                                    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>
                            @empty
                                <tr>
                                    <td colspan="6" class="px-3 py-8 text-center text-gray-400">
                                        테넌트 코드가 없습니다.<br>
                                        <span class="text-xs">글로벌 코드를 복사하거나 새로 추가하세요.</span>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        </div>
    @endif
</div>

<!-- 코드 추가 모달 -->
<div id="addModal" class="fixed inset-0 bg-black/50 hidden items-center justify-center z-50">
    <div class="bg-white rounded-lg shadow-xl w-full max-w-md mx-4">
        <form action="{{ route('common-codes.store') }}" method="POST">
            @csrf
            <input type="hidden" name="code_group" value="{{ $selectedGroup }}">

            <div class="px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-gray-800">코드 추가</h3>
            </div>

            <div class="px-6 py-4 space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">코드 *</label>
                    <input type="text" name="code" required
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                           placeholder="예: NEW_CODE">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">이름 *</label>
                    <input type="text" name="name" required
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                           placeholder="예: 새 코드">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">정렬 순서</label>
                    <input type="number" name="sort_order" value="0" min="0" max="9999"
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">속성 (JSON)</label>
                    <textarea name="attributes" rows="3"
                              class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm font-mono focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                              placeholder='{"key": "value"}'></textarea>
                </div>
                @if($isHQ)
                    <div class="flex items-center gap-2">
                        <input type="checkbox" name="is_global" value="1" id="addIsGlobal"
                               class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
                        <label for="addIsGlobal" class="text-sm text-gray-700">글로벌 코드로 생성</label>
                    </div>
                @endif
            </div>

            <div class="px-6 py-4 border-t border-gray-200 flex justify-end gap-3">
                <button type="button" onclick="closeAddModal()"
                        class="px-4 py-2 text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-lg text-sm font-medium transition-colors">
                    취소
                </button>
                <button type="submit"
                        class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg text-sm font-medium transition-colors">
                    추가
                </button>
            </div>
        </form>
    </div>
</div>

<!-- 코드 수정 모달 -->
<div id="editModal" class="fixed inset-0 bg-black/50 hidden items-center justify-center z-50">
    <div class="bg-white rounded-lg shadow-xl w-full max-w-md mx-4">
        <form id="editForm" method="POST">
            @csrf
            @method('PUT')

            <div class="px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-gray-800">코드 수정</h3>
            </div>

            <div class="px-6 py-4 space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">코드</label>
                    <input type="text" id="editCode" disabled
                           class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm bg-gray-50 text-gray-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">이름 *</label>
                    <input type="text" name="name" id="editName" required
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">정렬 순서</label>
                    <input type="number" name="sort_order" id="editSortOrder" min="0" max="9999"
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">속성 (JSON)</label>
                    <textarea name="attributes" id="editAttributes" rows="3"
                              class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm font-mono focus:ring-2 focus:ring-blue-500 focus:border-blue-500"></textarea>
                </div>
            </div>

            <div class="px-6 py-4 border-t border-gray-200 flex justify-end gap-3">
                <button type="button" onclick="closeEditModal()"
                        class="px-4 py-2 text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-lg text-sm font-medium transition-colors">
                    취소
                </button>
                <button type="submit"
                        class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg text-sm font-medium transition-colors">
                    저장
                </button>
            </div>
        </form>
    </div>
</div>

<!-- 삭제 확인 폼 (hidden) -->
<form id="deleteForm" method="POST" class="hidden">
    @csrf
    @method('DELETE')
</form>

<!-- 복사 폼 (hidden) -->
<form id="copyForm" method="POST" class="hidden">
    @csrf
</form>

<!-- 일괄 글로벌 승격 폼 (hidden) -->
<form id="bulkPromoteForm" action="{{ route('common-codes.bulk-promote') }}" method="POST" class="hidden">
    @csrf
    <input type="hidden" name="ids_json" id="bulkPromoteIds">
</form>

<!-- 일괄 복사 폼 (hidden) -->
<form id="bulkCopyForm" action="{{ route('common-codes.bulk-copy') }}" method="POST" class="hidden">
    @csrf
    <input type="hidden" name="ids_json" id="bulkCopyIds">
</form>

<!-- 코드 그룹 추가 모달 -->
<div id="groupModal" class="fixed inset-0 bg-black/50 hidden items-center justify-center z-50">
    <div class="bg-white rounded-lg shadow-xl w-full max-w-sm mx-4">
        <form action="{{ route('common-codes.store-group') }}" method="POST">
            @csrf
            <div class="px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-gray-800">코드 그룹 추가</h3>
            </div>

            <div class="px-6 py-4 space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">그룹 코드 *</label>
                    <input type="text" name="group_code" required
                           pattern="[a-z][a-z0-9_]*" maxlength="50"
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm font-mono focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                           placeholder="예: payment_method">
                    <p class="mt-1 text-xs text-gray-400">영문 소문자, 숫자, 언더스코어만 사용</p>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">그룹명 *</label>
                    <input type="text" name="group_label" required maxlength="50"
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                           placeholder="예: 결제방법">
                </div>
            </div>

            <div class="px-6 py-4 border-t border-gray-200 flex justify-end gap-3">
                <button type="button" onclick="closeGroupModal()"
                        class="px-4 py-2 text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-lg text-sm font-medium transition-colors">
                    취소
                </button>
                <button type="submit"
                        class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg text-sm font-medium transition-colors">
                    추가
                </button>
            </div>
        </form>
    </div>
</div>
@endsection

@push('scripts')
<script>
    // 그룹 스코프 필터
    function filterGroups(filter) {
        const items = document.querySelectorAll('.group-item');
        items.forEach(item => {
            const scope = item.dataset.scope;
            let show = false;
            switch (filter) {
                case 'all':
                    show = true;
                    break;
                case 'global-tenant':
                    show = (scope === 'global' || scope === 'both' || scope === 'tenant');
                    break;
                case 'global-based':
                    show = (scope === 'global' || scope === 'both');
                    break;
                case 'tenant-based':
                    show = (scope === 'tenant' || scope === 'both');
                    break;
            }
            item.style.display = show ? '' : 'none';
        });

        // 버튼 활성 상태
        document.querySelectorAll('.group-filter-btn').forEach(btn => {
            if (btn.dataset.scope === filter) {
                btn.className = 'group-filter-btn text-[10px] px-1.5 py-0.5 rounded font-medium transition-colors bg-gray-700 text-white';
            } else {
                btn.className = 'group-filter-btn text-[10px] px-1.5 py-0.5 rounded font-medium transition-colors bg-gray-100 text-gray-600 hover:bg-gray-200';
            }
        });
    }

    // 모달 열기/닫기
    function openAddModal() {
        document.getElementById('addModal').classList.remove('hidden');
        document.getElementById('addModal').classList.add('flex');
    }

    function closeAddModal() {
        document.getElementById('addModal').classList.add('hidden');
        document.getElementById('addModal').classList.remove('flex');
    }

    function openGroupModal() {
        document.getElementById('groupModal').classList.remove('hidden');
        document.getElementById('groupModal').classList.add('flex');
    }

    function closeGroupModal() {
        document.getElementById('groupModal').classList.add('hidden');
        document.getElementById('groupModal').classList.remove('flex');
    }

    function openEditModal(code) {
        document.getElementById('editForm').action = `/common-codes/${code.id}`;
        document.getElementById('editCode').value = code.code;
        document.getElementById('editName').value = code.name;
        document.getElementById('editSortOrder').value = code.sort_order || 0;
        document.getElementById('editAttributes').value = code.attributes ? JSON.stringify(code.attributes, null, 2) : '';

        document.getElementById('editModal').classList.remove('hidden');
        document.getElementById('editModal').classList.add('flex');
    }

    function closeEditModal() {
        document.getElementById('editModal').classList.add('hidden');
        document.getElementById('editModal').classList.remove('flex');
    }

    // 활성화 토글
    function toggleActive(id) {
        fetch(`/common-codes/${id}/toggle`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': '{{ csrf_token() }}',
                'Accept': 'application/json'
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            } else {
                alert(data.error || '오류가 발생했습니다.');
            }
        })
        .catch(() => alert('오류가 발생했습니다.'));
    }

    // 코드 복사
    function copyCode(id) {
        if (!confirm('이 글로벌 코드를 테넌트용으로 복사하시겠습니까?')) return;

        const form = document.getElementById('copyForm');
        form.action = `/common-codes/${id}/copy`;
        form.submit();
    }

    function toggleSelectAllTenant(checkbox) {
        document.querySelectorAll('.tenant-code-checkbox').forEach(cb => cb.checked = checkbox.checked);
        updateBulkPromoteButton();
    }

    function updateBulkPromoteButton() {
        const checked = document.querySelectorAll('.tenant-code-checkbox:checked');
        const btn = document.getElementById('bulkPromoteBtn');
        const count = document.getElementById('bulkPromoteCount');
        if (btn) {
            btn.classList.toggle('hidden', checked.length === 0);
            btn.classList.toggle('flex', checked.length > 0);
        }
        if (count) count.textContent = checked.length;
    }

    function bulkPromote() {
        const ids = Array.from(document.querySelectorAll('.tenant-code-checkbox:checked')).map(cb => parseInt(cb.value));
        if (ids.length === 0) return;
        if (!confirm(`선택한 ${ids.length}개 테넌트 코드를 글로벌로 복사하시겠습니까?`)) return;

        const form = document.getElementById('bulkPromoteForm');
        document.getElementById('bulkPromoteIds').value = JSON.stringify(ids);
        form.submit();
    }

    // 전체 선택 토글
    function toggleSelectAll(checkbox) {
        const checkboxes = document.querySelectorAll('.global-code-checkbox');
        checkboxes.forEach(cb => cb.checked = checkbox.checked);
        updateBulkCopyButton();
    }

    // 선택된 항목 수 업데이트 및 버튼 표시/숨김
    function updateBulkCopyButton() {
        const checkboxes = document.querySelectorAll('.global-code-checkbox:checked');
        const count = checkboxes.length;
        const btn = document.getElementById('bulkCopyBtn');
        const countSpan = document.getElementById('bulkCopyCount');

        if (btn) {
            if (count > 0) {
                btn.classList.remove('hidden');
                btn.classList.add('flex');
                countSpan.textContent = count;
            } else {
                btn.classList.add('hidden');
                btn.classList.remove('flex');
            }
        }

        // 전체 선택 체크박스 상태 업데이트
        const allCheckboxes = document.querySelectorAll('.global-code-checkbox');
        const selectAllCheckbox = document.getElementById('selectAllGlobal');
        if (selectAllCheckbox) {
            selectAllCheckbox.checked = allCheckboxes.length > 0 && checkboxes.length === allCheckboxes.length;
            selectAllCheckbox.indeterminate = checkboxes.length > 0 && checkboxes.length < allCheckboxes.length;
        }
    }

    // 일괄 복사
    function bulkCopy() {
        const checkboxes = document.querySelectorAll('.global-code-checkbox:checked');
        const ids = Array.from(checkboxes).map(cb => cb.value);

        if (ids.length === 0) {
            alert('복사할 코드를 선택해주세요.');
            return;
        }

        if (!confirm(`선택한 ${ids.length}개 글로벌 코드를 테넌트용으로 복사하시겠습니까?`)) return;

        // hidden form으로 POST
        const form = document.getElementById('bulkCopyForm');
        const idsInput = document.getElementById('bulkCopyIds');
        idsInput.value = JSON.stringify(ids);
        form.submit();
    }

    // 코드 삭제
    function deleteCode(id, code) {
        if (!confirm(`'${code}' 코드를 삭제하시겠습니까?`)) return;

        const form = document.getElementById('deleteForm');
        form.action = `/common-codes/${id}`;
        form.submit();
    }

    // 모달 외부 클릭 시 닫기
    document.getElementById('addModal').addEventListener('click', function(e) {
        if (e.target === this) closeAddModal();
    });
    document.getElementById('editModal').addEventListener('click', function(e) {
        if (e.target === this) closeEditModal();
    });
    document.getElementById('groupModal').addEventListener('click', function(e) {
        if (e.target === this) closeGroupModal();
    });

    // ESC 키로 모달 닫기
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            closeAddModal();
            closeEditModal();
            closeGroupModal();
        }
    });
</script>
@endpush
