@if($members->isEmpty())
    <div class="p-12 text-center">
        <svg class="mx-auto h-12 w-12 text-gray-400" 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>
        <h3 class="mt-2 text-sm font-medium text-gray-900">등록된 회원사가 없습니다</h3>
        <p class="mt-1 text-sm text-gray-500">새 회원사를 등록해보세요.</p>
        <div class="mt-6">
            <button
                type="button"
                onclick="MemberModal.openCreate()"
                class="inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700"
            >
                <svg class="-ml-1 mr-2 h-5 w-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>
@else
    <div class="flex-1 overflow-auto">
        <table class="min-w-full divide-y divide-gray-200">
            <thead class="bg-gray-50 sticky top-0 z-10">
                <tr>
                    <th scope="col" class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider w-16">
                        T-ID
                    </th>
                    @if($allTenants ?? false)
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        테넌트
                    </th>
                    @endif
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        사업자번호
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        상호 / 대표자
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        바로빌 ID
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        담당자 정보
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        상태
                    </th>
                    <th scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">
                        서버
                    </th>
                    <th scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">
                        바로빌 서비스
                    </th>
                    <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
                        관리
                    </th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">
                @foreach($members as $member)
                    <tr class="hover:bg-gray-50 transition-colors group">
                        <td class="px-4 py-4 whitespace-nowrap text-center">
                            <span class="inline-flex items-center justify-center w-8 h-8 rounded-full text-xs font-bold bg-indigo-100 text-indigo-700" title="Tenant ID: {{ $member->tenant_id }}">
                                {{ $member->tenant_id }}
                            </span>
                        </td>
                        @if($allTenants ?? false)
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-indigo-100 text-indigo-800">
                                {{ $member->tenant?->company_name ?? '미지정' }}
                            </span>
                        </td>
                        @endif
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm font-mono text-gray-500">{{ $member->formatted_biz_no }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm font-medium text-gray-900">{{ $member->corp_name }}</div>
                            <div class="text-xs text-gray-400">{{ $member->ceo_name }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm text-gray-600 font-medium">{{ $member->barobill_id }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm text-gray-700">{{ $member->manager_name ?: '-' }}</div>
                            <div class="text-xs text-gray-400">{{ $member->manager_email ?: '' }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {{ $member->status_color }}">
                                {{ $member->status_label }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-center">
                            <button
                                type="button"
                                onclick="ServerModeManager.requestChange({{ $member->id }}, '{{ addslashes($member->corp_name) }}', '{{ $member->server_mode ?? 'test' }}')"
                                class="px-2.5 py-1 inline-flex items-center text-xs leading-5 font-semibold rounded-full cursor-pointer hover:opacity-80 transition {{ $member->server_mode_color }}"
                                title="클릭하여 서버 변경"
                            >
                                @if(($member->server_mode ?? 'test') === 'test')
                                    <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
                                    </svg>
                                @else
                                    <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h14M5 12a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v4a2 2 0 01-2 2M5 12a2 2 0 00-2 2v4a2 2 0 002 2h14a2 2 0 002-2v-4a2 2 0 00-2-2m-2-4h.01M17 16h.01" />
                                    </svg>
                                @endif
                                {{ $member->server_mode_label }}
                            </button>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-center">
                            <div class="relative inline-block text-left barobill-dropdown">
                                <button
                                    type="button"
                                    onclick="toggleBarobillDropdown(this)"
                                    class="inline-flex items-center px-3 py-1.5 text-xs font-medium text-blue-700 bg-blue-50 hover:bg-blue-100 rounded-lg transition"
                                >
                                    <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
                                    </svg>
                                    바로빌
                                    <svg class="w-4 h-4 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" />
                                    </svg>
                                </button>
                                <div class="barobill-menu hidden absolute right-0 z-20 mt-2 w-56 rounded-lg bg-white shadow-lg ring-1 ring-black ring-opacity-5 transition-all duration-150 ease-out max-h-96 overflow-y-auto">
                                    <!-- 계좌 관리 -->
                                    <div class="px-3 py-2 bg-green-50 border-b border-green-100">
                                        <span class="text-xs font-semibold text-green-700 flex items-center gap-1">
                                            <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z" />
                                            </svg>
                                            계좌 관리
                                        </span>
                                        <p class="text-xs text-green-600 mt-0.5">빠른조회 서비스 필요</p>
                                    </div>
                                    <div class="py-1">
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'bank_account', '계좌 등록')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-green-50">
                                            계좌 등록
                                        </button>
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'bank_account_manage', '계좌 관리')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-green-50">
                                            등록 계좌 관리
                                        </button>
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'bank_account_log', '입출금 내역')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-green-50">
                                            입출금 내역 조회
                                        </button>
                                    </div>

                                    <!-- 카드 관리 -->
                                    <div class="px-3 py-2 bg-blue-50 border-b border-blue-100 border-t">
                                        <span class="text-xs font-semibold text-blue-700 flex items-center gap-1">
                                            <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z" />
                                            </svg>
                                            카드 관리
                                        </span>
                                        <p class="text-xs text-blue-600 mt-0.5">카드사 ID/PW 필요</p>
                                    </div>
                                    <div class="py-1">
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'card', '카드 등록')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-blue-50">
                                            카드 등록
                                        </button>
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'card_manage', '카드 관리')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-blue-50">
                                            등록 카드 관리
                                        </button>
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'card_log', '카드 사용내역')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-blue-50">
                                            카드 사용내역 조회
                                        </button>
                                    </div>

                                    <!-- 전자세금계산서 -->
                                    <div class="px-3 py-2 bg-purple-50 border-b border-purple-100 border-t">
                                        <span class="text-xs font-semibold text-purple-700 flex items-center gap-1">
                                            <svg class="w-3 h-3" 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>
                                            전자세금계산서
                                        </span>
                                        <p class="text-xs text-purple-600 mt-0.5">공동인증서 필요</p>
                                    </div>
                                    <div class="py-1">
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'certificate', '인증서 등록')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-purple-50">
                                            인증서 등록
                                        </button>
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'tax_invoice', '세금계산서 발행')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-purple-50">
                                            세금계산서 발행
                                        </button>
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'tax_invoice_list', '발행 내역')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-purple-50">
                                            발행 내역 조회
                                        </button>
                                    </div>

                                    <!-- 공통 -->
                                    <div class="border-t border-gray-200 py-1">
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.openUrl({{ $member->id }}, '{{ addslashes($member->corp_name) }}', 'cash_charge', '포인트 충전')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-yellow-50">
                                            <svg class="w-4 h-4 mr-2 text-yellow-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                                            </svg>
                                            포인트 충전
                                        </button>
                                        <button type="button" onclick="closeBarobillDropdown(); BarobillService.showStatus({{ $member->id }}, '{{ addslashes($member->corp_name) }}')" class="w-full flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                            <svg class="w-4 h-4 mr-2 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
                                            </svg>
                                            상태 조회
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                            <div class="flex justify-end gap-1 opacity-30 group-hover:opacity-100 transition-opacity">
                                <button
                                    type="button"
                                    onclick="MemberModal.openEdit({{ $member->id }})"
                                    class="p-2 text-blue-600 hover:bg-blue-50 rounded-lg"
                                    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="confirmDeleteMember({{ $member->id }}, '{{ addslashes($member->corp_name) }}')"
                                    class="p-2 text-red-600 hover:bg-red-50 rounded-lg"
                                    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>

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