{{-- 정산 테이블 --}}
<div class="bg-white rounded-lg shadow-sm overflow-hidden">
    <div class="overflow-x-auto">
        <table class="min-w-full divide-y divide-gray-200">
            <thead class="bg-gray-50">
                <tr>
                    <th class="w-12 px-4 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-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">입금구분</th>
                    <th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">입금액</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">입금일</th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">영업파트너</th>
                    <th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">파트너수당</th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">매니저</th>
                    <th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">매니저수당</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">지급예정일</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">상태</th>
                    <th class="px-4 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 $commission)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            @if (in_array($commission->status, ['pending', 'approved']))
                                <input type="checkbox"
                                       value="{{ $commission->id }}"
                                       onchange="updateSelection()"
                                       class="commission-checkbox rounded border-gray-300 text-emerald-600 focus:ring-emerald-500">
                            @endif
                        </td>
                        <td class="px-4 py-3">
                            <div class="text-sm font-medium text-gray-900">{{ $commission->tenant->name ?? $commission->tenant->company_name ?? '-' }}</div>
                            <div class="text-xs text-gray-500">ID: {{ $commission->tenant_id }}</div>
                        </td>
                        <td class="px-4 py-3">
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium
                                {{ $commission->payment_type === 'deposit' ? 'bg-blue-100 text-blue-800' : 'bg-green-100 text-green-800' }}">
                                {{ $commission->payment_type_label }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-right text-sm text-gray-900">
                            {{ number_format($commission->payment_amount) }}
                        </td>
                        <td class="px-4 py-3 text-center text-sm text-gray-500">
                            {{ $commission->payment_date->format('Y-m-d') }}
                        </td>
                        <td class="px-4 py-3">
                            <div class="text-sm text-gray-900">{{ $commission->partner?->user?->name ?? '-' }}</div>
                            <div class="text-xs text-gray-500">{{ $commission->partner_rate }}%</div>
                        </td>
                        <td class="px-4 py-3 text-right text-sm font-medium text-emerald-600">
                            {{ number_format($commission->partner_commission) }}
                        </td>
                        <td class="px-4 py-3">
                            <div class="text-sm text-gray-900">{{ $commission->manager?->name ?? '-' }}</div>
                            <div class="text-xs text-gray-500">{{ $commission->manager_rate }}%</div>
                        </td>
                        <td class="px-4 py-3 text-right text-sm font-medium text-blue-600">
                            {{ number_format($commission->manager_commission) }}
                        </td>
                        <td class="px-4 py-3 text-center text-sm text-gray-500">
                            {{ $commission->scheduled_payment_date->format('Y-m-d') }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $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',
                                ];
                            @endphp
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $statusColors[$commission->status] ?? 'bg-gray-100 text-gray-800' }}">
                                {{ $commission->status_label }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            <div class="flex items-center justify-center gap-1">
                                <button type="button"
                                        onclick="openDetailModal({{ $commission->id }})"
                                        class="p-1 text-gray-400 hover:text-gray-600"
                                        title="상세보기">
                                    <svg class="w-5 h-5" 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 ($commission->status === 'pending')
                                    <button type="button"
                                            onclick="approveCommission({{ $commission->id }})"
                                            class="p-1 text-blue-400 hover:text-blue-600"
                                            title="승인">
                                        <svg class="w-5 h-5" 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({{ $commission->id }})"
                                            class="p-1 text-red-400 hover:text-red-600"
                                            title="취소">
                                        <svg class="w-5 h-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>
                                    </button>
                                @elseif ($commission->status === 'approved')
                                    <button type="button"
                                            onclick="markPaidCommission({{ $commission->id }})"
                                            class="p-1 text-green-400 hover:text-green-600"
                                            title="지급완료">
                                        <svg class="w-5 h-5" 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="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>
