@props([
    'action' => null,
    'fields' => [],
    'clearUrl' => null,
    'showPerPage' => true,
    'hidden' => [],
])

@php
    $action = $action ?? url()->current();
    $clearUrl = $clearUrl ?? url()->current();
    $hasFilters = collect(request()->query())->except(['page'])->filter(fn ($v) => $v !== null && $v !== '')->isNotEmpty();
@endphp

<form method="GET" action="{{ $action }}" class="erp-index-filters w-100" data-erp-index-filters>
    <div class="d-flex flex-wrap align-items-end gap-2">
        @foreach ($fields as $field)
            @php
                $type = $field['type'] ?? 'text';
                $name = $field['name'] ?? 'q';
                $label = $field['label'] ?? null;
                $width = $field['width'] ?? 'min(16rem, 100%)';
            @endphp
            <div class="erp-index-filters__field" style="flex: 1 1 {{ $width }}; max-width: {{ $width }}; min-width: 9rem;">
                @if ($type === 'search')
                    <x-ui.form.input
                        :name="$name"
                        :label="$label ?? __('common.search')"
                        :value="request($name, $field['value'] ?? null)"
                        :placeholder="$field['placeholder'] ?? __('common.search')"
                    />
                @elseif ($type === 'select')
                    <x-ui.form.select
                        :name="$name"
                        :label="$label"
                        :options="$field['options'] ?? []"
                        :selected="request($name)"
                        data-auto-submit="1"
                    />
                @elseif ($type === 'search-select')
                    <x-ui.form.search-select
                        :name="$name"
                        :label="$label"
                        :lookup="$field['lookup'] ?? null"
                        :options="$field['options'] ?? []"
                        :selected="request($name)"
                        :placeholder="$field['placeholder'] ?? __('common.select')"
                        data-auto-submit="1"
                        optional
                    />
                @elseif ($type === 'date')
                    <x-ui.form.input
                        type="date"
                        :name="$name"
                        :label="$label"
                        :value="request($name, $field['value'] ?? null)"
                        :required="$field['required'] ?? false"
                    />
                @endif
            </div>
        @endforeach

        @if ($showPerPage)
            <div class="erp-index-filters__field" style="flex: 0 0 7.5rem; min-width: 7.5rem;">
                <x-ui.form.select
                    name="per_page"
                    :label="__('common.per_page')"
                    :options="[
                        25 => '25',
                        50 => '50',
                        100 => '100',
                    ]"
                    :selected="request('per_page', 25)"
                    data-auto-submit="1"
                />
            </div>
        @endif

        <div class="d-flex flex-wrap align-items-center gap-2 pb-1">
            <x-ui.buttons.secondary type="submit" size="sm" icon="fe fe-filter">
                {{ __('common.filter') }}
            </x-ui.buttons.secondary>
            @if ($hasFilters)
                <x-ui.buttons.secondary :href="$clearUrl" size="sm" icon="fe fe-x">
                    {{ __('common.clear_filters') }}
                </x-ui.buttons.secondary>
            @endif
        </div>
    </div>

    {{-- Preserve sort when submitting filters --}}
    @foreach (['sort', 'dir'] as $keep)
        @if (request()->filled($keep))
            <input type="hidden" name="{{ $keep }}" value="{{ request($keep) }}">
        @endif
    @endforeach

    {{-- Extra hidden fields the caller needs preserved (e.g. focus/view params) --}}
    @foreach ($hidden as $hiddenName => $hiddenValue)
        @if ($hiddenValue !== null && $hiddenValue !== '')
            <input type="hidden" name="{{ $hiddenName }}" value="{{ $hiddenValue }}">
        @endif
    @endforeach
</form>
