@extends('layouts.app')
@section('title', $config['label'])
@section('content')

{{-- Cabeçalho --}}
<div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:.75rem;margin-bottom:1rem">
    <div>
        <h2 style="margin:0">{{ $config['icone'] }} {{ $config['label'] }}</h2>
        <p style="margin:.2rem 0 0;color:var(--muted);font-size:.85rem">{{ $config['descricao'] }}</p>
    </div>
    <div style="display:flex;gap:.5rem;flex-wrap:wrap">
        @if(!$config['somente_leitura'])
            <a href="{{ route('tabelas.create', $tabela) }}" class="btn btn-primary">+ Novo Registro</a>
        @endif
        <a href="{{ route('tabelas.index') }}" class="btn btn-secondary">← Tabelas</a>
    </div>
</div>

{{-- Busca + paginação --}}
<form method="GET" action="{{ route('tabelas.show', $tabela) }}"
      style="display:flex;gap:.5rem;align-items:center;margin-bottom:1rem;flex-wrap:wrap">
    <input type="text" name="q" value="{{ $busca }}"
           placeholder="Buscar..."
           style="padding:.4rem .7rem;border:1px solid #ddd;border-radius:var(--radius);font-size:.9rem;min-width:220px">
    <select name="por_pagina" onchange="this.form.submit()"
            style="padding:.4rem .6rem;border:1px solid #ddd;border-radius:var(--radius);font-size:.9rem">
        @foreach([10,25,50,100] as $n)
            <option value="{{ $n }}" {{ $porPag==$n ? 'selected':'' }}>{{ $n }} / pág.</option>
        @endforeach
    </select>
    <button type="submit" class="btn btn-secondary" style="padding:.4rem .9rem">Buscar</button>
    @if($busca)
        <a href="{{ route('tabelas.show', $tabela) }}" class="btn btn-secondary" style="padding:.4rem .9rem">✕ Limpar</a>
    @endif
    <span style="font-size:.82rem;color:var(--muted);margin-left:.25rem">
        {{ number_format($registros->total()) }} registro(s)
    </span>
</form>

{{-- Tabela de dados --}}
<div style="overflow-x:auto">
    <table class="table" style="min-width:600px;font-size:.82rem">
        <thead>
            <tr>
                @foreach($colunas as $col)
                    <th style="white-space:nowrap">{{ $col }}</th>
                @endforeach
                @if(!$config['somente_leitura'])
                    <th style="width:90px;text-align:center">Ações</th>
                @endif
            </tr>
        </thead>
        <tbody>
            @forelse($registros as $row)
                @php $arr = (array)$row; @endphp
                <tr>
                    @foreach($colunas as $col)
                        <td style="max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"
                            title="{{ $arr[$col] ?? '' }}">
                            {{ $arr[$col] ?? '' }}
                        </td>
                    @endforeach
                    @if(!$config['somente_leitura'])
                        <td style="text-align:center;white-space:nowrap">
                            <a href="{{ route('tabelas.edit', [$tabela, $arr[$config['pk']]]) }}"
                               style="font-size:.78rem;color:var(--blue);text-decoration:none;margin-right:.3rem">✏️</a>
                            <form method="POST"
                                  action="{{ route('tabelas.destroy', [$tabela, $arr[$config['pk']]]) }}"
                                  style="display:inline"
                                  onsubmit="return confirm('Excluir este registro?')">
                                @csrf @method('DELETE')
                                <button type="submit"
                                        style="background:none;border:none;cursor:pointer;font-size:.78rem;color:#C62828;padding:0">🗑️</button>
                            </form>
                        </td>
                    @endif
                </tr>
            @empty
                <tr>
                    <td colspan="{{ count($colunas) + ($config['somente_leitura'] ? 0 : 1) }}"
                        style="text-align:center;color:#aaa;padding:1.5rem">
                        Nenhum registro encontrado.
                    </td>
                </tr>
            @endforelse
        </tbody>
    </table>
</div>

{{-- Paginação --}}
<div style="margin-top:1rem">
    {{ $registros->links() }}
</div>

@endsection
