{{-- 영업파트너 상세 모달 내용 --}}
<div class="p-6 max-h-[80vh] overflow-y-auto">
    <!-- 헤더 -->
    <div class="flex justify-between items-start mb-6">
        <div>
            <h2 class="text-xl font-bold text-gray-800">{{ $partner->name }}</h2>
            <p class="text-sm text-gray-500 mt-1">레벨 {{ $level }} 영업파트너</p>
            <span class="inline-block mt-2 px-2 py-1 text-xs font-medium rounded-full {{ $partner->approval_status_color }}">
                {{ $partner->approval_status_label }}
            </span>
        </div>
        <button type="button" onclick="typeof closeDetailModal !== 'undefined' ? closeDetailModal() : closePartnerModal()" class="text-gray-400 hover:text-gray-600">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
            </svg>
        </button>
    </div>

    <!-- 승인 대기 알림 -->
    @if($partner->isPendingApproval())
    <div class="mb-6 bg-yellow-50 border border-yellow-200 rounded-lg p-4">
        <div class="flex items-start gap-3">
            <svg class="w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5" 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>
            <div class="flex-1">
                <h3 class="font-semibold text-yellow-800 text-sm">승인 대기 중</h3>
                <p class="text-xs text-yellow-700 mt-1">
                    @if(auth()->user()->isAdmin())
                    첨부된 서류를 확인 후 승인 또는 반려해주세요.
                    @else
                    본사 운영팀에서 승인 처리 예정입니다.
                    @endif
                </p>
                @if(auth()->user()->isAdmin())
                <div class="mt-3 flex gap-2">
                    <form action="{{ route('sales.managers.approve', $partner->id) }}" method="POST" class="inline">
                        @csrf
                        <button type="submit" onclick="return confirm('승인하시겠습니까?')"
                                class="px-3 py-1.5 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-xs">
                            승인
                        </button>
                    </form>
                    <button type="button" onclick="showRejectForm()"
                            class="px-3 py-1.5 bg-red-600 text-white rounded-lg hover:bg-red-700 transition text-xs">
                        반려
                    </button>
                </div>
                <!-- 반려 폼 (숨김) -->
                <form id="rejectForm" action="{{ route('sales.managers.reject', $partner->id) }}" method="POST" class="hidden mt-3">
                    @csrf
                    <textarea name="rejection_reason" rows="2" required
                              class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-red-500"
                              placeholder="반려 사유를 입력해주세요."></textarea>
                    <div class="mt-2 flex gap-2">
                        <button type="submit" class="px-3 py-1.5 bg-red-600 text-white rounded-lg hover:bg-red-700 transition text-xs">
                            반려 확인
                        </button>
                        <button type="button" onclick="hideRejectForm()" class="px-3 py-1.5 border border-gray-300 rounded-lg hover:bg-gray-50 transition text-xs">
                            취소
                        </button>
                    </div>
                </form>
                @endif
            </div>
        </div>
    </div>
    @endif

    <!-- 반려된 경우 사유 표시 -->
    @if($partner->isRejected() && $partner->rejection_reason)
    <div class="mb-6 bg-red-50 border border-red-200 rounded-lg p-4">
        <div class="flex items-start gap-3">
            <svg class="w-5 h-5 text-red-500 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
            </svg>
            <div>
                <h3 class="font-semibold text-red-800 text-sm">반려됨</h3>
                <p class="text-sm text-red-700 mt-1">{{ $partner->rejection_reason }}</p>
                @if($partner->approver)
                <p class="text-xs text-red-600 mt-1">처리자: {{ $partner->approver->name }} ({{ $partner->approved_at->format('Y-m-d H:i') }})</p>
                @endif
            </div>
        </div>
    </div>
    @endif

    <div class="grid grid-cols-1 lg:grid-cols-2 gap-4">
        <!-- 기본 정보 -->
        <div class="bg-gray-50 rounded-lg p-4">
            <h3 class="text-sm font-semibold text-gray-800 mb-3">기본 정보</h3>
            <dl class="space-y-2 text-sm">
                <div class="flex justify-between">
                    <dt class="text-gray-500">로그인 ID</dt>
                    <dd class="font-medium text-gray-900">{{ $partner->user_id ?? $partner->email }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">이름</dt>
                    <dd class="font-medium text-gray-900">{{ $partner->name }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">이메일</dt>
                    <dd class="font-medium text-gray-900">{{ $partner->email }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">전화번호</dt>
                    <dd class="font-medium text-gray-900">{{ $partner->phone ?? '-' }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">추천인(유치자)</dt>
                    <dd class="font-medium text-gray-900">
                        @if($partner->parent)
                            {{ $partner->parent->name }}
                        @else
                            <span class="text-gray-400">최상위</span>
                        @endif
                    </dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">등록일</dt>
                    <dd class="font-medium text-gray-900">{{ $partner->created_at->format('Y-m-d H:i') }}</dd>
                </div>
                @if($partner->isApproved() && $partner->approved_at)
                <div class="flex justify-between">
                    <dt class="text-gray-500">승인일</dt>
                    <dd class="font-medium text-gray-900">{{ $partner->approved_at->format('Y-m-d H:i') }}</dd>
                </div>
                @endif
            </dl>
        </div>

        <!-- 통계 -->
        <div class="bg-gray-50 rounded-lg p-4">
            <h3 class="text-sm font-semibold text-gray-800 mb-3">활동 통계</h3>
            <div class="grid grid-cols-2 gap-3">
                <div class="bg-blue-100 rounded-lg p-3 text-center">
                    <div class="text-xl font-bold text-blue-800">{{ $children->count() }}</div>
                    <div class="text-xs text-blue-600">하위 파트너</div>
                </div>
                <div class="bg-green-100 rounded-lg p-3 text-center">
                    <div class="text-xl font-bold text-green-800">{{ $partner->salesDocuments->count() }}</div>
                    <div class="text-xs text-green-600">첨부 서류</div>
                </div>
            </div>
        </div>
    </div>

    <!-- 역할 관리 (승인된 파트너만) -->
    @if($partner->isApproved())
    <div class="mt-4 bg-gray-50 rounded-lg p-4">
        <h3 class="text-sm font-semibold text-gray-800 mb-3">역할 관리</h3>
        @php
            $currentRoles = $partner->userRoles->pluck('role.name')->toArray();
            $roleLabels = ['sales' => '영업파트너', 'manager' => '상담매니저'];
            $roleColors = [
                'sales' => 'bg-blue-100 text-blue-800 border-blue-200',
                'manager' => 'bg-purple-100 text-purple-800 border-purple-200',
            ];
        @endphp
        <div class="flex flex-wrap gap-2 mb-3">
            @forelse($currentRoles as $roleName)
                @if(isset($roleLabels[$roleName]))
                <div class="flex items-center gap-1 px-2 py-1 rounded-full border {{ $roleColors[$roleName] ?? 'bg-gray-100' }}">
                    <span class="text-xs font-medium">{{ $roleLabels[$roleName] }}</span>
                    <form action="{{ route('sales.managers.remove-role', $partner->id) }}" method="POST" class="inline">
                        @csrf
                        <input type="hidden" name="role_name" value="{{ $roleName }}">
                        <button type="submit" onclick="return confirm('이 역할을 제거하시겠습니까?')"
                                class="ml-1 text-gray-400 hover:text-red-500">
                            <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="M6 18L18 6M6 6l12 12" />
                            </svg>
                        </button>
                    </form>
                </div>
                @endif
            @empty
                <span class="text-gray-400 text-xs">역할이 없습니다</span>
            @endforelse
        </div>
        <div class="flex flex-wrap gap-2">
            @foreach(['sales' => '영업파트너', 'manager' => '상담매니저'] as $roleName => $label)
                @if(!in_array($roleName, $currentRoles))
                <form action="{{ route('sales.managers.assign-role', $partner->id) }}" method="POST" class="inline">
                    @csrf
                    <input type="hidden" name="role_name" value="{{ $roleName }}">
                    <button type="submit"
                            class="px-2 py-1 text-xs border border-gray-300 rounded-full hover:bg-gray-100 transition">
                        + {{ $label }}
                    </button>
                </form>
                @endif
            @endforeach
        </div>
    </div>
    @endif

    <!-- 첨부 서류 -->
    @if($partner->salesDocuments->isNotEmpty())
    <div class="mt-4 bg-gray-50 rounded-lg p-4">
        <h3 class="text-sm font-semibold text-gray-800 mb-3">첨부 서류</h3>
        <div class="space-y-2">
            @foreach($partner->salesDocuments as $document)
            <div class="flex items-center justify-between p-2 bg-white rounded border">
                <div class="flex items-center gap-2">
                    <span class="px-2 py-0.5 text-xs bg-gray-100 text-gray-600 rounded">{{ $document->document_type_label }}</span>
                    <span class="text-sm text-gray-700">{{ $document->original_name }}</span>
                </div>
                <a href="{{ route('sales.managers.documents.download', [$partner->id, $document->id]) }}"
                   class="text-xs text-blue-600 hover:underline">다운로드</a>
            </div>
            @endforeach
        </div>
    </div>
    @endif

    <!-- 하위 파트너 -->
    @if($children->isNotEmpty())
    <div class="mt-4 bg-gray-50 rounded-lg p-4">
        <h3 class="text-sm font-semibold text-gray-800 mb-3">하위 파트너 ({{ $children->count() }}명)</h3>
        <div class="space-y-2">
            @foreach($children->take(5) as $child)
            <div class="flex items-center justify-between p-2 bg-white rounded border">
                <div class="flex items-center gap-2">
                    <span class="text-sm font-medium text-gray-900">{{ $child->name }}</span>
                    @foreach($child->userRoles as $userRole)
                    @php
                        $roleColor = match($userRole->role->name ?? '') {
                            'sales' => 'bg-blue-100 text-blue-800',
                            'manager' => 'bg-purple-100 text-purple-800',
                            default => 'bg-gray-100 text-gray-800',
                        };
                        $roleLabel = match($userRole->role->name ?? '') {
                            'sales' => '영업파트너',
                            'manager' => '상담매니저',
                            default => $userRole->role->name ?? '-',
                        };
                    @endphp
                    <span class="px-1.5 py-0.5 text-xs font-medium rounded {{ $roleColor }}">{{ $roleLabel }}</span>
                    @endforeach
                </div>
                <span class="px-2 py-0.5 text-xs rounded-full {{ $child->approval_status_color }}">
                    {{ $child->approval_status_label }}
                </span>
            </div>
            @endforeach
            @if($children->count() > 5)
            <p class="text-xs text-gray-500 text-center mt-2">외 {{ $children->count() - 5 }}명...</p>
            @endif
        </div>
    </div>
    @endif

    <!-- 푸터 버튼 -->
    <div class="mt-6 flex justify-end gap-3">
        <button type="button" onclick="typeof closeDetailModal !== 'undefined' ? closeDetailModal() : closePartnerModal()"
                class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition text-sm">
            닫기
        </button>
        <button type="button" onclick="openEditModal({{ $partner->id }})"
                class="px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition text-sm">
            수정
        </button>
    </div>
</div>

<script>
function showRejectForm() {
    document.getElementById('rejectForm').classList.remove('hidden');
}
function hideRejectForm() {
    document.getElementById('rejectForm').classList.add('hidden');
}
</script>
