@extends('layouts.app')
@section('titulo', $meta['titulo'])
@section('contenido')
<div class="d-flex flex-wrap justify-content-between align-items-center mb-3">
  <h4 class="mb-0"><i class="fa fa-book"></i> {{ $meta['titulo'] }}</h4>
  <a href="{{ route('catalogos.create', $slug) }}" class="btn btn-primario"><i class="fa fa-plus"></i> Nuevo</a>
</div>
<form method="GET" class="mb-3">
  <div class="input-group">
    <input name="q" value="{{ $busqueda }}" class="form-control" placeholder="Buscar por código o nombre">
    <button class="btn btn-outline-primary"><i class="fa fa-search"></i></button>
  </div>
</form>
<div class="card">
  <div class="table-responsive">
    <table class="table table-striped table-hover mb-0 table-sticky">
      <thead><tr>
        @foreach($meta['campos'] as $c)<th>{{ $c[1] }}</th>@endforeach
        <th class="text-end">Acciones</th>
      </tr></thead>
      <tbody>
      @forelse($registros as $r)
        <tr>
          @foreach($meta['campos'] as $c)
            @php $val = $r->{$c[0]} ?? null; @endphp
            <td>
              @if($c[2]==='bool')
                <span class="badge text-bg-{{ $val ? 'success' : 'secondary' }}">{{ $val ? 'Sí' : 'No' }}</span>
              @elseif($c[2]==='color' && $val)
                <span class="badge" style="background: {{ $val }}; color:#fff;">{{ $val }}</span>
              @else
                {{ $val }}
              @endif
            </td>
          @endforeach
          <td class="text-end">
            <a href="{{ route('catalogos.edit', [$slug, $r->id]) }}" class="btn btn-sm btn-outline-primary"><i class="fa fa-pen"></i></a>
            <form action="{{ route('catalogos.destroy', [$slug, $r->id]) }}" method="POST" class="d-inline" onsubmit="return confirm('¿Eliminar?');">
              @csrf @method('DELETE')
              <button class="btn btn-sm btn-outline-danger"><i class="fa fa-trash"></i></button>
            </form>
          </td>
        </tr>
      @empty
        <tr><td colspan="{{ count($meta['campos'])+1 }}" class="text-center text-muted py-4">Sin registros</td></tr>
      @endforelse
      </tbody>
    </table>
  </div>
</div>
<div class="mt-2">{{ $registros->links() }}</div>
@endsection
