{% extends 'bootstrap_4_layout.html.twig' %}

{% block checkbox_widget -%}
    {%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
    {%- if 'custom-switch' in parent_label_class -%}
        {%- set attr = attr|merge({class: (attr.class|default('') ~ ' custom-control-input')|trim}) -%}
        {{- form_label(form, null, {
            widget: parent(),
            'label_attr': {
                'class': 'custom-control custom-switch'
            },
        }) -}}
    {%- else -%}
        {%- set attr = attr|merge({class: (attr.class|default('') ~ '  custom-control-input')|trim}) -%}
        {{- form_label(form, null, {
            widget: parent(),
            'label_attr': {
                'class': 'custom-control custom-checkbox',
                'type': 'checkbox'
            },
        }) -}}
    {%- endif -%}
{%- endblock checkbox_widget %}

{% block radio_widget -%}
    {{- form_label(form, null, {
        widget: parent(),
        'label_attr': {
            'class': 'custom-control custom-radio',
            'type': 'radio'
        },
    }) -}}
{%- endblock radio_widget %}

{# Labels #}

{% block form_label -%}
    {% if label is not same as(false) -%}
        {%- if compound is defined and compound -%}
            {%- set element = 'legend' -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' col-form-label')|trim}) -%}
        {%- else -%}
            {%- set label_attr = label_attr|merge({for: id}) -%}
        {%- endif -%}
        {% if required -%}
            {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) %}
        {%- endif -%}
        {% if label is empty -%}
            {%- if label_format is not empty -%}
                {% set label = label_format|replace({
                    '%name%': name,
                    '%id%': id,
                }) %}
            {%- else -%}
                {% set label = name|humanize %}
            {%- endif -%}
        {%- endif -%}
        <{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>{{ translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain) }}</{{ element|default('label') }}>
    {%- else -%}
        {%- if errors|length > 0 -%}
            <div id="{{ id }}_errors" class="mb-2">
                {{- form_errors(form) -}}
            </div>
        {%- endif -%}
    {%- endif -%}
{%- endblock form_label %}

{% block checkbox_radio_label -%}
    {#- Do not display the label if widget is not defined in order to prevent double label rendering -#}
    {%- if widget is defined -%}
        {%- set label_attr = label_attr|merge({class: (label_attr.class|default(''))|trim}) -%}
        {%- if not compound -%}
            {% set label_attr = label_attr|merge({'for': id}) %}
        {%- endif -%}
        {%- if required -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) -%}
        {%- endif -%}
        {%- if disabled -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' disabled')|trim}) -%}
        {%- endif -%}
        {%- if parent_label_class is defined -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ parent_label_class)|replace({'checkbox-inline': '', 'radio-inline': '', 'checkbox-custom': '', 'radio-custom': ''})|trim}) -%}
        {%- endif -%}
        {%- if label is not same as(false) and label is empty -%}
            {%- if label_format is not empty -%}
                {%- set label = label_format|replace({
                    '%name%': name,
                    '%id%': id,
                }) -%}
            {%- else -%}
                {%- set label = name|humanize -%}
            {%- endif -%}
        {%- endif -%}

        {%- set label_attr = label_attr|merge({class: (label_attr.class|default(''))|trim}) -%}

        <label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
            <input type="{{ label_attr.type|default('checkbox') }}"
                   name="{{ full_name }}"
                   id="{{ id }}"
                   value="{{ value }}"
                   class="custom-control-input"
                   {% if required %}required {% endif %}
                   {% if checked %}checked {% endif %}
                   {% if disabled %}disabled {% endif %}
                   {% with { attr: attr } %}{{ block('attributes') }}{% endwith %}
            >
            <span class="custom-control-field"></span>
            <span class="custom-control-text">{{- label is not same as(false) ? (translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain)) -}}</span>
            {{- form_errors(form) -}}
        </label>
    {%- endif -%}
{%- endblock checkbox_radio_label %}

{# Rows #}

{% block form_row -%}
    {%- if compound is defined and compound -%}
        {%- set element = 'fieldset' -%}
    {%- endif -%}
    {%- set widget_attr = {} -%}
    {%- if form.vars.choices is defined -%}
        {%- set widget_attr = {attr: {'class': 'selectpicker', 'data-style':null, 'data-style-base':null}} -%}
    {%- endif -%}
    <{{ element|default('div') }}{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group')|trim})} %}{{ block('attributes') }}{% endwith %}>
    {{- form_label(form) -}}
    {{- form_widget(form, widget_attr) -}}
    {{- form_help(form) -}}
    {{- form_errors(form) -}}
    </{{ element|default('div') }}>
{%- endblock form_row %}

{# Errors #}

{% block form_errors -%}
    {%- if errors|length > 0 -%}
        <span class="{% if form is not rootform %}invalid-feedback{% else %}alert alert-danger{% endif %} d-block">
            {%- for error in errors -%}
                {{ error.message }}
            {%- endfor -%}
        </span>
    {%- endif %}
{%- endblock form_errors %}

{# Help #}

{% block form_help -%}
    {%- if help is not empty -%}
        {%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' form-text')|trim}) -%}
        <small id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
            {%- if translation_domain is same as(false) -%}
                {%- if help_html is same as(false) -%}
                    {{- help -}}
                {%- else -%}
                    {{- help|raw -}}
                {%- endif -%}
            {%- else -%}
                {%- if help_html is same as(false) -%}
                    {{- help|trans(help_translation_parameters, translation_domain) -}}
                {%- else -%}
                    {{- help|trans(help_translation_parameters, translation_domain)|raw -}}
                {%- endif -%}
            {%- endif -%}
        </small>
    {%- endif -%}
{%- endblock form_help %}

{% block file_widget -%}
    <{{ element|default('div') }} class="custom-file">
    {%- set type = type|default('file') -%}
    {{- block('form_widget_simple') -}}
    {%- set label_attr = label_attr|merge({ class: (label_attr.class|default('') ~ ' custom-file-label form-control custom-file-placeholder')|trim }) -%}
    <label for="{{ form.vars.id }}" {% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
        {%- if attr.placeholder is defined and attr.placeholder is not none -%}
            {{- translation_domain is same as(false) ? attr.placeholder : attr.placeholder|trans({}, translation_domain) -}}
        {%- else -%}
            {{ 'app.ui.form.file_none'|trans }}
        {%- endif -%}
    </label>
    <div class="input-group-append">
        <button class="btn btn-primary" type="button">{{ 'app.ui.form.file_browse'|trans }}</button>
    </div>
    </{{ element|default('div') }}>
{% endblock %}
