@extends('layouts.super-admin', ['title' => 'Businesses - Booklio Super Admin', 'pageTitle' => 'Businesses', 'active' => 'businesses'])

@section('content')
    <div class="space-y-5">
        @if (session('status'))
            <div class="rounded-lg border border-emerald-100 bg-emerald-50 px-4 py-3 text-sm font-bold text-emerald-700">{{ session('status') }}</div>
        @endif

        <div class="flex flex-col gap-3 lg:flex-row lg:items-end lg:justify-between">
            <p class="text-sm font-semibold text-booklio-muted">Manage all businesses registered on the Booklio SaaS platform.</p>
            <form method="GET" class="grid gap-3 sm:grid-cols-[260px_160px_160px_auto]">
                <input name="q" value="{{ $filters['q'] ?? '' }}" type="search" placeholder="Search business..." class="h-11 rounded-lg border border-booklio-border bg-white px-3.5 text-sm font-medium shadow-soft outline-none focus:border-booklio-primary focus:ring-4 focus:ring-blue-100">
                <select name="plan_id" class="h-11 rounded-lg border border-booklio-border bg-white px-3 text-sm font-bold shadow-soft">
                    <option value="all">All plans</option>
                    @foreach ($plans as $plan)
                        <option value="{{ $plan->id }}" @selected(($filters['plan_id'] ?? 'all') == $plan->id)>{{ $plan->name }}</option>
                    @endforeach
                </select>
                <select name="status" class="h-11 rounded-lg border border-booklio-border bg-white px-3 text-sm font-bold shadow-soft">
                    @foreach (['all' => 'All statuses', 'active' => 'Active', 'trial' => 'Trial', 'suspended' => 'Suspended', 'expired' => 'Expired'] as $value => $label)
                        <option value="{{ $value }}" @selected(($filters['status'] ?? 'all') === $value)>{{ $label }}</option>
                    @endforeach
                </select>
                <x-ui.button type="submit">Filter</x-ui.button>
            </form>
        </div>

        <x-ui.card title="Businesses List" description="Business name, owner, plan, subscription state, revenue, status, and actions.">
            <x-ui.table :headers="['Business name', 'Owner', 'Email', 'Plan', 'Subscription status', 'Created date', 'Revenue', 'Status', 'Actions']">
                @forelse ($businesses as $business)
                    @php
                        $subscription = $business->subscription;
                        $revenue = (float) ($business->paid_revenue ?? 0);
                    @endphp
                    <tr class="hover:bg-slate-50">
                        <td class="font-extrabold">{{ $business->name }}</td>
                        <td class="font-semibold">{{ $business->owner?->name ?? 'No owner' }}</td>
                        <td class="text-booklio-muted">{{ $business->email ?? $business->owner?->email ?? '-' }}</td>
                        <td class="font-bold">{{ $subscription?->plan?->name ?? 'No plan' }}</td>
                        <td><x-ui.badge :variant="$subscription?->status_badge_variant ?? 'muted'">{{ $subscription?->status_label ?? 'None' }}</x-ui.badge></td>
                        <td class="font-semibold">{{ $business->created_at?->format('M d, Y') }}</td>
                        <td class="font-extrabold">${{ number_format($revenue, 2) }}</td>
                        <td><x-ui.badge :variant="app(\App\Services\SuperAdmin\PlatformAdminService::class)->businessStatusVariant($business->status)">{{ Str::headline($business->status) }}</x-ui.badge></td>
                        <td>
                            <div class="flex flex-wrap gap-2">
                                <x-ui.button size="sm" variant="secondary" href="{{ route('super-admin.businesses.show', $business) }}">View</x-ui.button>
                                <form method="POST" action="{{ route('super-admin.businesses.login-as-owner', $business) }}">
                                    @csrf
                                    <x-ui.button type="submit" size="sm" variant="ghost">Login as owner</x-ui.button>
                                </form>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="9" class="text-center text-booklio-muted">No businesses found.</td></tr>
                @endforelse
            </x-ui.table>
            <x-ui.pagination :paginator="$businesses" class="mt-4" />
        </x-ui.card>
    </div>
@endsection
