<div class="space-y-4">

    <div class="card card-pad">
        <div class="grid gap-4 sm:grid-cols-3">
            <div>
                <label class="label" for="e-busca">Buscar</label>
                <input id="e-busca" type="search" wire:model.live.debounce.400ms="busca" class="input"
                       placeholder="Razão social, fantasia ou CNPJ">
            </div>

            <div>
                <label class="label" for="e-status">Situação</label>
                <select id="e-status" wire:model.live="status" class="input">
                    <option value="">Todas</option>
                    <option value="ativa">Ativas</option>
                    <option value="inativa">Inativas</option>
                    <option value="vencida">Assinatura vencida</option>
                </select>
            </div>

            <div>
                <label class="label" for="e-cert">Certificado</label>
                <select id="e-cert" wire:model.live="certificado" class="input">
                    <option value="">Todos</option>
                    <option value="com">Instalado</option>
                    <option value="sem">Sem certificado</option>
                    <option value="vencendo">Vence em 30 dias</option>
                </select>
            </div>
        </div>
    </div>

    <div class="card overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-slate-200">
                <thead class="bg-slate-50">
                    <tr>
                        <th class="th">Empresa</th>
                        <th class="th">CNPJ</th>
                        <th class="th">Plano</th>
                        <th class="th">Certificado</th>
                        <th class="th">Monitor</th>
                        <th class="th text-right">XMLs</th>
                        <th class="th">Última sync</th>
                        <th class="th sr-only">Ações</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 bg-white">
                    @forelse ($empresas as $empresa)
                        <tr wire:key="empresa-{{ $empresa->id }}" class="transition hover:bg-slate-50">
                            <td class="td">
                                <a href="{{ route('master.empresas.show', $empresa) }}" class="font-medium text-slate-900 hover:text-brand-700">
                                    {{ $empresa->razao_social }}
                                </a>
                                @unless ($empresa->is_active)
                                    <span class="badge ml-1 bg-slate-100 text-slate-600">inativa</span>
                                @endunless
                                <p class="text-xs text-slate-500">{{ $empresa->nome_fantasia ?: $empresa->uf }}</p>
                            </td>

                            <td class="td tabular">{{ $empresa->cnpj_formatado }}</td>

                            <td class="td">
                                <span class="capitalize">{{ $empresa->plano }}</span>
                                @if ($empresa->assinatura_expira_em)
                                    <p class="tabular text-xs {{ $empresa->assinatura_expira_em->isPast() ? 'text-rose-600' : 'text-slate-500' }}">
                                        até {{ $empresa->assinatura_expira_em->format('d/m/Y') }}
                                    </p>
                                @endif
                            </td>

                            <td class="td">
                                @if (! $empresa->cert_path)
                                    <span class="badge bg-slate-100 text-slate-600">ausente</span>
                                @elseif ($empresa->certificateExpired())
                                    <span class="badge bg-rose-50 text-rose-700 ring-1 ring-rose-200">vencido</span>
                                @elseif ($empresa->certificateExpiringSoon())
                                    <span class="badge bg-amber-50 text-amber-800 ring-1 ring-amber-200">
                                        {{ $empresa->cert_valid_to->format('d/m/Y') }}
                                    </span>
                                @else
                                    <span class="badge bg-emerald-50 text-emerald-700 ring-1 ring-emerald-200">válido</span>
                                @endif
                            </td>

                            <td class="td">
                                <span class="badge {{ $empresa->monitor_enabled ? 'bg-emerald-50 text-emerald-700 ring-1 ring-emerald-200' : 'bg-slate-100 text-slate-600' }}">
                                    {{ $empresa->monitor_enabled ? 'ativo' : 'pausado' }}
                                </span>
                            </td>

                            <td class="td tabular text-right">{{ number_format($empresa->notas_fiscais_count, 0, ',', '.') }}</td>

                            <td class="td tabular text-xs whitespace-nowrap text-slate-500">
                                {{ $empresa->last_sync_at?->format('d/m/Y H:i') ?: '—' }}
                                @if ($empresa->last_sync_status === 'error')
                                    <span class="block text-rose-600">falhou</span>
                                @endif
                            </td>

                            <td class="td text-right whitespace-nowrap">
                                <a href="{{ route('master.empresas.show', $empresa) }}" class="btn-secondary btn-sm">Abrir</a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="px-4 py-16 text-center">
                                <p class="text-sm font-medium text-slate-700">Nenhuma empresa encontrada</p>
                                <p class="mt-1 text-sm text-slate-500">Ajuste os filtros ou cadastre a primeira empresa.</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if ($empresas->hasPages())
            <div class="border-t border-slate-100 px-4 py-3">{{ $empresas->links() }}</div>
        @endif
    </div>
</div>
