{% import 'macros/form.html' as form %}

{% set field_dict=h.composite_get_value_dict(field.field_name, data) %}

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

{% for subfield in field.subfields %}
   {% set name=field.field_name + '-' + subfield.field_name %}
   {% set value = '' %}
   {% if field_dict is mapping %}
      {% set value=field_dict.get(subfield.field_name, '') %}
   {% endif %}
   {% if subfield.get('preset', '') == 'tag_string_autocomplete' %}
      {%- snippet 'scheming/form_snippets/composite_tag_string_autocomplete.html', subfield=subfield, name=name, value=value, errors = errors -%}
   {% else %}
   {% 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),
         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) or h.scheming_field_required(field) 
        )
      %}
     {%- snippet 'scheming/form_snippets/help_text.html', field=subfield -%}
     {% endcall %}
   {% else %}
     {% set type = "text" %}
     {% set classes = ['control-medium']%}
     {% 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] if field_dict is mapping else '' %}
        {% set type = "date" %}
        {% set classes = []%}
     {% endif %}
     {%- call form.input_block(
       for='field-' + name,
       label=h.scheming_language_text(subfield.label),
       error=errors[name],
       classes=classes,
       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"/>

     {%- snippet 'scheming/form_snippets/help_text.html', field=subfield -%}
     {% endcall %}
   {% endif %}
   {% endif %}
{% endfor %}
<hr>


