@php
    $wrapperAttributes = $wrapperAttributes ?? ['class' => 'mb-3'];
@endphp

<div {!! Html::attributes($wrapperAttributes) !!}>
    @if (isset($label) && $label)
        <label @class(['form-label', 'required' => isset($required) && $required])>{{ $label }}</label>
        <fieldset class="form-fieldset">
    @endif
    <div class="shortcode-tabs-field-wrapper">
        <div class="mb-3">
            <label class="form-label">{{ trans('packages/shortcode::shortcode.form.quantity') }}</label>
            {!! Form::customSelect($tabKey ? "{$tabKey}_quantity" : 'quantity', $choices, $current, [
                'id' => $selector,
                'data-max' => $max,
                'data-key' => $tabKey,
                'class' => 'shortcode-tabs-quantity-select',
            ]) !!}
        </div>

        <div
            class="accordion"
            id="accordion-tab-shortcode mt-2"
            style="--bs-accordion-btn-padding-y: .7rem;"
        >
            @for ($i = $min; $i <= $max; $i++)
                @php
                    $tabItemKey = $tabKey ? "{$tabKey}_{$i}" : $i;
                @endphp
                <div
                    class="accordion-item"
                    @style(['display: none' => $i > $current])
                    data-tab-id="{{ $tabItemKey }}"
                >
                    <h2
                        class="accordion-header"
                        id="heading-{{ $tabItemKey }}"
                    >
                        <button
                            class="accordion-button collapsed"
                            data-bs-toggle="collapse"
                            data-bs-target="#collapse-{{ $tabItemKey }}"
                            type="button"
                            aria-expanded="false"
                            aria-controls="collapse-{{ $tabItemKey }}"
                        >
                            {{ trans('packages/shortcode::shortcode.form.tab_number', ['number' => $i]) }}
                        </button>
                    </h2>
                    <div
                        class="accordion-collapse collapse"
                        id="collapse-{{ $tabItemKey }}"
                        data-bs-parent="#accordion-tab-shortcode"
                        aria-labelledby="heading-{{ $tabItemKey }}"
                    >
                        <div class="accordion-body bg-light">
                            <div class="section">
                                @foreach ($fields as $k => $field)
                                    @php
                                        $key = $tabKey ? "{$tabKey}_{$k}_{$i}" : "{$k}_{$i}";
                                        $name = $i <= $current ? $key : '';
                                        $title = Arr::get($field, 'title');
                                        $placeholder = Arr::get($field, 'placeholder', $title);
                                        $defaultValue = Arr::get($field, 'value', Arr::get($field, 'default_value'));
                                        $value = Arr::get($attributes, $key, $defaultValue);
                                        $fieldAttributes = [...Arr::get($field, 'attributes', []), 'data-name' => $key];

                                        $options = [];
                                        if (Arr::has($field, 'options') || Arr::has($field, 'choices')) {
                                            $options =
                                                Arr::get($field, 'options', []) ?: Arr::get($field, 'choices', []);
                                        }
                                    @endphp

                                    <div class="mb-3">
                                        <label @class(['form-label', 'required' => Arr::get($field, 'required')])>{{ $title }}</label>
                                        @switch(Arr::get($field, 'type'))
                                            @case('image')
                                            @case('mediaImage')
                                                {!! Form::mediaImage($name, $value, $fieldAttributes) !!}
                                            @break

                                            @case('file')
                                            @case('mediaFile')
                                                {!! Form::mediaFile($name, $value, $fieldAttributes) !!}
                                            @break

                                            @case('color')
                                                {!! Form::customColor($name, $value, $fieldAttributes) !!}
                                            @break

                                            @case('icon')
                                                {!! Form::themeIcon($name, $value, $fieldAttributes) !!}
                                            @break

                                            @case('number')
                                                {!! Form::number($name, $value, [
                                                    'class' => 'form-control',
                                                    'placeholder' => $placeholder,
                                                    'data-name' => $key,
                                                    'required' => Arr::get($field, 'required', false),
                                                ]) !!}
                                            @break

                                            @case('textarea')
                                                {!! Form::textarea($name, $value, [
                                                    'class' => 'form-control',
                                                    'placeholder' => $placeholder,
                                                    'rows' => 3,
                                                    'required' => Arr::get($field, 'required', false),
                                                    ...$fieldAttributes,
                                                ]) !!}
                                            @break

                                            @case('url')
                                            @case('link')
                                                {!! Form::url($name, $value, [
                                                    'class' => 'form-control',
                                                    'placeholder' => $placeholder,
                                                    'required' => Arr::get($field, 'required', false),
                                                    ...$fieldAttributes,
                                                ]) !!}
                                            @break

                                            @case('email')
                                                {!! Form::email($name, $value, [
                                                    'class' => 'form-control',
                                                    'placeholder' => $placeholder,
                                                    'required' => Arr::get($field, 'required', false),
                                                    ...$fieldAttributes,
                                                ]) !!}
                                            @break

                                            @case('checkbox')
                                                @php($options = ['no' => trans('packages/shortcode::shortcode.form.no'), 'yes' => trans('packages/shortcode::shortcode.form.yes')])
                                            @case('select')
                                                {!! Form::customSelect($name, $options, $value, [
                                                    'required' => Arr::get($field, 'required', false),
                                                    ...$fieldAttributes,
                                                ]) !!}
                                            @break

                                            @case('onOff')
                                                {!! Form::onOff($name, $value, [...$options, ...$fieldAttributes]) !!}
                                            @break

                                            @case('coreIcon')
                                                {!! Form::coreIcon($name, $value, [...$options, ...$fieldAttributes]) !!}
                                            @break

                                            @default
                                                {!! Form::text($name, $value, [
                                                    'class' => 'form-control',
                                                    'placeholder' => $placeholder,
                                                    'required' => Arr::get($field, 'required', false),
                                                    ...$fieldAttributes,
                                                ]) !!}
                                        @endswitch

                                        @if ($helper = Arr::get($field, 'helper'))
                                            {{ Form::helper($helper) }}
                                        @endif
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                </div>
            @endfor
        </div>
    </div>
</div>

@if (isset($label) && $label)
    </fieldset>
@endif
<script src="{{ asset('vendor/core/packages/shortcode/js/shortcode-fields.js') }}?v={{ get_cms_version() }}"></script>
