{% import 'macros/form.html' as form %}
{% resource 'composite/composite-repeating.js' %}
{% resource 'composite/composite.css' %}

{%- snippet 'scheming/form_snippets/composite_header.html', field=field, inline=field.help_inline -%}

{% set field_dict_list=h.composite_repeating_get_value_dict_list(field.field_name, field.subfields, data, field.form_blanks if 'form_blanks' in field else 1 ) %}
<div data-module="composite-repeating">
{% set index = 0 %}
{% for field_dict in field_dict_list %}
    {% set index = index + 1 %}
    <div class="composite-control-repeating"> 
    {% set subfield_index = 0 %}
    {% for subfield in field.subfields %}
      {% set subfield_index = subfield_index + 1 %}
      {% set name=field.field_name + '-' + index|string + '-' + subfield.field_name %}
      {% set value=field_dict.get(subfield.field_name, '') %}
      
      {% if subfield.get('choices', false) %}
          {%- set options=[] -%}
          {%- if not h.scheming_field_required(subfield) or
              subfield.get('form_include_blank_choice', true) -%}
              {%- do options.append({'value': '', 'text': ''}) -%}
          {%- endif -%}
          {%- for c in subfield.choices -%}
             {%- do options.append({'value': c.value, 'text': h.scheming_language_text(c.label) }) -%}
          {%- endfor -%}
          {% call form.select(
             name,
             id=name,
             label=h.scheming_language_text(subfield.label) + ' ' + index|string,
             options=options,
             selected=value,
             error=errors[name],
             classes=['mediunput'],
             attrs=subfield.form_attrs if 'form_attrs' in subfield else {},
             is_required=h.scheming_field_required(subfield)
            )
           %}
           {% if subfield_index>1 %}
              {%- snippet 'scheming/form_snippets/help_text.html', field=subfield -%}
           {% endif %}
           {% endcall %}
      {% else %}
        {% set type = "text" %}
        {% set classes = ['control-medium']%}
        {% set input_extra_html = "" %}
        {% set extra_html="" %}
        {% set modal_text=subfield.modal_text if 'modal_text' in subfield else none %}
        {% set attrs=subfield.form_attrs if 'form_attrs' in subfield else {} %}
        {% set placeholder=subfield.form_placeholder if 'form_placeholder' in subfield else '' %}
        {% if subfield.get("preset", "") == "date" %}
           {% set value=field_dict.get(subfield.field_name, '').split()[0] %}
           {% set type = "date" %}
           {% set classes = []%}
        {% endif %}
        {%- call form.input_block(
          for='field-' + name,
          label=h.scheming_language_text(subfield.label) + ' ' + index|string,
          error=errors[name],
          classes=classses,
          control_classes=[],
          extra_html="",
          is_required=h.scheming_field_required(subfield)) -%}
            <input id="{{ id or name }}" type="{{ type }}" name="{{ name }}"
                 value="{{ value | empty_and_escape }}" placeholder="{{ placeholder }}"
                 {{ form.attributes(attrs) }} class="medinput"/>
            {% if subfield_index>1 %}
               {%- snippet 'scheming/form_snippets/help_text.html', field=subfield -%}
            {% endif %}
        {% endcall %}
      {% endif %}
      {% if subfield_index==1 %}
         {% if subfield.help_text %}
            <div class="first-subfield-info {{ ' first-subfield-info-inline' if subfield.help_inline }}">
             {{ form.info(text=h.scheming_language_text(subfield.help_text), inline=subfield.help_inline)}}
            </div>
         {% endif %}
      {% endif %}
    {% endfor %}
    <br>
    </div>
{% endfor %}
</div>
<hr>


