{% set container_style = schema.get('style') %}
<div class="form-group row">
  <div class="col-md-3 control-label">
    {% if schema.title %}<strong><span {% if is_required and schema.minItems %}class="required-label"{% endif %} {% if 'tooltip' in schema %}{% if is_template %} data-template-toggle="tooltip" {% else %} data-toggle="tooltip" {% endif %} data-placement="top" title="{{ schema.tooltip | get_translated_text }}"{% endif %}>{{ schema.title | get_translated_text }} {% if 'tooltip' in schema %} <i class="fa fa-question-circle" aria-hidden="true"></i>{% endif %}</span></strong>{% endif %}
  </div>
  {% set minItems = schema.get('minItems', 0) %}
  {% set maxItems = schema.get('maxItems', -1) %}
  {% set defaultItems = schema.get('defaultItems', minItems) %}
  {% if is_template and data is none %}
    {% set data = get_default_data(root_schema, property_path) %}
  {% endif %}
  {% if data is none %}
    {% set data = [] %}
  {% endif %}
  {% if diff is none %}
    {% set diff = [] %}
  {% endif %}
</div>
{% set template_order_index = template_order_index + 1 if template_order_index else 1 %}
{% set col_order_index = col_order_index + 1 if col_order_index else 1 %}
{% set parent_prefix = id_prefix %}
<div {% if get_style_variant(schema.style, template_mode) == "full_width_table" %}class="full-width-table-wrapper"{% else %}style="padding-left:2.5em;"{% endif %}>
  <input type="hidden" name="{{ id_prefix }}_hidden" value="array" />
  {% if schema['items']['type'] == 'object' %}
    {% set item_property_names = get_property_names_in_order(schema["items"], previous_schema["items"]) %}
    <table class="table" id="table_{{ table_prefix }}" data-id-prefix="{{ id_prefix }}" data-template-order-index="{{ template_order_index }}" data-array-container="object-table" data-min-items="{{ minItems }}" data-max-items="{{ maxItems }}" data-default-items="{{ defaultItems }}" data-is-required="{{ (not not is_required) | tojson }}">
      {% set is_required = True %}
      <thead>
        <tr>
          {% for property_name in item_property_names %}
            <th scope="col">{% if schema["items"].properties.get(property_name, {}).title %}<span {% if (property_name in schema['items'].get('required', ()) and (schema["items"].properties[property_name].type != "text" or schema["items"].properties[property_name].choices) and schema["items"].properties[property_name].type != "array") or schema["items"].properties[property_name].minLength or schema["items"].properties[property_name].minItems %}class="required-label"{% endif %} {% if 'tooltip' in schema["items"].properties[property_name] %}{% if is_template %} data-template-toggle="tooltip" {% else %} data-toggle="tooltip" {% endif %} data-placement="top" title="{{ schema["items"].properties[property_name].tooltip | get_translated_text }}"{% endif %}>{{ schema["items"].properties[property_name].title | get_translated_text }} {% if 'tooltip' in schema["items"].properties[property_name] %}<i class="fa fa-question-circle" aria-hidden="true"></i>{% endif %}</span>{% elif previous_schema and previous_schema['items'].properties.get(property_name, {}).title %}{{ previous_schema['items'].properties[property_name].title | get_translated_text }}{% else %}{{ property_name }}{% endif %}</th>
          {% endfor %}
          <th style="width: 1px"></th>
        </tr>
      </thead>
      <tbody>
        {% for item, item_diff in (zip_longest(data, diff) | list) + [(None, None)] %}
          {% set schema = schema['items'] %}
          {% if previous_schema %}
            {% set previous_schema = previous_schema.get('items') %}
          {% endif %}
          {% if loop.last %}
            {% set is_template = true %}
            {% set property_path = property_path + ('!index' ~ template_order_index ~ '!',) %}
            {% set item = get_default_data(root_schema, property_path) %}
          {% else %}
            {% set property_path = property_path + (loop.index0,) %}
          {% endif %}
          {% set id_prefix = id_prefix_for_property_path(property_path, id_prefix_root) %}
          <tr data-id-prefix="{{ id_prefix }}" data-array-item="object-table" {% if loop.last %}class="array-template"{% endif %}>
            {% set data = item %}
            {% set diff = item_diff %}
            <input type="hidden" name="{{ id_prefix }}_hidden" value="hidden" />
            {% for property_name in item_property_names %}
              <td {% if diff and diff.get(property_name) %}{% if '_after' in diff.get(property_name) and '_before' in diff.get(property_name) %}class="data-diff-both"{% elif '_after' in diff.get(property_name) %}class="data-diff-after"{% elif '_before' in diff.get(property_name) %}class="data-diff-before"{% endif %}{% endif %}>
                {% set is_required = property_name in schema.get('required', ()) %}
                {% set schema = schema.properties[property_name] %}
                {% set property_path = property_path + (property_name,) %}
                {% set id_prefix = id_prefix_for_property_path(property_path, id_prefix_root) %}
                {% if data is not none %}
                  {% set data = data.get(property_name) %}
                {% endif %}
                {% if diff is not none %}
                  {% set diff = diff.get(property_name) %}
                {% endif %}
                {% if previous_schema is not none %}
                  {% set previous_schema = previous_schema.get('properties', {}).get(property_name) %}
                {% endif %}
                {% if schema and 'conditions' in schema %}
                  <div data-condition-wrapper-for="{{ id_prefix }}" class="condition-wrapper" data-id-prefix="{{ id_prefix}}" data-conditions='{{ schema["conditions"] | encode_choices | stringify }}'>
                    {% include "objects/forms/form_any.html" %}
                  </div>
                  <div data-condition-replacement-for="{{ id_prefix }}" class="condition-replacement">
                    &mdash;
                  </div>
                {% else %}
                  {% include "objects/forms/form_any.html" %}
                {% endif %}
              </td>
            {% endfor %}
            <td>
              <div style="display: flex; justify-content: flex-end;">
                {% if 'recipes' in schema %}
                  {% include "objects/forms/form_table_recipe.html" %}
                {% endif %}
                <button type="button" id="action_{{ id_prefix }}_copy" class="btn btn-success form-copy-button" data-object-form-button="table_array_copy"><i class="fa fa-clone fa-fw"></i></button>
                <button type="button" id="action_{{ id_prefix }}_delete" class="btn btn-danger form-delete-button" data-object-form-button="table_array_delete" data-element-id="{{ property_path[-1] }}"><i class="fa fa-times fa-fw"></i></button>
              </div>
            </td>
          </tr>
        {% endfor %}
      </tbody>
      <tfoot>
        <tr>
          {% for property_name in item_property_names %}
            <th scope="col"></th>
          {% endfor %}
          <th scope="col">
            <div style="display: flex; justify-content: flex-end;">
              <button type="button" id="action_{{ id_prefix }}_?__add" class="btn btn-success form-add-button" data-object-form-button="table_array_add"><i class="fa fa-plus fa-fw"></i></button>
              &nbsp;
              <button type="button" id="action_{{ id_prefix }}_?__clear" class="btn btn-danger form-clear-button" data-object-form-button="table_array_clear"><i class="fa fa-trash-o fa-fw"></i></button>
            </div>
          </th>
        </tr>
      </tfoot>
    </table>

  {% elif schema['items']['type'] == 'array' %}
    {% set max_used_fields = [0] %}
    {% for item in data %}
      {% if (item | length) > max_used_fields[-1] %}
        {% do max_used_fields.append(item | length) %}
      {% endif %}
    {% endfor %}
    {% for item in diff %}
      {% if (item | length) > max_used_fields[-1] %}
        {% do max_used_fields.append(item | length) %}
      {% endif %}
    {% endfor %}
    {% set max_used_fields = max_used_fields [-1] %}
    <table class="table" data-template-order-index="{{ template_order_index }}" data-col-order-index="{{ col_order_index }}" data-id-prefix="{{ id_prefix }}" data-array-container="array-table" data-min-cols="{{ schema['items'].get('minItems', 0) }}" data-max-cols="{{ schema['items'].get('maxItems', -1) }}" data-min-rows="{{ schema.get('minItems', 0) }}" data-max-rows="{{ schema.get('maxItems', -1) }}" data-is-required="{{ (not not is_required) | tojson }}">
     {% set is_required = True %}
      <thead>
        <tr>
          {% for i in range(max_used_fields) %}
            <th>{{ _('Field %(index)s', index=i + 1) }}</th>
          {% endfor %}
          <th class="control-buttons text-right">
            <div class="btn-group" role="group" aria-label="Column Controls">
              <button type="button" id="action_{{ id_prefix }}_?__deletecolumn" class="btn btn-danger" data-object-form-button="table_col_delete"><i class="fa fa-arrow-left fa-fw"></i></button>
              <button type="button" id="action_{{ id_prefix }}_?__addcolumn" class="btn btn-success" data-object-form-button="table_col_add"><i class="fa fa-arrow-right fa-fw"></i></button>
            </div>
          </th>
        </tr>
      </thead>
      <tbody>
        {% set parent_prefix = id_prefix %}
        {% if previous_schema is not none %}
          {% set previous_schema = previous_schema.get('items', {}).get('items') %}
        {% endif %}
        {% for item, item_diff in (zip_longest(data, diff) | list) + [(None, None)] %}
          {% if loop.last %}
            {% set is_template = True %}
            {% set property_path = property_path + ('!index' ~ template_order_index ~ '!',) %}
            {% set item = get_default_data(root_schema, property_path) %}
          {% else %}
            {% set property_path = property_path + (loop.index0,) %}
          {% endif %}
          {% set id_prefix = id_prefix_for_property_path(property_path, id_prefix_root) %}
          <tr data-row-id="{{ property_path[-1] }}" {% if loop.last %}class="array-template" data-template-table-type="row"{% endif %}>
            <input type="hidden" name="{{ id_prefix }}_hidden" value="hidden" />
            {% if item_diff is none %}
              {% set item_diff = [] %}
            {% endif %}
            {% for field_index, field, field_diff in zip_longest(range(max_used_fields), item, item_diff) %}
              <td>
                {% set schema = schema["items"]["items"] %}
                {% set data = field %}
                {% set diff = field_diff %}
                {% set property_path = property_path + (loop.index0,) %}
                {% set id_prefix = id_prefix_for_property_path(property_path, id_prefix_root) %}
                {% include "objects/forms/form_any.html" %}
              </td>
            {% endfor %}
            <td class="control-buttons text-right">
              <button type="button" class="btn btn-danger delete-row-button" id="action_{{ id_prefix }}_?__deleterow" data-object-form-button="table_row_delete"><i class="fa fa-times fa-fw"></i></button>
            </td>
          </tr>
        {% endfor %}
        <tr class="array-template" data-template-table-type="col">
          <td class="array-col-template">
            {% with is_template = true %}
              {% set schema = schema["items"]["items"] %}
              {% set property_path = property_path + ('!index' ~ template_order_index ~ '!', '!cindex' ~ col_order_index ~ '!',) %}
              {% set data = get_default_data(root_schema, property_path) %}
              {% set diff = none %}
              {% set id_prefix = id_prefix_for_property_path(property_path, id_prefix_root) %}
              {% include "objects/forms/form_any.html" %}
            {% endwith %}
          </td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          {% for i in range(max_used_fields) %}
            <th>{{ _('Field %(index)s', index=i + 1) }}</th>
          {% endfor %}
          <th class="control-buttons text-right">
            <button type="button" id="action_{{ parent_prefix }}_?__addrow" class="btn btn-success" data-object-form-button="table_row_add"><i class="fa fa-plus fa-fw"></i></button>
            <button type="button" id="action_{{ parent_prefix }}_?__clear" class="btn btn-danger" data-object-form-button="table_rows_clear"><i class="fa fa-trash-o fa-fw"></i></button>
          </th>
        </tr>
      </tfoot>
    </table>
  {% endif %}
  <div class="form-group row">
    <div class="col-md-9 col-md-offset-3" style="left: -2.5em;">
      {% if 'note' in schema['items'] %}<span class="help-block"><strong>{{ _('Note:') }}</strong> {{ schema['items']['note'] | get_translated_text }}</span>{% endif %}
      {% if 'note' in schema %}<span class="help-block"><strong>{{ _('Note:') }}</strong> {{ schema['note'] | get_translated_text }}</span>{% endif %}
    </div>
  </div>
</div>
