{% macro render_field(form, fields, scope) %}
  {% import _self as self %}

  {% for index, field in fields %}
    {%- set show_field = attribute(field, "input@") ?? field.store ?? true %}
    {% if field.fields %}
      {%- set new_scope = field.nest_id ? scope ~ field.name ~ '.' : scope -%}
      {{- self.render_field(form, field.fields, new_scope) }}
    {% else %}
      {% if show_field %}
        {%- set value = form.value(scope ~ (field.name ?? index)) -%}
        {% if value %}
          {% block field %}
            <div>
              {% block field_label %}
                <strong>
                  {%- if field.markdown -%}
                    {{ field.data_label ? field.data_label|t|e|markdown(false) : field.label|t|emarkdown(false) }}
                  {%- else -%}
                    {{ field.data_label ? field.data_label|t|e : field.label|t|e }}
                  {%- endif -%}
                </strong>:
              {% endblock %}

              {% block field_value %}
                {% if field.type == 'checkboxes' %}
                  <ul>
                    {% set use_keys = field.use is defined and field.use == 'keys' %}
                    {% for key,value in form.value(scope ~ field.name) %}
                      {% set index = (use_keys ? key : value) %}
                      <li>{{ field.options[index]|t|e }}</li>
                    {% endfor %}
                  </ul>
                {% elseif field.type == 'radio' %}
                  {% set value = form.value(scope ~ field.name) %}
                  {{ field.options[value]|t|e }}
                {% elseif field.type == 'checkbox' %}
                  {{ (form.value(scope ~ field.name) == 1) ? "GRAV.YES"|t|e : "GRAV.NO"|t|e }}
                {% elseif field.type == 'select' %}
                  {% set value = form.value(scope ~ field.name) %}
                  {% if value is iterable %}
                    <ul>
                      {% set use_keys = field.use is defined and field.use == 'keys' %}
                      {% for key, val in value %}
                        {% set index = (use_keys ? key : val) %}
                        <li>{{ field.options[index]|t|e }}</li>
                      {% endfor %}
                    </ul>
                  {% else %}
                    {{ field.options[value]|t|e }}
                  {% endif %}
                {% else %}
                  {% set value = form.value(scope ~ field.name) %}
                  {% if value is iterable %}
                    <ul>
                      {% for val in value %}
                        {% if val is iterable %}
                          <ul>
                            {% for v in val %}
                              <li>{{ string(v)|e }}</li>
                            {% endfor %}
                          </ul>
                        {% else %}
                          <li>{{ string(val)|e }}</li>
                        {% endif %}
                      {% endfor %}
                    </ul>
                  {% else %}
                    {{ string(value)|e|nl2br }}
                  {% endif %}
                {% endif %}
              {% endblock %}
            </div>
          {% endblock %}
        {% endif %}
      {% endif %}
    {% endif %}
  {% endfor %}
{% endmacro %}

{% import _self as macro %}

{{ macro.render_field(form, form.fields, '') }}
