@extends('layouts.app')

@section('title', 'Companies')
@section('heading', 'Tenant Companies')

@section('content')
    <div class="mb-6 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
        <p class="text-slate-600">View and filter all tenant companies.</p>
        <form method="get" class="flex flex-wrap items-center gap-2">
            <input type="text" name="search" value="{{ request('search') }}" placeholder="Search name, email…"
                   class="rounded-lg border border-slate-300 px-3 py-2 text-sm focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
            <select name="status" class="rounded-lg border border-slate-300 px-3 py-2 text-sm focus:border-slate-500 focus:outline-none js-select2" style="width: 150px">
                <option value="">All statuses</option>
                <option value="active" {{ request('status') === 'active' ? 'selected' : '' }}>Active</option>
                <option value="inactive" {{ request('status') === 'inactive' ? 'selected' : '' }}>Inactive</option>
                <option value="suspended" {{ request('status') === 'suspended' ? 'selected' : '' }}>Suspended</option>
            </select>
            <button type="submit" class="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-white">Filter</button>
        </form>
    </div>

    <x-card>
        <div class="overflow-x-auto">
            <x-table>
                <x-slot name="head">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Company</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Email</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Plan</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Licence expiry</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Actions</th>
                    </tr>
                </x-slot>
                @forelse($companies as $c)
                    <tr class="hover:bg-slate-50">
                        <td class="whitespace-nowrap px-4 py-3">
                            <span class="font-medium text-slate-900">{{ $c->name }}</span>
                            @if($c->short_name)
                                <span class="ml-1 text-slate-500">({{ $c->short_name }})</span>
                            @endif
                        </td>
                        <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $c->email ?? '—' }}</td>
                        <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $c->subscriptionPlan?->name ?? '—' }}</td>
                        <td class="whitespace-nowrap px-4 py-3">
                            <x-badge :status="$c->status">{{ $c->status ?? '—' }}</x-badge>
                        </td>
                        <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $c->licence_expire_on ? $c->licence_expire_on->format('d M Y') : '—' }}</td>
                        <td class="whitespace-nowrap px-4 py-3">
                            <div class="flex flex-wrap gap-2">
                                <a href="{{ route('superadmin.companies.edit', $c) }}" class="text-sm font-medium text-primary hover:underline">Open details</a>
                                <a href="{{ route('superadmin.companies.change-plan', $c) }}" class="text-sm font-medium text-slate-600 hover:underline">Change plan</a>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="6" class="px-4 py-8 text-center text-sm text-slate-500">No companies found.</td>
                    </tr>
                @endforelse
            </x-table>
        </div>
        @if($companies->hasPages())
            <div class="mt-4 border-t border-slate-200 pt-4">
                {{ $companies->links() }}
            </div>
        @endif
    </x-card>
@endsection
