@php $orderedChoices = function ($choices, $field) use ($sectionSettings) { return $choices->values()->map(function ($choice, $index) use ($field, $sectionSettings) { $key = $field === 'category_order' ? $choice->id : $choice['key']; $value = old('section_settings.'.$field.'.'.$key, $sectionSettings[$field][$key] ?? null); return ['key' => $key, 'title' => $field === 'category_order' ? $choice->title : $choice['title'], 'order' => $value, 'saved_order' => $sectionSettings[$field][$key] ?? null, 'natural' => $index]; })->sortBy(fn ($choice) => [filled($choice['saved_order']) ? 0 : 1, filled($choice['saved_order']) ? (int) $choice['saved_order'] : 0, $choice['natural']]); }; $selectedCategoryIds = (array) old('section_settings.category_ids', old('category_selection_present') ? [] : ($sectionSettings['category_ids'] ?? [])); @endphp

@lang('qr_builder.optional_order_help')

@foreach (['categories' => 'category_order', 'stores' => 'store_order'] as $type => $field)
@lang('qr_builder.' . ($type === 'categories' ? 'nested_categories' : 'store_order'))
@foreach ($orderedChoices($type === 'categories' ? ($categoryChoices ?? collect()) : ($storeChoices ?? collect()), $field) as $choice)
$type === 'categories' && !in_array($choice['key'], $selectedCategoryIds)]) data-order-item data-order-title="{{ $choice['title'] }}" @if($type === 'categories') data-category-order-id="{{ $choice['key'] }}" @endif>
@endforeach
@if($type === 'categories') @elseif(($storeChoices ?? collect())->isEmpty())

@lang('qr_builder.store_order_empty')

@endif
@endforeach