{% load wagtailcore_tags gem_tags %}
{% wagtail_site as current_site %}
{% if self.description %}
  <div class="surveys--page__excerpt">
    {% for block in self.description %}
      {% if forloop.counter %}
        {% if block.block_type == 'heading' %}
          <h4 class="surveys--page__excerpt-heading">
           {{ block.value }}
          </h4>
        {% else %}
          {{ block }}
        {% endif %}
      {% endif %}
    {% endfor %}
  </div>
{% endif %}

{% if page.terms_and_conditions.exists and page.terms_and_conditions.first.terms_and_conditions.live %}
  <div class="surveys--page__terms">
    {% trans "Read:" %}
    <a href="{% pageurl page.terms_and_conditions.first.terms_and_conditions %}" class="surveys--page__terms-anchor">
      {{page.terms_and_conditions.first.terms_and_conditions.title}}
    </a>
  </div>
{% endif %}

{% comment %}
  {% if fields_step and fields_step.paginator.num_pages > 1 %}
    <div class="surveys__pagination surveys__pagination--multi-step">
      <h6 class="surveys__pagination-title">
        {% if self.your_words_competition %}
          {% blocktrans with pages=fields_step.paginator.num_pages %}This competition has {{pages}} pages {% endblocktrans %}
        {% elif not self.display_form_directly %}
          {% blocktrans with pages=fields_step.paginator.num_pages %}This survey has {{pages}} pages{% endblocktrans %}
        {% endif %}
      </h6>
    </div>
  {% endif %}
{% endcomment %}


{% if form %}
  <form action="{% if form_page.display_form_directly or form_page.article_form_only %}{% pageurl form_page %}{% else %}{% pageurl self.specific %}{% endif %}{% if self.multi_step or self.has_page_breaks %}?p={{ fields_step.number|add:'1' }}{% endif %}" method="post" class="{% if form_page.display_form_directly %}polls__form{% elif form_page.article_form_only %}reaction-questions__form{% elif self.contact_form %}contact__form{% elif self.your_words_competition %}gcn-competition__form{% else %}surveys__form{% endif %}" novalidate>

    {% csrf_token %}
    {% if not is_via_freebasics %}
      {{ form.media }}
    {% endif %}
    {% for field in form %}
      {% if 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="{% if self.display_form_directly %}polls{% else %}surveys{% endif %}__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="id_{{ field.name|slugify }}__{{ forloop.counter }}" type="checkbox" value="{{ value }}" {% if field.value.0 == value %}checked="checked"{% endif %}/>
              <label for="id_{{ field.name|slugify }}__{{ forloop.counter }}">{{ text|capfirst }}</label>
            </div>
          {% elif field|fieldtype == 'CheckboxSelectMultiple'  %}
            {% for value, text in field.field.choices %}
              <div class="choice-group">
                <input name="{{ field.name }}" id="id_{{field.name|slugify}}_{{ forloop.counter }}" type="checkbox" value="{{ value }}" {% if field.value.0 == value %}checked="checked"{% endif %}/>
                <label for="id_{{field.name|slugify}}_{{ 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="id_{{field.name|slugify}}_{{ forloop.counter }}" type="radio" value="{{ value }}" {% if field.value == value %}checked="checked"{% endif %}/>
                <label for="id_{{field.name|slugify}}_{{ 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 %}

    {% if form.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 %}
        <div class="polls">
          <a href="{{ LOGIN_URL }}?next={{request.path}}" class="call-to-action__button call-to-action__button--primary">
            <span class="call-to-action__button-text call-to-action__button-text--primary">
              {% trans "Log in to vote" %}
            </span>
          </a>
        </div>
      {% 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 and not self.personalisableform %}
            {% trans 'competition' %}
          {% elif self.display_form_directly and not self.personalisableform %}
            {% trans 'poll' %}
          {% elif self.contact_form %}
            {% trans 'form' %}
          {% elif self.article_form_only %}
            {% trans 'response.' %}
          {% else %}
            {% trans 'survey' %}
          {% endif %}
        {% endif %}
      </button>
    {% endif %}

    {% if fields_step and fields_step.paginator.num_pages > 1 %}
      <div class="{% if self.display_form_directly %}polls{% else %}surveys{% endif %}__pagination">
          <p class="{% if self.display_form_directly %}polls{% else %}surveys{% endif %}__page">
            {% with page=fields_step.number pages=fields_step.paginator.num_pages %}
              {% trans "Page"  %}<span class="{% if self.display_form_directly %}polls{% else %}surveys{% endif %}__page-number"> {{page}}</span> {% trans "of" %}<span class="{% if self.display_form_directly %}polls{% else %}surveys{% endif %}__page-number"> {{pages}}</span>
            {% endwith %}
          </p>
      </div>
    {% endif %}

    {% if not fields_step or fields_step and fields_step.number == 1 %}
      <input type="hidden" {% if self.content_type|lower == 'article' %} value="{{ self.pk }}" {% endif %} />
    {% endif %}
  </form>
{% else %}
  <div class="{% if self.display_form_directly %}polls{% else %}surveys{% endif %}">
    <p class="errors">
      {% trans 'You have already completed' %}
      {% if form_page.your_words_competition and not form_page.personalisableform %}
        {% trans 'this competition.' %}
      {% elif form_page.display_form_directly and not form_page.personalisableform %}
        {% trans 'this poll.' %}
      {% elif form_page.contact_form and not form_page.personalisableform %}
        {% trans 'this form.' %}
      {% elif form_page.article_form_only and not form_page.personalisableform %}
        {% trans 'a response for this article.' %}
      {% else %}
        {% trans 'this survey.' %}
      {% endif %}
    </p>
    {% if form_page.show_results %}
      <a href="{% url 'molo.forms:success' form_page.slug %}" class="call-to-action__button call-to-action__button--primary">
        <span class="call-to-action__button-text call-to-action__button-text--primary">
          {% trans "Show Results" %}
        </span>
      </a>
    {% else %}
      <div class="call-to-action call-to-action--wide-back">
        <a href="{{current_site.root_page.specific.url}}" class="call-to-action__nav-item-text call-to-action__nav-item-text--left">
          {% trans "Back home" %}</a>
      </div>
    {% endif %}
  </div>
{% endif %}
