<x-admin-layout title="Platform Overview">
    <div class="row g-3 mb-4">
        <div class="col-xl-3 col-md-6">
            <div class="stat-card">
                <div>
                    <div class="text-muted small fw-semibold text-uppercase mb-1">Total Companies</div>
                    <div class="fs-3 fw-bold">{{ $stats['total'] }}</div>
                </div>
                <div class="stat-card-icon is-blue"><i class="bi bi-building-fill"></i></div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6">
            <div class="stat-card">
                <div>
                    <div class="text-muted small fw-semibold text-uppercase mb-1">Active</div>
                    <div class="fs-3 fw-bold">{{ $stats['active'] }}</div>
                </div>
                <div class="stat-card-icon is-green"><i class="bi bi-check-circle-fill"></i></div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6">
            <div class="stat-card">
                <div>
                    <div class="text-muted small fw-semibold text-uppercase mb-1">Inactive</div>
                    <div class="fs-3 fw-bold">{{ $stats['inactive'] }}</div>
                </div>
                <div class="stat-card-icon is-violet"><i class="bi bi-pause-circle-fill"></i></div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6">
            <div class="stat-card">
                <div>
                    <div class="text-muted small fw-semibold text-uppercase mb-1">Needs Renewal</div>
                    <div class="fs-3 fw-bold">{{ $stats['needs_renewal'] }}</div>
                </div>
                <div class="stat-card-icon is-amber"><i class="bi bi-hourglass-split"></i></div>
            </div>
        </div>
    </div>

    @if ($expiredCompanies->isNotEmpty() || $expiringSoonCompanies->isNotEmpty())
        <div class="card mb-4">
            <div class="card-body">
                <h5 class="fw-bold mb-3"><i class="bi bi-exclamation-triangle-fill text-warning me-1"></i> Companies Needing Renewal</h5>
                <div class="table-responsive">
                    <table class="table align-middle">
                        <thead>
                            <tr>
                                <th>Company</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($expiredCompanies->concat($expiringSoonCompanies) as $company)
                                <tr>
                                    <td>
                                        <a href="{{ route('admin.companies.edit', $company) }}" class="fw-semibold text-decoration-none">{{ $company->name }}</a>
                                        <div class="text-muted small">Code {{ $company->code }}</div>
                                    </td>
                                    <td><span class="badge {{ $company->expiryBadge()['class'] }}">{{ $company->expiryBadge()['label'] }}</span></td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    @endif

    <div class="card">
        <div class="card-body">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="fw-bold mb-0">All Companies</h5>
                <span class="text-muted small">{{ $companies->count() }} companies</span>
            </div>

            @if ($companies->isEmpty())
                <div class="text-center text-muted py-5">
                    <i class="bi bi-building fs-1 d-block mb-2"></i>
                    No companies yet.
                </div>
            @else
                <div class="table-responsive">
                    <table class="table align-middle datatable">
                        <thead>
                            <tr>
                                <th>Company</th>
                                <th>Status</th>
                                <th class="text-end">Bookings (This Month)</th>
                                <th class="text-end">Calls (This Month)</th>
                                <th class="text-end">Drivers</th>
                                <th class="text-end">Customers</th>
                                <th class="text-end">Vehicles</th>
                                <th class="text-end">Staff</th>
                                <th>Last Booking</th>
                                <th>Valid Until</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($companies as $company)
                                <tr>
                                    <td>
                                        <a href="{{ route('admin.companies.edit', $company) }}" class="fw-semibold text-decoration-none">{{ $company->name }}</a>
                                        <div class="text-muted small">Code {{ $company->code }}</div>
                                    </td>
                                    <td>
                                        <span class="badge {{ $company->is_active ? 'is-green' : 'is-rose' }}">
                                            {{ $company->is_active ? 'Active' : 'Inactive' }}
                                        </span>
                                    </td>
                                    <td class="text-end fw-semibold">{{ $company->bookings_this_month }}</td>
                                    <td class="text-end">{{ $company->calls_this_month }}</td>
                                    <td class="text-end">{{ $company->active_drivers_count }} / {{ $company->drivers_count }}</td>
                                    <td class="text-end">{{ $company->active_customers_count }} / {{ $company->customers_count }}</td>
                                    <td class="text-end">{{ $company->vehicles_count }}</td>
                                    <td class="text-end">{{ $company->users_count }}</td>
                                    <td class="text-muted small">
                                        {{ $company->last_booking_at ? \Illuminate\Support\Carbon::parse($company->last_booking_at)->diffForHumans() : 'Never' }}
                                    </td>
                                    <td class="text-muted small">
                                        <span class="badge {{ $company->expiryBadge()['class'] }}">{{ $company->expiryBadge()['label'] }}</span>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        </div>
    </div>
</x-admin-layout>
