<!-- array start -->
<div class="form-group row">
  <div class="col-md-3 control-label">
<strong>{{ schema.title }}</strong>
  </div>
{% set minItems = schema.get('minItems', 0) %}
{% set maxItems = schema.get('maxItems', None) %}
{% if data is not none %}
{% set numItems = (data | length) %}
{% else %}
{% set numItems = 0 %}
{% endif %}
{% if schema.style == "table" %}
</div>
<div style="padding-left:2.5em;">
  {% if schema['items']['type'] == 'object' %}
  <table class="table">
    <thead>
      <tr>
        {% set item_property_names = schema["items"].properties.keys() %}
        {% set property_order = schema.get('items', {}).get('propertyOrder', []) %}
        {% for property_name in property_order %}
          {% if property_name in item_property_names %}
          <th scope="col">{{ schema["items"].properties[property_name].title }}</th>
          {% endif %}
        {% endfor %}
        {% for property_name in item_property_names %}
          {% if property_name not in property_order %}
          <th scope="col">{{ schema["items"].properties[property_name].title }}</th>
          {% endif %}
        {% endfor %}
        <th></th>
      </tr>
    </thead>
    <tbody>
    {% if data is not none %}
      {% for item in data %}
        {% set name = "item" %}
        {% set schema = schema['items'] %}
        {% set id_prefix = id_prefix + '_{}_'.format(loop.index0) %}
        {% set data = item %}
        <tr>
        {% for property_name in property_order %}
          {% if property_name in item_property_names %}
          <td>
            {% set name = property_name %}
            {% set schema = schema.properties[property_name] %}
            {% set id_prefix = id_prefix + '_' + name + '_' %}
            {% if data is not none and property_name in data %}
            {% set data = data[property_name] %}
            {% else %}
            {% set data = none %}
            {% endif %}
            {% include "objects/forms/form_table_any.html" %}
          </td>
          {% endif %}
        {% endfor %}
        {% for property_name in item_property_names %}
          {% if property_name not in property_order %}
          <td>
            {% set name = property_name %}
            {% set schema = schema.properties[property_name] %}
            {% set id_prefix = id_prefix + '_' + name + '_' %}
            {% if data is not none and property_name in data %}
            {% set data = data[property_name] %}
            {% else %}
            {% set data = none %}
            {% endif %}
            {% include "objects/forms/form_table_any.html" %}
          </td>
          {% endif %}
        {% endfor %}
        {% if numItems > minItems %}
          <td><button type="submit" name="action_{{ id_prefix }}_delete" class="btn btn-danger pull-right">&times;</button></td>
        {% else %}
          <td><button type="button" class="btn btn-danger pull-right disabled">&times;</button></td>
        {% endif %}
        </tr>
      {% endfor %}
    {% endif %}
    </tbody>
    <tfoot>
      <tr>
        {% for property_name in item_property_names %}
          <th scope="col"></th>
        {% endfor %}
  {% if maxItems is none or numItems < maxItems %}
        <th><button type="submit" name="action_{{ id_prefix }}_?__add" class="btn btn-success pull-right">+</button></th>
  {% else %}
        <th><button type="button" class="btn btn-success pull-right disabled">+</button></th>
  {% endif %}
      </tr>
    </tfoot>
  </table>
  {% elif schema['items']['type'] == 'array' %}
  <table class="table">
  {% set max_used_fields = 0 %}
  {% if data is not none %}
    {% set max_used_fields = [0] %}
    {% for item in data %}
      {% if (item | length) > max_used_fields[-1] %}
        {% set tmp = max_used_fields.append(item | length) %}
      {% endif %}
    {% endfor %}
    {% set max_used_fields = max_used_fields [-1] %}
    <thead>
    <tr>
    {% for i in range(max_used_fields) %}
      <th>Field {{ (i+1) }}</th>
    {% endfor %}
      <th>
        <div class="btn-group pull-right" role="group" aria-label="Column Controls">
      {% if (data | length) > 0 and max_used_fields > schema["items"].get("minItems", 1) %}
          <button type="submit" name="action_{{ id_prefix }}_?__deletecolumn" class="btn btn-danger"><i class="fa fa-arrow-left"></i></button>
      {% else %}
          <button type="button" class="btn btn-danger disabled"><i class="fa fa-arrow-left"></i></button>
      {% endif %}
      {% if (data | length) > 0 and ("maxItems" not in schema["items"] or max_used_fields < schema["items"]["maxItems"]) %}
          <button type="submit" name="action_{{ id_prefix }}_?__addcolumn" class="btn btn-success"><i class="fa fa-arrow-right"></i></button>
      {% else %}
          <button type="button" class="btn btn-success disabled"><i class="fa fa-arrow-right"></i></button>
      {% endif %}
        </div>
      </th>
    </tr>
    </thead>
    <tbody>
    {% for item in data %}
      {% set item_index = loop.index0 %}
      <tr>
      {% for field in item %}
        <td>
          {% set name = "field_".format(loop.index0) %}
          {% set schema = schema["items"]["items"] %}
          {% set id_prefix = id_prefix + '_{}__{}_'.format(item_index, loop.index0) %}
          {% set data = field %}
          {% include "objects/forms/form_table_any.html" %}
        </td>
      {% endfor %}
      {% if (item | length) < max_used_fields %}
        {% for i in range((item | length), max_used_fields) %}
        <td>
          {% set name ="field_".format(loop.index0) %}
          {% set schema = schema["items"]["items"] %}
          {% set id_prefix = id_prefix + '_{}__{}_'.format(item_index, i) %}
          {% set data = null %}
          {% include "objects/forms/form_table_any.html" %}
        </td>{% endfor %}
      {% endif %}
      {% if numItems > minItems %}
        <td><button type="submit" name="action_{{ id_prefix }}_{{ item_index }}__delete" class="btn btn-danger pull-right">&times;</button></td>
      {% else %}
        <td><button type="button" class="btn btn-danger pull-right disabled">&times;</button></td>
      {% endif %}
      </tr>
    {% endfor %}
    </tbody>
  {% endif %}
  <tfoot>
  <tr>
  {% for i in range(max_used_fields) %}
    <th>Field {{ (i+1) }}</th>
  {% endfor %}
  {% if maxItems is none or numItems < maxItems %}
        <th><button type="submit" name="action_{{ id_prefix }}_?__add" class="btn btn-success pull-right">+</button></th>
  {% else %}
        <th><button type="button" class="btn btn-success pull-right disabled">+</button></th>
  {% endif %}
  </tr>
  </tfoot>
  </table>
  {% endif %}
{% elif schema.style == "list" %}
  {% set itemName = schema['items'].title %}
  {% if data is not none %}
    {% for item in data %}
      {% set name = "item" %}
      {% set schema = schema['items'] %}
      {% set id_prefix = id_prefix + '_{}_'.format(loop.index0) %}
      {% set data = item %}
        <div class="col-md-9 {% if loop.index0 %}col-md-offset-3{% endif %}" style="display:flex; padding-left:2em; padding-right:1.75em">
          {% include "objects/forms/form_list_any.html" %}
          <div style="flex-grow: 0; flex-basis: 4em; text-align: right;">
          {% if numItems > minItems %}
            <button type="submit" name="action_{{ id_prefix }}_delete" class="btn btn-danger" style="height:34px;"><i class="fa fa-minus"></i></button>
          {% else %}
            <button type="button" class="btn btn-danger disabled" style="height:34px;"><i class="fa fa-minus"></i></button>
          {% endif %}
          </div>
      </div>
    {% endfor %}
  {% endif %}
    <div class="col-md-9 {% if data %}col-md-offset-3{% endif %}" style="display: flex; padding-left:1em; padding-right:1.75em">
      <div style="flex-grow: 1">
      {% if 'note' in schema['items'] %}
        <span class="help-block"><strong>Note:</strong> {{ schema['items']['note'] }}</span>
      {% endif %}
      </div>
    <div style="flex-grow: 0; flex-basis: 4em;"></div>
    {% if maxItems is none or numItems < maxItems %}
      <button type="submit" name="action_{{ id_prefix }}_?__add" class="btn btn-success" style="height:34px;"><i class="fa fa-plus"></i></button>
    {% else %}
      <button type="button" class="btn btn-success disabled" style="height:34px;"><i class="fa fa-plus"></i></button>
    {% endif %}
    </div>
{% else %}
</div>
<div style="padding-left:2.5em;">
  {% set itemName = schema['items'].title %}
  {% if data is not none %}
    {% for item in data %}
      {% set name = "item" %}
      {% set schema = schema['items'] %}
      {% set id_prefix = id_prefix + '_{}_'.format(loop.index0) %}
      {% set data = item %}
        <div style="border-left:1px solid #cccccc; border-bottom:1px solid #cccccc; border-top:1px solid #cccccc; border-right:1px solid #cccccc; margin-right:-1px; border-radius:5px 0 0 5px; padding-left:2.5em; padding-top:1em; margin-bottom: 1em;">
        {% if numItems > minItems %}
        <div class="pull-right" style="padding-right:0.75em"><button type="submit" name="action_{{ id_prefix }}_delete" class="btn btn-danger" style="width:20em;">Remove {{ itemName }}</button></div>
        {% else %}
          <div class="pull-right" style="padding-right:0.75em"><button type="button" class="btn btn-danger disabled" style="width:20em;">Remove {{ itemName }}</button></div>
        {% endif %}
        {% include "objects/forms/form_any.html" %}
        </div>
    {% endfor %}
  {% endif %}
  <div class="form-group row clearfix" style="padding-right:0.75em">
    <div class="col-md-9 col-md-offset-3">
  {% if maxItems is none or numItems < maxItems %}
    <button type="submit" name="action_{{ id_prefix }}_?__add" class="btn btn-success pull-right" style="width:20em">Add {{ itemName }}</button>
  {% else %}
    <button type="button" class="btn btn-success pull-right disabled" style="width:20em">Add {{ itemName }}</button>
  {% endif %}
    </div>
  </div>
{% endif %}
</div>
<!-- array end -->
