{{--
 * Blade Template
 *
 * Usage:       <x-globals::dynamic-form.form
 *                  :template="$printerTypeConfigTemplate"
 *                  :sections="$configSections"
 *                  wire-model-prefix="configSettings"
 *                  display="sections" />
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Views
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
--}}

@props(['template', 'sections' => [], 'wireModelPrefix', 'display' => 'sections'])

@if ($template !== null)

    @if ($display === 'tabs')

        @php
            $sectionKeys         = array_keys($template);
            $hasDottedSections   = collect($sectionKeys)->contains(fn ($k) => str_contains($k, '.'));

            if ($hasDottedSections === true) {
                /*
                **  Zwei-Ebenen-Hierarchie: Section-Keys mit Punkt aufteilen.
                **  Tab-Key = Teil vor dem ersten Punkt.
                **  Sub-Section-Key = voller Section-Key (für Label-Lookup via $sections). */
                $tabs = [];
                foreach ($template as $sectionKey => $fields) {
                    $tabKey        = str_contains($sectionKey, '.') ? explode('.', $sectionKey, 2)[0] : $sectionKey;
                    $tabs[$tabKey][$sectionKey] = $fields;
                }
            } else {
                /*
                **  Einfache Tabs: jede Section wird ein Tab. */
                $tabs = array_map(fn ($fields) => [array_key_first($template) => $fields], array_values($template));
                $tabs = array_combine(array_keys($template), array_map(fn ($fields) => ['' => $fields], $template));
                $tabs = [];
                foreach ($template as $sectionKey => $fields) {
                    $tabs[$sectionKey] = [$sectionKey => $fields];
                }
            }

            $firstTabKey = array_key_first($tabs);
        @endphp

        <div x-data="{ currentTab: @js($firstTabKey) }">

            <x-globals::global.tabs.standard-container>

        @foreach ($tabs as $tabKey => $tabContent)

                    @php
                        /*
                        **  Tab-Label: aus $sections wenn vorhanden (z.B. wenn ein Feld
                        **  "section": "<tabKey>" ohne Punkt definiert ist), sonst Fallback
                        **  auf den Tab-Key selbst. */
                        $tabLabel = $sections[$tabKey] ?? $tabKey;
                    @endphp

                    <x-globals::global.tabs.standard-list-item id="{{ $tabKey }}">
                        {{ $tabLabel }}
                    </x-globals::global.tabs.standard-list-item>

        @endforeach

            </x-globals::global.tabs.standard-container>

        @foreach ($tabs as $tabKey => $tabContent)

                <div x-show="currentTab === @js($tabKey)">

            @foreach ($tabContent as $sectionKey => $fields)

                @php
                    $dotPos   = strpos($sectionKey, '.');
                    $subKey   = ($dotPos !== false) ? substr($sectionKey, $dotPos + 1) : null;
                    $subLabel = ($subKey !== null) ? ($sections[$sectionKey] ?? $subKey) : null;
                @endphp

                @if ($subLabel !== null)
                            <h4 class="flex items-center gap-2 text-sm font-semibold text-gray-700 dark:text-gray-300 mb-3 mt-4 first:mt-0">
                                {{ $subLabel }}
                                <span class="flex-1 border-b border-current opacity-20"></span>
                            </h4>
                @endif

                        <div class="grid gap-4 sm:grid-cols-2 mb-4">

                @foreach ($fields as $Field)

                                <x-globals::dynamic-form.field :field="$Field" :wire-model-prefix="$wireModelPrefix" />

                @endforeach

                        </div>

            @endforeach

                </div>

        @endforeach

        </div>

    @else

        {{-- display="sections" --}}

        @foreach ($template as $sectionKey => $fields)

            @if (!empty($fields))

                @php $sectionLabel = $sections[$sectionKey] ?? $sectionKey; @endphp

                @if (count($template) > 1 || ($sectionKey !== 'default' && $sectionLabel !== 'default'))

                    <h4 class="flex items-center gap-2 text-sm font-semibold text-gray-700 dark:text-gray-300 mb-3">
                        {{ $sectionLabel }}
                        <span class="flex-1 border-b border-current opacity-20"></span>
                    </h4>

                @endif

                <div class="grid gap-4 sm:grid-cols-2 mb-4">

                @foreach ($fields as $Field)

                        <x-globals::dynamic-form.field :field="$Field" :wire-model-prefix="$wireModelPrefix" />

                @endforeach

                </div>

            @endif

        @endforeach

    @endif

@endif
