{% import _self as self %}

{% set subject = metadata.name|replace({'_attribute': ''}) %}
{% for code, localeCodes in forms %}
    {% for localeCode, form in localeCodes %}
        <div class="attribute" data-id="{{ code }}">
            {% set id = form.vars.label|replace({' ': '_'})|lower %}
            {% if 'type_checkbox' in form.vars.cache_key %}
                {{ self.checkboxField(form, count, id, subject, metadata.applicationName) }}
            {% else %}
                {{ self.textField(form, count, id, subject, metadata.applicationName) }}
            {% endif %}
            <input type="hidden"
                   name="{{ metadata.applicationName }}_{{ subject }}[attributes][{{ count }}][attribute]"
                   id="{{ metadata.applicationName }}_{{ subject }}_attributes_{{ count }}_attribute"
                   value="{{ code }}"/>
            <input type="hidden"
                   name="{{ metadata.applicationName }}_{{ subject }}[attributes][{{ count }}][localeCode]"
                   id="{{ metadata.applicationName }}_{{ subject }}_attributes_{{ count }}_localeCode"
                   value="{{ localeCode }}"/>
            {% set count = count + 1 %}
        </div>
    {% endfor %}
{% endfor %}

{% macro formField(item, count, id, prefix, subject, applicationName) %}
    {% from _self import formField %}
    {% if item.children|length > 0 %}
        {% set prefix = prefix~'_'~item.vars.name %}
        {% for child in item.children %}
            {{ formField(child, count, id, prefix, subject, applicationName) }}
        {% endfor %}
    {% elseif item.vars.name != '_token' %}
        {% set namePrefix = prefix|replace({'_': ']['}) %}
        {% set dataName = applicationName~'_'~subject~'[attributes]['~count~namePrefix~']['~item.vars.name~']' %}
        {% if item.vars.multiple is defined and item.vars.multiple %}
            {% set dataName = dataName~'[]' %}
        {% endif %}

        {{ form_widget(item, {'id': id, 'attr': {'data-name': dataName }}) }}
    {% endif %}
{% endmacro %}

{% macro textField(form, count, id, subject, applicationName) %}
    {% from _self import formField %}
    <div class="ui right fluid labeled input" style="margin-top: 15px; margin-bottom: 15px;">
        <div class="ui label">{{ form.vars.label }}</div>
        {{ formField(form, count, id, '', subject, applicationName) }}
        <button class="ui red right labeled icon button">
            <i class="remove icon"></i> {{ 'sylius.ui.delete'|trans }}
        </button>
    </div>
{% endmacro %}

{% macro checkboxField(form, count, id, subject, applicationName) %}
    {% from _self import formField %}
    <div class="ui toggle checkbox" style="margin-top: 15px; margin-bottom: 15px;">
        {{ formField(form, count, id, '', subject, applicationName) }}
        <label>{{ form.vars.label }}</label>
    </div>
    <button class="ui red right labeled icon button" style="float: right;">
        <i class="remove icon"></i> {{ 'sylius.ui.delete'|trans }}
    </button>
{% endmacro %}
