@if($summaries->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="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>
        <h3 class="mt-2 text-sm font-medium text-gray-900">과금 내역이 없습니다</h3>
        <p class="mt-1 text-sm text-gray-500">{{ $billingMonth }} 기준 과금 내역이 없습니다.</p>
        <p class="mt-1 text-sm text-gray-500">"과금 처리" 버튼을 클릭하여 월정액을 처리하세요.</p>
    </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>
                    @if($allTenants ?? false)
                    <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>
                    <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-right 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>
                    <th scope="col" class="px-6 py-3 text-right 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>
                    <th scope="col" class="px-6 py-3 text-right 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>
                    <th scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider w-20">
                        상세
                    </th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">
                @foreach($summaries as $summary)
                    <tr class="hover:bg-gray-50 transition-colors group">
                        @if($allTenants ?? false)
                        <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">
                                {{ $summary->member->tenant_id ?? '-' }}
                            </span>
                        </td>
                        <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">
                                {{ $summary->member->tenant->company_name ?? '-' }}
                            </span>
                        </td>
                        @endif
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm font-mono text-gray-500">{{ $summary->member->formatted_biz_no ?? '-' }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm font-medium text-gray-900">{{ $summary->member->corp_name ?? '-' }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right">
                            <span class="text-sm {{ $summary->bank_account_fee > 0 ? 'text-green-600 font-medium' : 'text-gray-400' }}">
                                {{ number_format($summary->bank_account_fee) }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right">
                            <span class="text-sm {{ $summary->card_fee > 0 ? 'text-blue-600 font-medium' : 'text-gray-400' }}">
                                {{ number_format($summary->card_fee) }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right">
                            <span class="text-sm {{ $summary->hometax_fee > 0 ? 'text-orange-600 font-medium' : 'text-gray-400' }}">
                                {{ number_format($summary->hometax_fee) }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right">
                            <span class="text-sm font-medium text-gray-700">
                                {{ number_format($summary->subscription_total) }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right">
                            @if($summary->tax_invoice_count > 0)
                            <div class="text-sm text-purple-600">
                                <span class="font-medium">{{ number_format($summary->tax_invoice_count) }}건</span>
                                <span class="text-xs text-gray-400">({{ number_format($summary->tax_invoice_amount) }})</span>
                            </div>
                            @else
                            <span class="text-sm text-gray-400">-</span>
                            @endif
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right">
                            <span class="text-sm font-bold text-gray-900">
                                {{ number_format($summary->grand_total) }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-center">
                            <button
                                type="button"
                                onclick="showDetailModal({{ $summary->member_id }}, '{{ addslashes($summary->member->corp_name ?? '') }}')"
                                class="p-2 text-blue-600 hover:bg-blue-50 rounded-lg opacity-30 group-hover:opacity-100 transition"
                                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>
                        </td>
                    </tr>
                @endforeach
            </tbody>
            <!-- 합계 행 -->
            <tfoot class="bg-gray-100 border-t-2 border-gray-300">
                <tr>
                    @if($allTenants ?? false)
                    <td class="px-4 py-4" colspan="2"></td>
                    @endif
                    <td class="px-6 py-4" colspan="2">
                        <span class="text-sm font-bold text-gray-700">합계</span>
                    </td>
                    <td class="px-6 py-4 text-right">
                        <span class="text-sm font-bold text-green-600">{{ number_format($total['bank_account_fee']) }}</span>
                    </td>
                    <td class="px-6 py-4 text-right">
                        <span class="text-sm font-bold text-blue-600">{{ number_format($total['card_fee']) }}</span>
                    </td>
                    <td class="px-6 py-4 text-right">
                        <span class="text-sm font-bold text-orange-600">{{ number_format($total['hometax_fee']) }}</span>
                    </td>
                    <td class="px-6 py-4 text-right">
                        <span class="text-sm font-bold text-gray-700">{{ number_format($total['subscription_total']) }}</span>
                    </td>
                    <td class="px-6 py-4 text-right">
                        <span class="text-sm font-bold text-purple-600">{{ number_format($total['tax_invoice_amount']) }}</span>
                    </td>
                    <td class="px-6 py-4 text-right">
                        <span class="text-lg font-bold text-gray-900">{{ number_format($total['grand_total']) }}원</span>
                    </td>
                    <td class="px-6 py-4"></td>
                </tr>
            </tfoot>
        </table>
    </div>
@endif
