@php
    $baseIndexUrl = route('administracion.crud.index', ['entity' => $entity]);
    $historyBienLabel = trim((string) data_get($historyBien, 'codigo', ''));
    $historyBienDescripcion = trim((string) data_get($historyBien, 'descripcion', ''));
    $historyBienTitle = trim($historyBienLabel . ($historyBienDescripcion !== '' ? ' - ' . $historyBienDescripcion : ''));
@endphp

<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-2 mb-3">
    <div>
        <h1 class="h3 mb-0">{{ $historyMode ? 'Variacion de precios' : 'Listado de ' . $entityPluralLabel }}</h1>
        @if ($historyMode && $historyBienTitle !== '')
            <div class="text-muted small">Bien: {{ $historyBienTitle }}</div>
        @endif
    </div>
    <div class="d-flex gap-2">
        <a href="{{ route('administracion.index') }}" class="btn btn-outline-secondary">Volver a Adm.</a>
        @if ($historyMode)
            <a href="{{ $baseIndexUrl }}" class="btn btn-outline-secondary">Volver a Precios por Bien</a>
        @endif
        @if (! ($isReadOnly ?? false))
            @if (! $historyMode)
                <a class="btn btn-outline-primary" href="{{ route('administracion.crud.precios-por-bien.bulk-form') }}">Asignacion masiva</a>
            @endif
            <button
                class="btn btn-brand"
                type="button"
                id="openCreateModalBtn"
                @if ($historyMode)
                    data-selected-bien-id="{{ data_get($historyBien, 'id') }}"
                @endif
            >Nuevo registro</button>
        @endif
    </div>
</div>

<form id="crudSearchForm" class="crud-card mb-3" method="GET" action="{{ $baseIndexUrl }}" autocomplete="off">
    @if ($historyMode)
        <input type="hidden" name="bien_id" value="{{ data_get($historyBien, 'id') }}">
    @endif
    <div class="card-body p-3 d-flex flex-column flex-md-row gap-2 align-items-md-center">
        <div class="input-group">
            <input
                id="crudSearchInput"
                type="text"
                class="form-control"
                name="q"
                value="{{ $search ?? '' }}"
                autocomplete="off"
                placeholder="{{ $searchPlaceholder ?? ('Buscar en ' . $entityPluralLabel) }}"
            >
        </div>
        <div class="d-flex flex-column flex-sm-row gap-2 ms-md-auto">
            <select class="form-select" name="per_page" onchange="this.form.submit()" aria-label="Resultados por pagina" style="min-width: 11rem;">
                @foreach ([10, 50, 100] as $pageSize)
                    <option value="{{ $pageSize }}" @selected(($perPage ?? 10) === $pageSize)>
                        {{ $pageSize }} por pagina
                    </option>
                @endforeach
            </select>
            <button class="btn btn-brand" type="submit">Buscar</button>
            <a class="btn btn-outline-secondary" href="{{ $historyMode ? route('administracion.crud.index', ['entity' => $entity, 'bien_id' => data_get($historyBien, 'id'), 'per_page' => $perPage ?? 10]) : route('administracion.crud.index', ['entity' => $entity, 'per_page' => $perPage ?? 10]) }}">Limpiar</a>
        </div>
    </div>
</form>

@if (filled($search ?? ''))
    <p class="text-muted small mb-3">Buscando por: '{{ $search }}'</p>
@endif