<x-layouts::app :title="__('School Billing Profile - ' . ($tenant->school_name ?? $tenant->id))">
    <div class="space-y-6">
        <div>
            <a href="{{ route('admin.billing.baddebts.index') }}" class="inline-flex items-center rounded-xl border border-slate-200 bg-slate-100 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-200">
                <i class="fas fa-arrow-left mr-2"></i> {{ __('Back to Debtor List') }}
            </a>
        </div>

        <div class="grid gap-6 xl:grid-cols-3">
            <flux:card class="xl:col-span-1">
                <div class="text-center space-y-4">
                    <div class="mx-auto flex h-16 w-16 items-center justify-center rounded-full bg-slate-100 text-sky-600">
                        <i class="fas fa-school text-2xl"></i>
                    </div>
                    <div>
                        <h4 class="text-xl font-semibold text-slate-900">{{ $tenant->school_name ?? 'School Profile' }}</h4>
                        <p class="text-sm text-slate-500">ID: {{ $tenant->id }}</p>
                    </div>
                </div>

                <div class="mt-6 space-y-4">
                    <div>
                        <p class="text-xs uppercase tracking-[0.2em] text-sky-600">Account Status</p>
                        <flux:badge color="{{ $tenant->is_active ? 'success' : 'danger' }}" class="mt-2">{{ $tenant->is_active ? 'ENABLED' : 'DISABLED' }}</flux:badge>
                    </div>
                    <div>
                        <p class="text-xs uppercase tracking-[0.2em] text-rose-600">Pending Balance</p>
                        <p class="mt-2 text-3xl font-semibold text-rose-600">${{ number_format($balance, 2) }}</p>
                    </div>
                    <div class="space-y-2 text-left text-sm text-slate-700">
                        <p><strong>Domain:</strong> {{ $tenant->domains->first()->domain ?? 'N/A' }}</p>
                        <p><strong>Subscription Ends:</strong> {{ $tenant->subscription_ends_at ? date('M d, Y', strtotime($tenant->subscription_ends_at)) : 'N/A' }}</p>
                    </div>

                    <div class="space-y-2">
                        <a href="{{ route('admin.billing.baddebts.statement', $tenant) }}" class="inline-flex w-full items-center justify-center rounded-xl bg-sky-600 px-4 py-3 text-sm font-semibold text-white hover:bg-sky-700">
                            <i class="fas fa-file-invoice mr-2"></i> {{ __('View Full Statement') }}
                        </a>
                        <form action="{{ route('admin.billing.toggle', $tenant) }}" method="POST">
                            @csrf
                            <button type="submit" class="inline-flex w-full items-center justify-center rounded-xl border px-4 py-3 text-sm font-semibold text-slate-900 hover:bg-slate-100 {{ $tenant->is_active ? 'border-rose-500 text-rose-700' : 'border-emerald-500 text-emerald-700' }}">
                                <i class="fas fa-{{ $tenant->is_active ? 'pause' : 'play' }} mr-2"></i>
                                {{ $tenant->is_active ? __('Disable Account') : __('Enable Account') }}
                            </button>
                        </form>
                    </div>
                </div>
            </flux:card>

            <div class="xl:col-span-2 space-y-6">
                <flux:card>
                    <div class="flex items-center justify-between gap-2 mb-4">
                        <flux:heading size="md">{{ __('Invoice History') }}</flux:heading>
                        <a href="{{ route('admin.billing.index') }}?tenant={{ $tenant->id }}" class="text-sm font-semibold text-slate-700 hover:text-slate-900">{{ __('See All') }}</a>
                    </div>
                    <div class="overflow-x-auto">
                        <table class="min-w-full table-reset divide-y divide-slate-200">
                            <thead class="bg-slate-50">
                                <tr>
                                    <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wider text-slate-500">ID</th>
                                    <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wider text-slate-500">Description</th>
                                    <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wider text-slate-500">Amount</th>
                                    <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wider text-slate-500">Date</th>
                                    <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wider text-slate-500">Status</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-slate-200">
                                @forelse($tenant->invoices->sortByDesc('created_at')->take(10) as $invoice)
                                    <tr class="hover:bg-slate-50">
                                        <td class="px-4 py-3 text-sm text-slate-700">#{{ $invoice->id }}</td>
                                        <td class="px-4 py-3 text-sm text-slate-700">{{ $invoice->description }}</td>
                                        <td class="px-4 py-3 text-sm font-semibold text-slate-900">${{ number_format($invoice->amount, 2) }}</td>
                                        <td class="px-4 py-3 text-sm text-slate-700">{{ $invoice->created_at->format('M d, Y') }}</td>
                                        <td class="px-4 py-3 text-sm">
                                            <flux:badge color="{{ $invoice->status == 'paid' ? 'success' : 'warning' }}">{{ strtoupper($invoice->status) }}</flux:badge>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="5" class="px-4 py-8 text-center text-sm text-slate-500">{{ __('No invoices found for this school.') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </flux:card>

                <flux:card class="border-sky-200 border">
                    <flux:heading size="sm" class="text-sky-700">{{ __('Manual Billing') }}</flux:heading>
                    <flux:text class="text-slate-600">{{ __('Need to generate a one-time invoice or adjustment fee for this school?') }}</flux:text>
                    <a href="{{ route('admin.billing.index') }}?tenant={{ $tenant->id }}&action=create" class="inline-flex items-center rounded-xl bg-sky-600 px-4 py-3 text-sm font-semibold text-white hover:bg-sky-700">
                        <i class="fas fa-plus-circle mr-2"></i> {{ __('Generate New Invoice') }}
                    </a>
                </flux:card>
            </div>
        </div>
    </div>
</x-layouts::app>
