{% load wagtailcore_tags core_tags gem_tags %}

<form action="{% if not request.is_preview %}{% pageurl form_page %}{% endif %}{% if self.multi_step or self.has_page_breaks %}?p={{ fields_step.number|add:'1' }}{% endif %}" method="post" class="{{formtypeClass}}__form{% if self.get_effective_extra_style_hints %} {{formtypeClass}}__form{{self.get_effective_extra_style_hints}}{% endif %}" novalidate>
{% csrf_token %}
  {% if not is_via_freebasics %}
    {{ form.media }}
  {% endif %}
  {% for field in form %}
    {% if not field|fieldtype == 'HiddenInput' %}
    <fieldset>
      <div class="input-group">
        <label for="{{ field.id_for_label }}">{{ field.label|capfirst }}</label>
        {% if field.help_text %}
          {% if field|fieldtype == 'CharacterCountWidget' %}
            <p class="{{formtypeClass}}__helptext helptext">{{ field.help_text }}</p>
          {% else %}
            <p class="helptext">{{ field.help_text }}</p>
          {% endif %}
        {% endif %}
        {% if field|fieldtype == 'CheckboxInput' %}
          <div class="choice-group">
            <input name="{{ field.name }}" id="{{ field.label|idfromlabel }}" type="checkbox" value="{{field.value}}"/>
            <label for="{{ field.label|idfromlabel }}">{{ field.label|capfirst }}</label>
          </div>
        {% elif field|fieldtype == 'CheckboxSelectMultiple' %}
          {% for value, text in field.field.choices %}
            <div class="choice-group">
              <input name="{{ field.name }}" id="value-{{ field.label|idfromlabel }}-{{ forloop.counter }}" type="checkbox" value="{{ value }}" {% if field.value.0 == value %}checked="checked"{% endif %}/>
              <label for="value-{{ field.label|idfromlabel }}-{{ forloop.counter }}">{{ text|capfirst }}</label>
            </div>
          {% endfor %}
        {% elif field|fieldtype == 'RadioSelect' %}
          {% for value, text in field.field.choices %}
            <div class="choice-group choice-group__radio">
              <input name="{{ field.name }}"
               id="value-{{ field.id_for_label }}-{{ forloop.counter }}" type="radio" value="{{ value }}" {% if field.value == value %}checked="checked"{% endif %}/>
               <label for="value-{{ field.id_for_label }}-{{ forloop.counter }}">{{ text|capfirst }}</label>
            </div>
          {% endfor %}
        {% elif field|fieldtype == 'Select' %}
          <div class="select-style">
            <select id="{{ field.id_for_label }}"  name="{{field.name}}" class="select">
              {% if field.value.0 %}
                <option value="{{field.value}}" selected="selected">{{field.value}}</option>
                {% for choice in field.field.choices %}
                  <option value="{{choice.0}}" {% if field.value == choice.0 %}selected="selected"{% endif %}>
                    {{choice.0}}
                  </option>
                {% endfor %}
                {% elif not field.value.0  %}
                  <option value selected="selected">Choose..</option>
                  {% for choice in field.field.choices %}
                    <option value="{{choice.0}}" {% if field.value == choice.0 %}selected="selected"{% endif %}>
                      {{choice.0}}
                    </option>
                  {% endfor %}
              {% endif %}
            </select>
          </div>
        {% else %}
          {{field}}
        {% endif %}

        {% if field.errors %}
          <ul class="error-list errorlist">
          {% for error in field.errors %}
            <li class="error-list__item">{{ error }}</li>
          {% endfor %}
          </ul>
        {% endif %}
      </div>
    </fieldset>
    {% endif %}
  {% endfor %}

  {% trans "Submit" as text %}
  {% trans "Vote" as polltext %}
  {#  The display_survey_directly uses survey instead of self #}
  {% if survey.display_survey_directly %}
    {% if user.is_authenticated and user.is_active or request.is_preview or survey.allow_anonymous_submissions %}
      <button type="submit" class="call-to-action__button call-to-action__button--primary">{{ survey.submit_text|default:polltext }}</button>
    {% else %}
      {% trans "Log in to vote" as buttontext %}
      {% url LOGIN_URL as link %}
      {% include "patterns/basics/buttons/sp_variations/button.html" with type="primary" hyperlink=link|add:"?next="|add:request.path text=buttontext %}
    {% endif %}
  {% else %}
    <button type="submit" class="call-to-action__button call-to-action__button--primary">
      {% if is_intermediate_step %}
        {% trans 'Next Question' %}
      {% elif self.submit_text %}
        {{self.submit_text}}
      {% else %}{{ text|add:" "|add:formtype}}{% endif %}</button>
  {% endif %}
  {% if fields_step and fields_step.paginator.num_pages > 1 %}
    <div class="{{formtypeClass}}__pagination">
      <p class="{{formtypeClass}}__page">
        {% with page=fields_step.number pages=fields_step.paginator.num_pages %}
          {% trans Page %} <span class="{{formtypeClass}}__page-number">{{ page }}</span> {% trans of %} <span class="{{formtypeClass}}__page-number">{{ pages }}</span>
        {% endwith %}
      </p>
    </div>
  {% endif %}
</form>
