@use('Illuminate\Support\Str')

<x-layouts.app title="Logs do monitor" subtitle="Execuções do NFeDistribuicaoDFe em todas as empresas">
    <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">Quando</th>
                        <th class="th">Empresa</th>
                        <th class="th">Origem</th>
                        <th class="th">Resultado</th>
                        <th class="th text-right">Notas</th>
                        <th class="th text-right">Eventos</th>
                        <th class="th text-right">Req.</th>
                        <th class="th text-right">NSU</th>
                        <th class="th">Mensagem</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 bg-white">
                    @forelse ($logs as $log)
                        <tr class="transition hover:bg-slate-50">
                            <td class="td tabular whitespace-nowrap">{{ $log->created_at->format('d/m/Y H:i') }}</td>
                            <td class="td">
                                <a href="{{ route('master.empresas.show', $log->company_id) }}"
                                   class="font-medium text-brand-700 hover:text-brand-800">
                                    {{ Str::limit($log->company?->razao_social, 30) }}
                                </a>
                            </td>
                            <td class="td capitalize">{{ $log->origem }}</td>
                            <td class="td">
                                <span class="badge {{ match ($log->status) {
                                    'success' => 'bg-emerald-50 text-emerald-700 ring-1 ring-emerald-200',
                                    'error' => 'bg-rose-50 text-rose-700 ring-1 ring-rose-200',
                                    default => 'bg-slate-100 text-slate-600',
                                } }}">{{ $log->statusLabel() }}</span>
                            </td>
                            <td class="td tabular text-right">{{ $log->docs_novos }}</td>
                            <td class="td tabular text-right">{{ $log->eventos_novos }}</td>
                            <td class="td tabular text-right">{{ $log->requisicoes }}</td>
                            <td class="td tabular text-right">{{ $log->nsu_final }}</td>
                            <td class="td max-w-xs truncate text-slate-500" title="{{ $log->mensagem }}">{{ $log->mensagem }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="9" class="px-4 py-16 text-center text-sm text-slate-500">
                                Nenhuma sincronização registrada até agora.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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