<x-filament-panels::page>
    {{-- Date Range Filters --}}
    <div class="flex flex-wrap items-end gap-4 mb-6">
        <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">From</label>
            <input type="date" wire:model.live="dateFrom"
                class="block rounded-lg border-gray-300 shadow-sm focus:border-primary-500 focus:ring-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:text-gray-200 text-sm" />
        </div>
        <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">To</label>
            <input type="date" wire:model.live="dateTo"
                class="block rounded-lg border-gray-300 shadow-sm focus:border-primary-500 focus:ring-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:text-gray-200 text-sm" />
        </div>
    </div>

    {{-- KPI Cards --}}
    @php
        $revenue = $this->getRevenueOverview();
        $expenses = $this->getExpenseOverview();
    @endphp

    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
        <x-filament::section>
            <div class="text-center">
                <p class="text-sm font-medium text-gray-500 dark:text-gray-400">Total Revenue</p>
                <p class="text-2xl font-bold text-gray-900 dark:text-white mt-1">{{ number_format($revenue['total_collected'], 2) }}</p>
                <p class="text-xs text-green-600 mt-1">{{ $revenue['collection_rate'] }}% collection rate</p>
            </div>
        </x-filament::section>

        <x-filament::section>
            <div class="text-center">
                <p class="text-sm font-medium text-gray-500 dark:text-gray-400">Outstanding</p>
                <p class="text-2xl font-bold text-amber-600 mt-1">{{ number_format($revenue['total_outstanding'], 2) }}</p>
                <p class="text-xs text-red-600 mt-1">{{ number_format($revenue['total_overdue'], 2) }} overdue</p>
            </div>
        </x-filament::section>

        <x-filament::section>
            <div class="text-center">
                <p class="text-sm font-medium text-gray-500 dark:text-gray-400">Total Expenses</p>
                <p class="text-2xl font-bold text-gray-900 dark:text-white mt-1">{{ number_format($expenses['total_approved'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-1">{{ number_format($expenses['total_pending'], 2) }} pending</p>
            </div>
        </x-filament::section>

        <x-filament::section>
            <div class="text-center">
                <p class="text-sm font-medium text-gray-500 dark:text-gray-400">Net Position</p>
                <p class="text-2xl font-bold {{ $revenue['net_position'] >= 0 ? 'text-green-600' : 'text-red-600' }} mt-1">
                    {{ number_format($revenue['net_position'], 2) }}
                </p>
                <p class="text-xs text-gray-500 mt-1">Revenue − Expenses</p>
            </div>
        </x-filament::section>
    </div>

    {{-- Export Buttons --}}
    <div class="flex flex-wrap gap-3 mb-8">
        <x-filament::button wire:click="exportPaymentHistory" icon="heroicon-o-arrow-down-tray" color="info" size="sm">
            Export Payment History
        </x-filament::button>
        <x-filament::button wire:click="exportExpenseSummary" icon="heroicon-o-arrow-down-tray" color="info" size="sm">
            Export Expense Summary
        </x-filament::button>
        <x-filament::button wire:click="exportDuesCollection" icon="heroicon-o-arrow-down-tray" color="info" size="sm">
            Export Dues Collection
        </x-filament::button>
    </div>

    {{-- Dues Collection Section --}}
    <x-filament::section heading="Dues Collection Report" icon="heroicon-o-banknotes" collapsible>
        <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Filter by Financial Item</label>
            <select wire:model.live="financialItemId"
                class="block rounded-lg border-gray-300 shadow-sm focus:border-primary-500 focus:ring-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:text-gray-200 text-sm">
                <option value="">All Items</option>
                @foreach($this->getFinancialItems() as $id => $title)
                    <option value="{{ $id }}">{{ $title }}</option>
                @endforeach
            </select>
        </div>

        @php $duesCollection = $this->getDuesCollection(); @endphp

        @if(count($duesCollection) > 0)
            <div class="overflow-x-auto">
                <table class="w-full text-sm text-left">
                    <thead class="text-xs text-gray-500 uppercase bg-gray-50 dark:bg-gray-800 dark:text-gray-400">
                        <tr>
                            <th class="px-4 py-3">Member</th>
                            <th class="px-4 py-3">Item</th>
                            <th class="px-4 py-3 text-right">Due</th>
                            <th class="px-4 py-3 text-right">Paid</th>
                            <th class="px-4 py-3 text-right">Balance</th>
                            <th class="px-4 py-3">Status</th>
                            <th class="px-4 py-3">Due Date</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
                        @foreach(array_slice($duesCollection, 0, 50) as $row)
                            <tr class="hover:bg-gray-50 dark:hover:bg-gray-800/50">
                                <td class="px-4 py-3 font-medium text-gray-900 dark:text-white">{{ $row['member_name'] }}</td>
                                <td class="px-4 py-3 text-gray-600 dark:text-gray-400">{{ $row['item_title'] }}</td>
                                <td class="px-4 py-3 text-right">{{ number_format($row['amount_due'], 2) }}</td>
                                <td class="px-4 py-3 text-right text-green-600">{{ number_format($row['amount_paid'], 2) }}</td>
                                <td class="px-4 py-3 text-right {{ $row['balance'] > 0 ? 'text-red-600 font-medium' : 'text-green-600' }}">
                                    {{ number_format($row['balance'], 2) }}
                                </td>
                                <td class="px-4 py-3">
                                    <span @class([
                                        'inline-flex items-center px-2 py-1 rounded-md text-xs font-medium',
                                        'bg-green-50 text-green-700 dark:bg-green-500/10 dark:text-green-400' => $row['status'] === 'paid',
                                        'bg-amber-50 text-amber-700 dark:bg-amber-500/10 dark:text-amber-400' => $row['status'] === 'pending',
                                        'bg-red-50 text-red-700 dark:bg-red-500/10 dark:text-red-400' => $row['status'] === 'overdue',
                                        'bg-gray-50 text-gray-700 dark:bg-gray-500/10 dark:text-gray-400' => !in_array($row['status'], ['paid', 'pending', 'overdue']),
                                    ])>
                                        {{ ucfirst($row['status']) }}
                                    </span>
                                </td>
                                <td class="px-4 py-3 text-gray-600 dark:text-gray-400">
                                    {{ $row['due_date'] ?? '—' }}
                                    @if($row['days_overdue'] > 0)
                                        <span class="text-red-500 text-xs ml-1">({{ $row['days_overdue'] }}d overdue)</span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            @if(count($duesCollection) > 50)
                <p class="mt-3 text-sm text-gray-500 dark:text-gray-400">
                    Showing 50 of {{ count($duesCollection) }} records. Export for the full list.
                </p>
            @endif
        @else
            <div class="text-center py-8">
                <x-filament::icon icon="heroicon-o-check-circle" class="mx-auto w-10 h-10 text-gray-400" />
                <p class="mt-2 text-gray-500 dark:text-gray-400">No dues collection records found.</p>
            </div>
        @endif
    </x-filament::section>
</x-filament-panels::page>
