<x-app-layout>

    <x-module-hero
        title="My Payment History"
        description="View your payment records and subscription status"
        icon="fa-credit-card">

        <x-slot name="tabs">
            @can('view institutions')
            <a href="{{ route('dashboard.institutions.index') }}"
               class="module-hero-tab">
                <i class="fas fa-hospital fa-fw"></i> My Institutions
            </a>
            @endcan
            @can('view machines')
            <a href="{{ route('dashboard.machines.index') }}"
               class="module-hero-tab">
                <i class="fas fa-computer fa-fw"></i> My Machines
            </a>
            @endcan
            @can('view payments')
            <a href="{{ route('dashboard.payments.index') }}"
               class="module-hero-tab active-tab">
                <i class="fas fa-credit-card fa-fw"></i> Payment History
            </a>
            @endcan
        </x-slot>

    </x-module-hero>

    {{-- Stats --}}
    <div class="p-6 grid grid-cols-1 md:grid-cols-3 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-coins text-green-600 text-xl"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">Total Paid</p>
                <p class="text-2xl font-bold text-gray-800">{{ number_format($stats['total_paid'] ?? 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-exchange-alt text-blue-600 text-xl"></i>
            </div>
            <div>
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">Transactions</p>
                <p class="text-2xl font-bold text-gray-800">{{ $stats['total_transactions'] ?? 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-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'] ?? 0 }}</p>
            </div>
        </div>
    </div>

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

        {{-- 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>
            </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">Institution</th>
                            <th class="px-4 py-3 text-left th-col-3">Amount</th>
                            <th class="px-4 py-3 text-left th-col-4">Frequency</th>
                            <th class="px-4 py-3 text-left th-col-5">Coverage Until</th>
                            <th class="px-4 py-3 text-left th-col-6">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>
                            <td class="px-4 py-3 text-sm text-gray-600">{{ $payment->created_at->format('M j, Y') }}</td>
                            <td class="px-4 py-3 font-medium text-gray-800">{{ $payment->institution->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 text-sm text-gray-600">
                                {{ $payment->coverage_end ? $payment->coverage_end->format('M j, Y') : '—' }}
                            </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>
                            <td colspan="6" 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 yet
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </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>
            </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">Machine</th>
                            <th class="px-4 py-3 text-left th-col-3">Amount</th>
                            <th class="px-4 py-3 text-left th-col-4">Frequency</th>
                            <th class="px-4 py-3 text-left th-col-5">Coverage Until</th>
                            <th class="px-4 py-3 text-left th-col-6">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>
                            <td class="px-4 py-3 text-sm text-gray-600">{{ $payment->created_at->format('M j, Y') }}</td>
                            <td class="px-4 py-3 font-medium text-gray-800">{{ $payment->machine->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 text-sm text-gray-600">
                                {{ $payment->coverage_end ? $payment->coverage_end->format('M j, Y') : '—' }}
                            </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>
                            <td colspan="6" 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 yet
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

    </div>

</x-app-layout>
