{{--
    Partial: иерархический список марок/моделей с алфавитным фильтром и аккордеоном.
    Переменные из родителя: $section, $items
--}}
@php
    $letters = $items
        ->map(fn($m) => mb_strtoupper(mb_substr($m->value, 0, 1, 'UTF-8')))
        ->unique()->sort()->values();
@endphp

{{-- Алфавитная навигация --}}
<div class="alpha-nav-wrap mb-4">
    <div class="alpha-nav">
        <button class="alpha-btn active" data-letter="" title="Показать все"><span>Все</span></button>
        @foreach($letters as $letter)
            <button class="alpha-btn" data-letter="{{ $letter }}" title="{{ $letter }}"><span>{{ $letter }}</span></button>
        @endforeach
    </div>
</div>

<div class="d-flex align-items-center mb-3">
    <span class="text-muted small">
        Показано: <strong id="visible-count">{{ $items->count() }}</strong> из {{ $items->count() }}
    </span>
</div>

{{-- Карточки марок --}}
@forelse($items as $parent)
    <div class="card card-primary card-outline mb-2 make-card collapsed-card"
         data-letter="{{ mb_strtoupper(mb_substr($parent->value, 0, 1, 'UTF-8')) }}">
        <div class="card-header" style="cursor:pointer" data-card-widget="collapse">
            <h3 class="card-title d-flex align-items-center">
                @if($section->code === 'makes' && $parent->logo)
                    <img src="{{ asset('storage/' . $parent->logo) }}" alt="{{ $parent->value }}"
                         style="width:26px;height:26px;object-fit:contain;margin-right:8px;background:#f4f4f4;border-radius:3px;padding:2px">
                @else
                    <span class="alpha-badge mr-2">{{ mb_strtoupper(mb_substr($parent->value, 0, 1, 'UTF-8')) }}</span>
                @endif
                {{ $parent->value }}
                <small class="text-muted ml-2" style="font-size:11px">
                    {{ $parent->children->count() }} {{ trans_choice('модель|модели|моделей', $parent->children->count()) }}
                </small>
            </h3>
            <div class="card-tools">
                <a href="{{ route('admin.manuals.value.create', [$section, 'parent_id' => $parent->id]) }}"
                   class="btn btn-xs btn-success mr-1" onclick="event.stopPropagation()">
                    <i class="fas fa-plus"></i>
                </a>
                <a href="{{ route('admin.manuals.value.edit', [$section, $parent]) }}"
                   class="btn btn-xs btn-info mr-1" onclick="event.stopPropagation()">
                    <i class="fas fa-edit"></i>
                </a>
                <form action="{{ route('admin.manuals.value.destroy', [$section, $parent]) }}"
                      method="POST" class="d-inline"
                      onsubmit="return confirm('Удалить {{ addslashes($parent->value) }} и все модели?')"
                      onclick="event.stopPropagation()">
                    @csrf @method('DELETE')
                    <button class="btn btn-xs btn-danger mr-1"><i class="fas fa-trash"></i></button>
                </form>
                <button type="button" class="btn btn-tool">
                    <i class="fas fa-plus"></i>
                </button>
            </div>
        </div>
        <div class="card-body p-0" style="display:none">
            @if($parent->children->isNotEmpty())
                <table class="table table-sm table-hover mb-0">
                    <tbody>
                        @foreach($parent->children as $child)
                            <tr>
                                <td class="pl-4">{{ $child->value }}</td>
                                <td class="text-muted" style="width:60px">#{{ $child->id }}</td>
                                <td style="width:80px" class="text-right">
                                    <a href="{{ route('admin.manuals.value.edit', [$section, $child]) }}"
                                       class="btn btn-xs btn-info"><i class="fas fa-edit"></i></a>
                                    <form action="{{ route('admin.manuals.value.destroy', [$section, $child]) }}"
                                          method="POST" class="d-inline"
                                          onsubmit="return confirm('Удалить {{ addslashes($child->value) }}?')">
                                        @csrf @method('DELETE')
                                        <button class="btn btn-xs btn-danger"><i class="fas fa-trash"></i></button>
                                    </form>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            @else
                <p class="text-muted text-center py-2 mb-0 small">Моделей нет</p>
            @endif
        </div>
    </div>
@empty
    <div class="alert alert-info">
        Значения не найдены.
        <a href="{{ route('admin.manuals.value.create', $section) }}">Добавить первое</a>
    </div>
@endforelse

<div id="no-results" class="alert alert-warning" style="display:none">
    <i class="fas fa-search mr-1"></i> Марок на эту букву не найдено.
</div>
