{{-- 영업파트너 수당 테이블 (1차/2차 그룹 레이아웃) --}}
<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="w-12 px-2 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-left 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>
                    <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">
                @php
                    $grouped = $commissions->getCollection()->groupBy('management_id');
                @endphp

                @forelse ($grouped as $managementId => $group)
                    @php
                        $deposit = $group->firstWhere('payment_type', 'deposit');
                        $balance = $group->firstWhere('payment_type', 'balance');
                        $firstComm = $deposit ?? $balance;
                        $mgmt = $firstComm->management;

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

                        $totalFee = $mgmt?->total_registration_fee
                            ?? $mgmt?->contractProducts?->sum('registration_fee')
                            ?? 0;
                        $calcBase = $totalFee / 2;

                        if ($firstComm->partner_rate > 0) {
                            $pRate = $firstComm->partner_rate;
                        } else {
                            $pRate = $isGroup ? 30 : 20;
                        }
                        $rRate = $isGroup ? 3 : 5;

                        $partnerName = $firstComm->partner?->user?->name
                            ?? $mgmt?->salesPartner?->user?->name
                            ?? $mgmt?->tenantProspect?->registeredBy?->name
                            ?? '-';

                        $referrerName = $firstComm->referrerPartner?->user?->name;
                        if (!$referrerName) {
                            $partnerUser = $firstComm->partner?->user
                                ?? $mgmt?->salesPartner?->user
                                ?? $mgmt?->tenantProspect?->registeredBy;
                            $referrerName = $partnerUser?->parent?->name;
                        }

                        $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',
                        ];

                        // 데이터 있는 차수만 필터
                        $activePhases = collect();
                        if ($deposit) $activePhases->push(['label' => '1차', 'comm' => $deposit, 'color' => 'bg-blue-50 text-blue-700', 'paidField' => 'first_partner_paid_at']);
                        if ($balance) $activePhases->push(['label' => '2차', 'comm' => $balance, 'color' => 'bg-green-50 text-green-700', 'paidField' => 'second_partner_paid_at']);
                        $rowCount = $activePhases->count();
                    @endphp

                    @foreach ($activePhases as $idx => $phase)
                        @php
                            $c = $phase['comm'];
                            $isFirst = $idx === 0;

                            $displayPayment = $c->payment_amount > 0
                                ? $c->payment_amount
                                : ($c->payment_type === 'deposit'
                                    ? ($mgmt?->deposit_amount ?? $calcBase)
                                    : ($mgmt?->balance_amount ?? $calcBase));

                            $displayPartnerComm = $c->partner_commission > 0
                                ? $c->partner_commission
                                : ($calcBase > 0 ? $calcBase * ($pRate / 100) : 0);
                            $displayReferrerComm = $c->referrer_commission > 0
                                ? $c->referrer_commission
                                : ($calcBase > 0 ? $calcBase * ($rRate / 100) : 0);

                            $paidValue = $c->{$phase['paidField']}?->format('Y-m-d');
                        @endphp

                        <tr class="{{ $isFirst ? 'border-t border-gray-200' : '' }} {{ $idx === $rowCount - 1 ? 'border-b border-gray-200' : '' }} {{ !$isFirst ? 'bg-gray-50/30' : '' }}">
                            {{-- 체크박스 --}}
                            <td class="px-3 py-1.5">
                                @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>

                            @if ($isFirst)
                                {{-- 테넌트 --}}
                                <td class="px-4 py-1.5 align-middle" @if($rowCount > 1) rowspan="{{ $rowCount }}" @endif>
                                    <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-1.5 text-center align-middle" @if($rowCount > 1) rowspan="{{ $rowCount }}" @endif>
                                    <span class="text-sm text-gray-600">{{ $mgmt?->contracted_at?->format('Y-m-d') ?? '-' }}</span>
                                </td>
                            @endif

                            {{-- 구분 --}}
                            <td class="px-2 py-1.5 text-center">
                                <span class="inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-semibold {{ $phase['color'] }}">{{ $phase['label'] }}</span>
                            </td>

                            {{-- 입금 --}}
                            <td class="px-3 py-1.5">
                                @if ($displayPayment > 0)
                                    <div class="text-sm font-bold text-gray-900">{{ number_format($displayPayment) }}</div>
                                @else
                                    <div class="text-sm text-gray-400">-</div>
                                @endif
                                <div class="text-xs text-gray-500">{{ $c->payment_date->format('Y-m-d') }}</div>
                            </td>

                            {{-- 영업파트너 --}}
                            <td class="px-3 py-1.5">
                                <div class="text-sm text-gray-900">{{ $partnerName }} <span class="text-gray-400">({{ $pRate }}%)</span></div>
                                <div class="text-sm font-medium text-emerald-600">
                                    @if ($displayPartnerComm > 0)
                                        {{ number_format($displayPartnerComm) }}
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </div>
                            </td>

                            {{-- 유치파트너 --}}
                            <td class="px-3 py-1.5">
                                @if ($referrerName)
                                    <div class="text-sm text-gray-900">{{ $referrerName }} <span class="text-gray-400">({{ $rRate }}%)</span></div>
                                    <div class="text-sm font-medium text-orange-600">
                                        @if ($displayReferrerComm > 0)
                                            {{ number_format($displayReferrerComm) }}
                                        @else
                                            <span class="text-gray-400">-</span>
                                        @endif
                                    </div>
                                @else
                                    <span class="text-sm text-gray-400">-</span>
                                @endif
                            </td>

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

                            {{-- 수당지급일 --}}
                            <td class="px-3 py-1.5 text-center">
                                <input type="date"
                                    class="h-6 text-xs px-1 border border-gray-300 rounded cursor-pointer hover:border-blue-400 focus:outline-none focus:border-blue-500 {{ $paidValue ? 'text-blue-600 font-medium bg-blue-50 border-blue-400' : 'text-gray-500 bg-gray-50' }}"
                                    value="{{ $paidValue }}"
                                    onchange="saveSettlementDate({{ $c->id }}, '{{ $phase['paidField'] }}', this.value)">
                            </td>

                            {{-- 협업지원금 --}}
                            <td class="px-3 py-1.5 text-center">
                                @if(!$isGroup)
                                    <input type="text"
                                        class="w-24 h-6 text-xs px-1 border border-gray-300 rounded cursor-pointer hover:border-orange-400 focus:outline-none focus:border-orange-500 text-right {{ ($c->referrer_commission ?? 0) > 0 ? 'text-orange-600 font-medium bg-orange-50 border-orange-400' : 'text-gray-500 bg-gray-50' }}"
                                        value="{{ ($c->referrer_commission ?? 0) > 0 ? number_format(intval($c->referrer_commission)) : '' }}"
                                        placeholder="0"
                                        onfocus="this.value = this.value.replace(/,/g, '')"
                                        onblur="formatCommissionInput(this)"
                                        onchange="saveSettlementDate({{ $c->id }}, 'referrer_commission', this.value.replace(/,/g, ''))">
                                @else
                                    <span class="text-gray-400">-</span>
                                @endif
                            </td>

                            {{-- 상태 --}}
                            <td class="px-3 py-1.5 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-1.5 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>
                    @endforeach
                @empty
                    <tr>
                        <td colspan="12" 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>
