{{--
--}}
{{ __('locale.Categories') }}
@php
$selectedCategoryIds = [];
$lastParentIdAtDepth = [];
$groupedCategories = [];
$childIdsByParent = [];
foreach ($categories as $id => $name) {
$depth = substr_count($name, '—');
$cleanName = ltrim($name, '— ');
$parentId = $depth > 0 ? $lastParentIdAtDepth[$depth - 1] ?? null : null;
$lastParentIdAtDepth[$depth] = $id;
if ($parentId) {
$childIdsByParent[$parentId][] = $id;
}
$groupedCategories[] = [
'id' => $id,
'name' => $cleanName,
'depth' => $depth,
'parent_id' => $parentId,
];
}
@endphp
@foreach ($groupedCategories as $parent)
@php
// Check if any of this parent's children are selected
$hasSelectedChild = !empty(
array_intersect($childIdsByParent[$parent['id']] ?? [], $selectedCategoryIds)
);
$isExpanded = $hasSelectedChild || $loop->first; // also open first by default
@endphp
@if ($parent['depth'] == 0)
@foreach ($groupedCategories as $child)
@if ($child['parent_id'] === $parent['id'])
@endif
@endforeach
@endif
@endforeach