{% extends "forms/field.html.twig" %}

{% block field %}
  <div class="form-fieldset{% if vertical %} vertical{% endif %}{% if field.classes is defined %} {{ field.classes }}{% endif %}">
    {% block contents %}
      <input type="checkbox" class="hidden" id="fieldset_collapsible_{{ field.name }}"{% if not field.collapsible or not field.collapsed %} checked="checked"{% endif %} />

      <div class="form-label form-fieldset--label">
        <h2>
          <label{% if field.collapsible %} for="fieldset_collapsible_{{ field.name }}" class="form-fieldset--cursor {{ field.labelclasses }}"{% endif %}>
            {% if field.help %}
            <span class="hint--bottom" data-hint="{{ field.help|t }}">
            {% endif %}
            {% block label %}
              {% if field.icon %}
                <i class="fa fa-fw fa-{{ field.icon }}"></i>
              {% endif %}
              {{ field.title|t }}
              {{ field.validate.required in ['on', 'true', 1] ? '<span class="required">*</span>' }}
              {% if field.info %}
                <span class="form-fieldset--info">{{ field.info|t }}</span>
              {% endif %}
            {% endblock %}
          {% if field.help %}
          </span>
          {% endif %}

          {# Actions panel #}
          <span class="actions">
            {% block actions %}
              {% if field.collapsible %}
                <span class="form-fieldset--collapsible">
                  <i class="fa fa-chevron-down open"></i>
                  <i class="fa fa-chevron-up close"></i>
                </span>
              {% endif %}
            {% endblock %}
          </span>
        </label>
      </h2>
      </div>
      <div class="form-data"
        {#{% block global_attributes %}
          data-grav-field="{{ field.type }}"
          data-grav-disabled="{{ originalValue is null ? 'true' : 'false' }}"
          data-grav-default="{{ field.default|json_encode()|e('html_attr') }}"
        {% endblock %}#}
      >

          {% block group %}
              {% if field.text %}
                {{ field.markdown ? field.text|t|markdown : ('<p>' ~ field.text|t ~ '</p>')|raw }}
              {% endif %}

              {% if field.fields %}
                  {% for child_name, child in field.fields %}
                      {% set child = prepare_form_field(child, child_name, field.name, {key: key}) %}
                      {% if child %}
                        {% set default_layout = 'text' %}
                        {% if child.type == 'key' or child.key == true %}
                            {# Special handling for the key field #}
                            {% set default_layout = 'key' %}
                            {% set child_value = key %}
                        {% elseif child.name == 'value' %}
                            {# Special handling for the value field #}
                            {% set child = child|merge({ name: field.name }) %}
                            {% set child_value = value %}
                        {% else %}
                          {% set child_value = form ? form.value(child.name) : data.value(child.name) %}
                        {% endif %}

                        {% set field_templates = include_form_field(child.type, field_layout, default_layout) %}
                        {% include field_templates with { field: child, value: child_value, originalValue: null } %}
                      {% endif %}
                  {% endfor %}
              {% endif %}
          {% endblock %}
      </div>
    {% endblock %}
  </div>
{% endblock %}
