<x-layouts::app :title="__('School Details: ' . ($tenant->school_name ?: $tenant->tenant_id))">
    <div class="space-y-6">
        <div class="grid gap-6 lg:grid-cols-2">
            <flux:card>
                <flux:heading size="lg">{{ __('General Information') }}</flux:heading>
                <ul class="mt-6 space-y-4 text-sm text-slate-700">
                    <li class="flex items-center justify-between rounded-2xl border border-slate-200 bg-slate-50 p-4">
                        <span class="font-semibold">{{ __('Tenant ID') }}</span>
                        <span>{{ $tenant->tenant_id }}</span>
                    </li>
                    <li class="flex items-center justify-between rounded-2xl border border-slate-200 bg-slate-50 p-4">
                        <span class="font-semibold">{{ __('School Name') }}</span>
                        <span>{{ $tenant->school_name ?: 'N/A' }}</span>
                    </li>
                    <li class="flex items-center justify-between rounded-2xl border border-slate-200 bg-slate-50 p-4">
                        <span class="font-semibold">{{ __('Status') }}</span>
                        <flux:badge color="{{ $tenant->is_active ? 'success' : 'danger' }}">{{ $tenant->is_active ? __('Active') : __('Suspended') }}</flux:badge>
                    </li>
                    <li class="flex items-center justify-between rounded-2xl border border-slate-200 bg-slate-50 p-4">
                        <span class="font-semibold">{{ __('Subscription Ends At') }}</span>
                        <span>{{ $tenant->subscription_ends_at ? $tenant->subscription_ends_at->format('M d, Y') : 'N/A' }}</span>
                    </li>
                    <li class="flex items-center justify-between rounded-2xl border border-slate-200 bg-slate-50 p-4">
                        <span class="font-semibold">{{ __('Created At') }}</span>
                        <span>{{ $tenant->created_at->format('M d, Y') }}</span>
                    </li>
                </ul>

                <div class="mt-6 text-center">
                    <a href="{{ route('admin.tenants.edit', $tenant->tenant_id) }}" class="inline-flex items-center justify-center rounded-xl bg-sky-600 px-5 py-3 text-sm font-semibold text-white hover:bg-sky-700">
                        <i class="fas fa-edit mr-2"></i> {{ __('Edit School Info') }}
                    </a>
                </div>
            </flux:card>

            <flux:card>
                <div class="flex items-start justify-between gap-4">
                    <div>
                        <flux:heading size="lg">{{ __('Financial Summary') }}</flux:heading>
                    </div>
                    <a href="{{ route('admin.billing.baddebts.show', $tenant->tenant_id) }}" class="inline-flex items-center rounded-xl border border-slate-200 bg-white px-4 py-2 text-sm font-semibold text-slate-900 hover:bg-slate-100">
                        {{ __('View Full Statement') }}
                    </a>
                </div>

                <div class="grid gap-4 mt-6 grid-cols-1 sm:grid-cols-3">
                    <div class="rounded-2xl border border-slate-200 bg-rose-50 p-4 text-center">
                        <p class="text-sm uppercase tracking-[0.2em] text-rose-700">{{ __('Pending Balance') }}</p>
                        <p class="mt-3 text-2xl font-semibold text-rose-700">${{ number_format($pendingBalance, 2) }}</p>
                    </div>
                    <div class="rounded-2xl border border-slate-200 bg-emerald-50 p-4 text-center">
                        <p class="text-sm uppercase tracking-[0.2em] text-emerald-700">{{ __('Total Paid') }}</p>
                        <p class="mt-3 text-2xl font-semibold text-emerald-700">${{ number_format($totalPaid, 2) }}</p>
                    </div>
                    <div class="rounded-2xl border border-slate-200 bg-sky-50 p-4 text-center">
                        <p class="text-sm uppercase tracking-[0.2em] text-sky-700">{{ __('Account Surplus') }}</p>
                        <p class="mt-3 text-2xl font-semibold text-sky-700">${{ number_format($surplus, 2) }}</p>
                    </div>
                </div>

                <div class="mt-6">
                    <flux:heading size="md">{{ __('Recent Invoices') }}</flux:heading>
                    @if($tenant->invoices->count() > 0)
                        <div class="overflow-x-auto mt-4">
                            <table class="min-w-full table-reset divide-y divide-slate-200 text-sm">
                                <thead class="bg-slate-50 text-slate-600 uppercase tracking-wider">
                                    <tr>
                                        <th class="px-4 py-3 text-left">{{ __('ID') }}</th>
                                        <th class="px-4 py-3 text-left">{{ __('Amount') }}</th>
                                        <th class="px-4 py-3 text-left">{{ __('Status') }}</th>
                                        <th class="px-4 py-3 text-left">{{ __('Due Date') }}</th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white divide-y divide-slate-200">
                                    @foreach($tenant->invoices->sortByDesc('created_at')->take(5) as $invoice)
                                        <tr class="hover:bg-slate-50">
                                            <td class="px-4 py-3 text-slate-700">#{{ $invoice->id }}</td>
                                            <td class="px-4 py-3 text-slate-900">${{ number_format($invoice->amount, 2) }}</td>
                                            <td class="px-4 py-3">
                                                <flux:badge color="{{ $invoice->status == 'paid' ? 'success' : 'warning' }}">{{ ucfirst($invoice->status) }}</flux:badge>
                                            </td>
                                            <td class="px-4 py-3 text-slate-700">{{ \\Carbon\\Carbon::parse($invoice->due_date)->format('M d, Y') }}</td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    @else
                        <p class="mt-4 text-sm text-slate-500">{{ __('No invoices found for this tenant.') }}</p>
                    @endif
                </div>
            </flux:card>
        </div>

        <flux:card>
            <flux:heading size="lg">{{ __('Associated Domains') }}</flux:heading>
            <div class="mt-6 space-y-4">
                @if($tenant->domains->count() > 0)
                    <div class="overflow-x-auto">
                        <table class="min-w-full table-reset divide-y divide-slate-200 text-sm">
                            <thead class="bg-slate-50 text-slate-600 uppercase tracking-wider">
                                <tr>
                                    <th class="px-4 py-3 text-left">{{ __('Domain') }}</th>
                                    <th class="px-4 py-3 text-left">{{ __('Access Link') }}</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-slate-200">
                                @foreach($tenant->domains as $domain)
                                    <tr class="hover:bg-slate-50">
                                        <td class="px-4 py-3 text-slate-700">{{ $domain->domain }}</td>
                                        <td class="px-4 py-3">
                                            <a href="http://{{ $domain->domain }}" target="_blank" class="inline-flex items-center rounded-xl border border-slate-200 bg-white px-4 py-2 text-sm font-semibold text-slate-900 hover:bg-slate-100">
                                                <i class="fas fa-external-link-alt mr-2"></i> {{ __('Visit Site') }}
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <flux:text>{{ __('No domains associated with this tenant.') }}</flux:text>
                @endif
            </div>
        </flux:card>

        <div class="flex justify-start">
            <a href="{{ route('admin.tenants.index') }}" class="inline-flex items-center rounded-xl border border-slate-200 bg-slate-100 px-5 py-3 text-sm font-semibold text-slate-700 hover:bg-slate-200">
                <i class="fas fa-arrow-left mr-2"></i> {{ __('Back to Schools') }}
            </a>
        </div>
    </div>
</x-layouts::app>
