<x-app-layout>

    <x-module-hero
        title="Payments Management"
        description="View and manage all payment transactions across modules"
        icon="fa-credit-card">

        <x-slot name="tabs">
            @can('view payments')
            <a href="{{ route('admin.payments.index') }}"
               class="module-hero-tab {{ request()->routeIs('admin.payments.index') ? 'active-tab' : '' }}">
                <i class="fas fa-list fa-fw"></i> All Payments
            </a>
            @endcan
            @can('configure payments')
            <a href="{{ route('admin.payments.configure') }}"
               class="module-hero-tab {{ request()->routeIs('admin.payments.configure') ? 'active-tab' : '' }}">
                <i class="fas fa-cog fa-fw"></i> Configure
            </a>
            @endcan
        </x-slot>

    </x-module-hero>

    @if(session('success'))
    <div class="px-6 pt-4">
        <div class="p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-check-circle"></i> {{ session('success') }}
        </div>
    </div>
    @endif

    {{-- Stats --}}
    <div class="p-6 grid grid-cols-1 md:grid-cols-4 gap-4">
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
            <div class="w-12 h-12 rounded-full bg-green-100 flex items-center justify-center flex-shrink-0">
                <i class="fas fa-hospital text-green-600 text-xl"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">Institution Revenue</p>
                <p class="text-2xl font-bold text-gray-800">{{ number_format($stats['institution_total'] ?? 0) }} <span class="text-sm font-normal text-gray-500">SSP</span></p>
            </div>
        </div>
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
            <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center flex-shrink-0">
                <i class="fas fa-computer text-blue-600 text-xl"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">Machine Revenue</p>
                <p class="text-2xl font-bold text-gray-800">{{ number_format($stats['machine_total'] ?? 0) }} <span class="text-sm font-normal text-gray-500">SSP</span></p>
            </div>
        </div>
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
            <div class="w-12 h-12 rounded-full bg-yellow-100 flex items-center justify-center flex-shrink-0">
                <i class="fas fa-clock text-yellow-600 text-xl"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">Pending</p>
                <p class="text-2xl font-bold text-gray-800">{{ $stats['pending_count'] ?? 0 }}</p>
            </div>
        </div>
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
            <div class="w-12 h-12 rounded-full bg-red-100 flex items-center justify-center flex-shrink-0">
                <i class="fas fa-times-circle text-red-600 text-xl"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">Failed</p>
                <p class="text-2xl font-bold text-gray-800">{{ $stats['failed_count'] ?? 0 }}</p>
            </div>
        </div>
    </div>

    <div class="px-6 pb-6 space-y-6">

        {{-- Filters --}}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
            <form method="GET" class="flex flex-wrap gap-3 items-end">
                <div class="flex-1 min-w-[220px]">
                    <label class="block text-xs font-medium text-gray-600 mb-1">Search</label>
                    <input type="text" name="search" value="{{ request('search') }}"
                           placeholder="Reference or transaction ID..."
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Status</label>
                    <select name="status" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                        <option value="">All Statuses</option>
                        <option value="successful" {{ request('status') == 'successful' ? 'selected' : '' }}>Successful</option>
                        <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                        <option value="processing" {{ request('status') == 'processing' ? 'selected' : '' }}>Processing</option>
                        <option value="failed" {{ request('status') == 'failed' ? 'selected' : '' }}>Failed</option>
                    </select>
                </div>
                <button type="submit" class="px-4 py-2 bg-green-600 text-white text-sm rounded-lg hover:bg-green-700 transition-colors">
                    <i class="fas fa-search mr-1"></i> Filter
                </button>
                @if(request()->hasAny(['search','status']))
                <a href="{{ route('admin.payments.index') }}" class="px-4 py-2 bg-gray-200 text-gray-700 text-sm rounded-lg hover:bg-gray-300 transition-colors">
                    <i class="fas fa-times mr-1"></i> Clear
                </a>
                @endif
            </form>
        </div>

        {{-- Institution Payments --}}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-200 flex items-center gap-2">
                <i class="fas fa-hospital text-green-600"></i>
                <h3 class="font-semibold text-gray-800">Institution Payments</h3>
                <span class="ml-auto text-xs text-gray-500">{{ $institutionPayments->total() }} records</span>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full table-zebra">
                    <thead>
                        <tr>
                            <th class="px-4 py-3 text-left th-col-1">Date</th>
                            <th class="px-4 py-3 text-left th-col-2">Reference</th>
                            <th class="px-4 py-3 text-left th-col-3">Institution</th>
                            <th class="px-4 py-3 text-left th-col-4">User</th>
                            <th class="px-4 py-3 text-left th-col-5">Amount</th>
                            <th class="px-4 py-3 text-left th-col-6">Frequency</th>
                            <th class="px-4 py-3 text-left th-col-7">Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($institutionPayments as $payment)
                        @php
                            $sc = ['successful'=>'bg-green-100 text-green-800','pending'=>'bg-yellow-100 text-yellow-800','processing'=>'bg-blue-100 text-blue-800','failed'=>'bg-red-100 text-red-800','timeout'=>'bg-orange-100 text-orange-800','cancelled'=>'bg-gray-100 text-gray-800'];
                        @endphp
                        <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                            <td class="px-4 py-3 text-sm text-gray-600">{{ $payment->created_at->format('M j, Y H:i') }}</td>
                            <td class="px-4 py-3"><code class="text-xs bg-gray-100 px-2 py-1 rounded font-mono">{{ $payment->reference }}</code></td>
                            <td class="px-4 py-3 font-medium text-gray-800">{{ $payment->institution->name ?? '—' }}</td>
                            <td class="px-4 py-3 text-sm text-gray-600">{{ $payment->user->name ?? '—' }}</td>
                            <td class="px-4 py-3 font-semibold text-gray-800">{{ $payment->formatted_amount }}</td>
                            <td class="px-4 py-3 text-sm text-gray-600">{{ $payment->frequency_label }}</td>
                            <td class="px-4 py-3">
                                <span class="px-2 py-1 rounded-full text-xs font-semibold {{ $sc[$payment->status] ?? 'bg-gray-100 text-gray-800' }}">
                                    {{ ucfirst($payment->status) }}
                                </span>
                            </td>
                        </tr>
                        @empty
                        <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                            <td colspan="7" class="px-4 py-8 text-center text-gray-500">
                                <i class="fas fa-inbox text-2xl mb-2 block text-gray-300"></i>
                                No institution payments found
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if($institutionPayments->hasPages())
            <div class="px-4 py-3 border-t border-gray-200">
                {{ $institutionPayments->appends(request()->query())->links() }}
            </div>
            @endif
        </div>

        {{-- Machine Payments --}}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-200 flex items-center gap-2">
                <i class="fas fa-computer text-blue-600"></i>
                <h3 class="font-semibold text-gray-800">Machine Payments</h3>
                <span class="ml-auto text-xs text-gray-500">{{ $machinePayments->total() }} records</span>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full table-zebra">
                    <thead>
                        <tr>
                            <th class="px-4 py-3 text-left th-col-1">Date</th>
                            <th class="px-4 py-3 text-left th-col-2">Reference</th>
                            <th class="px-4 py-3 text-left th-col-3">Machine</th>
                            <th class="px-4 py-3 text-left th-col-4">User</th>
                            <th class="px-4 py-3 text-left th-col-5">Amount</th>
                            <th class="px-4 py-3 text-left th-col-6">Frequency</th>
                            <th class="px-4 py-3 text-left th-col-7">Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($machinePayments as $payment)
                        @php
                            $sc = ['successful'=>'bg-green-100 text-green-800','pending'=>'bg-yellow-100 text-yellow-800','processing'=>'bg-blue-100 text-blue-800','failed'=>'bg-red-100 text-red-800','timeout'=>'bg-orange-100 text-orange-800','cancelled'=>'bg-gray-100 text-gray-800'];
                        @endphp
                        <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                            <td class="px-4 py-3 text-sm text-gray-600">{{ $payment->created_at->format('M j, Y H:i') }}</td>
                            <td class="px-4 py-3"><code class="text-xs bg-gray-100 px-2 py-1 rounded font-mono">{{ $payment->reference }}</code></td>
                            <td class="px-4 py-3 font-medium text-gray-800">{{ $payment->machine->name ?? '—' }}</td>
                            <td class="px-4 py-3 text-sm text-gray-600">{{ $payment->user->name ?? '—' }}</td>
                            <td class="px-4 py-3 font-semibold text-gray-800">{{ $payment->formatted_amount }}</td>
                            <td class="px-4 py-3 text-sm text-gray-600">{{ $payment->frequency_label }}</td>
                            <td class="px-4 py-3">
                                <span class="px-2 py-1 rounded-full text-xs font-semibold {{ $sc[$payment->status] ?? 'bg-gray-100 text-gray-800' }}">
                                    {{ ucfirst($payment->status) }}
                                </span>
                            </td>
                        </tr>
                        @empty
                        <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                            <td colspan="7" class="px-4 py-8 text-center text-gray-500">
                                <i class="fas fa-inbox text-2xl mb-2 block text-gray-300"></i>
                                No machine payments found
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if($machinePayments->hasPages())
            <div class="px-4 py-3 border-t border-gray-200">
                {{ $machinePayments->appends(request()->query())->links() }}
            </div>
            @endif
        </div>

    </div>

</x-app-layout>
