{{-- 매니저 수당 테이블 (계약 단위 1행) --}}
<div class="bg-white rounded-lg shadow-sm overflow-hidden">
    <div class="overflow-x-auto">
        <table class="min-w-full">
            <thead class="bg-gray-50">
                <tr>
                    <th class="w-10 px-3 py-3">
                        <input type="checkbox" onchange="toggleSelectAll(this)" class="rounded border-gray-300 text-emerald-600 focus:ring-emerald-500">
                    </th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">테넌트</th>
                    <th class="px-3 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">계약일</th>
                    <th class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">매니저</th>
                    <th class="px-3 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">지급예정일</th>
                    <th class="px-3 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">매니저지급일</th>
                    <th class="px-3 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">상태</th>
                    <th class="px-3 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">액션</th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">
                @forelse ($commissions as $c)
                    @php
                        $mgmt = $c->management;

                        $partnerObj = $c->partner
                            ?? $mgmt?->salesPartner
                            ?? $mgmt?->tenantProspect?->registeredBy?->salesPartner;
                        $isGroup = $partnerObj?->partner_type === 'corporate';

                        $monthlySubscription = $mgmt?->contractProducts?->sum('subscription_fee') ?? 0;

                        $statusColors = [
                            'pending' => 'bg-yellow-100 text-yellow-800',
                            'approved' => 'bg-blue-100 text-blue-800',
                            'paid' => 'bg-green-100 text-green-800',
                            'cancelled' => 'bg-red-100 text-red-800',
                        ];

                        $hqColors = [
                            'pending' => 'bg-gray-100 text-gray-600',
                            'review' => 'bg-yellow-100 text-yellow-700',
                            'planning' => 'bg-blue-100 text-blue-700',
                            'coding' => 'bg-indigo-100 text-indigo-700',
                            'dev_test' => 'bg-purple-100 text-purple-700',
                            'dev_done' => 'bg-teal-100 text-teal-700',
                            'int_test' => 'bg-cyan-100 text-cyan-700',
                            'handover' => 'bg-green-100 text-green-700',
                        ];
                    @endphp

                    <tr>
                        {{-- 체크박스 --}}
                        <td class="px-3 py-2">
                            @if (in_array($c->status, ['pending', 'approved']))
                                <input type="checkbox"
                                       value="{{ $c->id }}"
                                       onchange="updateSelection()"
                                       class="commission-checkbox rounded border-gray-300 text-emerald-600 focus:ring-emerald-500">
                            @endif
                        </td>

                        {{-- 테넌트 --}}
                        <td class="px-4 py-2">
                            <div class="text-sm font-medium text-gray-900">
                                {{ $mgmt?->tenant?->company_name ?? $mgmt?->tenantProspect?->company_name ?? '-' }}
                            </div>
                            <div class="flex flex-wrap gap-1 mt-1">
                                <span class="inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-medium {{ $isGroup ? 'bg-purple-100 text-purple-800' : 'bg-sky-100 text-sky-800' }}">{{ $isGroup ? '단체' : '개인' }}</span>
                                @if ($mgmt?->hq_status)
                                    <span class="inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-medium {{ $hqColors[$mgmt->hq_status] ?? 'bg-gray-100 text-gray-600' }}">{{ $mgmt->hq_status_label }}</span>
                                @endif
                            </div>
                        </td>

                        {{-- 계약일 --}}
                        <td class="px-3 py-2 text-center">
                            <span class="text-sm text-gray-600">{{ $mgmt?->contracted_at?->format('Y-m-d') ?? '-' }}</span>
                        </td>

                        {{-- 매니저 --}}
                        <td class="px-3 py-2">
                            <div class="text-sm text-gray-900">{{ $c->manager?->name ?? $mgmt?->manager?->name ?? '-' }} <span class="text-gray-400">(구독1월)</span></div>
                            <div class="text-sm font-medium text-blue-600">
                                @if ($monthlySubscription > 0)
                                    {{ number_format($monthlySubscription) }}
                                @else
                                    <span class="text-gray-400">-</span>
                                @endif
                            </div>
                        </td>

                        {{-- 지급예정일 --}}
                        <td class="px-3 py-2 text-center">
                            <span class="text-sm text-gray-600">{{ $c->scheduled_payment_date->format('Y-m-d') }}</span>
                        </td>

                        {{-- 매니저지급일 --}}
                        <td class="px-3 py-2 text-center">
                            @if($isGroup)
                                <span class="text-gray-400">-</span>
                            @else
                                <input type="date"
                                    class="h-6 text-xs px-1 border border-gray-300 rounded cursor-pointer hover:border-purple-400 focus:outline-none focus:border-purple-500 {{ $c->manager_paid_at ? 'text-purple-600 font-medium bg-purple-50 border-purple-400' : 'text-gray-500 bg-gray-50' }}"
                                    value="{{ $c->manager_paid_at?->format('Y-m-d') }}"
                                    onchange="saveSettlementDate({{ $c->id }}, 'manager_paid_at', this.value)">
                            @endif
                        </td>

                        {{-- 상태 --}}
                        <td class="px-3 py-2 text-center">
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $statusColors[$c->status] ?? 'bg-gray-100 text-gray-800' }}">
                                {{ $c->status_label }}
                            </span>
                        </td>

                        {{-- 액션 --}}
                        <td class="px-3 py-2 text-center">
                            <div class="flex items-center justify-center gap-1">
                                <button type="button"
                                        onclick="openDetailModal({{ $c->id }})"
                                        class="p-1 text-gray-400 hover:text-gray-600"
                                        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="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
                                    </svg>
                                </button>
                                @if ($c->status === 'pending')
                                    <button type="button"
                                            onclick="approveCommission({{ $c->id }})"
                                            class="p-1 text-blue-400 hover:text-blue-600"
                                            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="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                        </svg>
                                    </button>
                                    <button type="button"
                                            onclick="cancelCommission({{ $c->id }})"
                                            class="p-1 text-red-400 hover:text-red-600"
                                            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="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                        </svg>
                                    </button>
                                @elseif ($c->status === 'approved')
                                    <button type="button"
                                            onclick="markPaidCommission({{ $c->id }})"
                                            class="p-1 text-green-400 hover:text-green-600"
                                            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="M5 13l4 4L19 7"/>
                                        </svg>
                                    </button>
                                @endif
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="8" class="px-4 py-8 text-center text-gray-500">
                            등록된 정산 내역이 없습니다.
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    @if ($commissions->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $commissions->links() }}
        </div>
    @endif
</div>
