{% load wagtailcore_tags gem_tags %}
{% wagtail_site as current_site %}
{% if form %}
  <form action="{% if form_page.display_form_directly %}{% pageurl form_page %}{% else %}{% pageurl self.specific %}{% endif %}{% if self.multi_step or self.has_page_breaks %}?p={{ self.number|add:'1' }}{% endif %}" method="post" class="{% if form_page.your_words_competition %}yourwords__form yourwords__form--{{form_page.get_effective_extra_style_hints}} {% elif form_page.display_form_directly %}polls__form polls__form--{{form_page.get_effective_extra_style_hints}}{% else %}surveys__form surveys__form--{{form_page.get_effective_extra_style_hints}}{% endif %}" novalidate>
    {% csrf_token %}
    {% if not is_via_freebasics %}
      {{ form.media }}
    {% endif %}
    {% for field in form %}
      {% if field|fieldtype != 'HiddenInput' %}
        {% if field|fieldtype == 'CheckboxInput' %}
          <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>
        {% endif %}
        {% if field|fieldtype == 'CheckboxSelectMultiple' %}
          <fieldset {% if self.display_form_directly %}class="choice-fieldset"{% endif %}>
            {% 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 %}
          </fieldset>
        {% elif field|fieldtype == 'RadioSelect' %}
          <fieldset {% if self.display_form_directly %}class="choice-fieldset"{% endif %}>
            {% 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 %}
          </fieldset>
        {% elif field|fieldtype == 'Select' %}
          <fieldset {% if self.display_form_directly %}class="choice-fieldset"{% endif %}>
            <div class="select-group">
              <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>
          </fieldset>
        {% else %}
          <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="{% if self.display_form_directly %}polls{% else %}surveys{% endif %}__helptext helptext">{{ field.help_text }}</p>
                {% else %}
                  <p class="input-hint">{{ field.help_text }}</p>
                {% endif %}
              {% endif %}
              {{field}}
            </div>
          </fieldset>
        {% endif %}
        {% if field.errors %}
          <ul class="error-list">
          {% for error in field.errors %}
            <li class="error-list__item">{{ error }}</li>
          {% endfor %}
          </ul>
        {% endif %}
      {% endif %}
    {% endfor %}
    <div class="call-to-action--group">
      {% if form_page.display_form_directly %}
        {% if user.is_authenticated and user.is_active or request.is_preview or form.allow_anonymous_submissions %}
          {% trans "Vote" as defaultText %}
          <button type="submit" class="call-to-action__button call-to-action__button--primary">
            {{ form.submit_text|default:defaultText }}
          </button>
        {% else %}
          <a href="{{ LOGIN_URL }}?next={{request.path}}" class="call-to-action__button call-to-action__button--primary">
            {% trans "Log in to vote" %}
          </a>
        {% 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 %}
            {% trans 'Submit' %}
            {% if self.your_words_competition %}
              {% trans 'competition' %}
            {% elif self.contact_form %}
              {% trans 'contact' %}
            {% elif self.article_form_only %}
              {% trans 'question' %}
            {% else %}
              {% trans 'survey' %}
            {% endif %}
          {% endif %}
        </button>
      {% endif %}
    </div>

    {% if fields_step and fields_step.paginator.num_pages > 1 %}
      <div class="pagination">
        <p class="call-to-action__item--text call-to-action__item--text-current">
          {% with page=fields_step.number pages=fields_step.paginator.num_pages %}
            {% trans "Page"  %} {{page}} {% trans "of" %} {{pages}}
          {% endwith %}
        </p>
      </div>
    {% endif %}
  </form>
{% else %}

  <p class="error-list__item">
    {% trans 'You have already completed this ' %}
    {% if self.your_words_competition %}
      {% trans 'competition' %}
    {% elif self.display_form_directly %}
      {% trans 'poll' %}
    {% elif self.contact_form %}
      {% trans 'contact form' %}
    {% elif self.article_form_only %}
      {% trans 'question' %}
    {% else %}
      {% trans 'survey' %}
    {% endif %}
  </p>

  <div class="call-to-action--group">
    {% if request.path == "/" %}
      <a href="{% pageurl form %}success/" class="call-to-action__button call-to-action__button--primary">
        {% trans "Show Results" %}
      </a>
    {% else %}
      <a href="{{current_site.root_page.specific.url}}" class="call-to-action__button call-to-action__button--previous">
        {% trans "Back home" %}
      </a>
    {% endif %}
  </div>

{% endif %}
