{% extends "base.html" %}

{% comment %}This view is used to create or edit a ``mtp.apps.surveys.Pool`` instance.

The author field is not displayed: it must be set to the current user when saving the pool,
by the view.

Context
-------
user : instance of ``mtp.apps.accounts.MTPUser``
    The user is expected to be a superuser or a trainer.
pool : instance of ``mtp.apps.surveys.models.Pool``
    The pool to edit or create.
form : instance of ``django.forms.models.ModelForm``
    The form to created/edit the pool (pre-filled if it's an edition).

Notes
-----
If ``pool.pk`` is "falsy", then we assume that's it's new pool to be created, else it's an
existing pool to update.

{% endcomment %}

{% load i18n %}

{% block body_class %}{{ block.super }} page-cms-surveys page-cms-surveys-pools{% endblock %}

{% block subtitle %}
    {% if pool.pk %}
        {% blocktrans with pool_name=pool.name %}Edit the pool of questions "{{ pool_name }}"{% endblocktrans %}
    {% else %}
        {% if user.is_superuser %}
            {% trans 'New global pool of questions' %}
        {% else %}
            {% trans 'New pool of questions' %}
        {% endif %}
    {% endif %}
{% endblock %}

{% block actions %}
    <a class="btn btn-default" href="{{ view.model.get_list_url }}">{% trans 'Back to the list of pools' %}</a>
    {% if pool.pk %}
        <a class="btn btn-primary" href="{{ pool.get_questions_list_url }}">{%  trans 'Manage questions' %}</a>
    {% endif %}
{% endblock %}

{% block main %}
    {% if form.errors %}
        <div class="alert alert-danger">
            {% trans 'Please correct errors below.' %}
            {% if form.non_field_errors %}
                {{ form.non_field_errors }}
            {% endif %}
        </div>
    {% endif %}

    <form action="{% if pool.pk %}{{ pool.get_edit_url }}{% else %}{{ view.model.get_create_url }}{% endif %}" method="post" enctype="multipart/form-data">{% csrf_token %}
        {{ form.media }}
        <table class="table table-hover">
            <tbody>
                {% for field in form %}
                    <tr>
                        <td>
                            {{ field.label_tag }}
                        </td>
                        <td>
                            {%  if field.errors %}<div class="alert alert-danger">{{ field.errors }}</div>{% endif %}
                            {{ field }}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>

        </table>

        <div class="well">
            {% if pool.pk %}
                <input class="btn btn-primary" type="submit" value="{% trans 'Save' %}" />
                <a class="btn btn-danger" href="{{ pool.get_delete_url }}" title="{% trans 'You will have to confirm on the next page.' %}">{%  trans 'Delete this pool' %}</a>
            {% else %}
                <input class="btn btn-primary" type="submit" value="{% trans 'Create' %}" />
            {% endif %}
        </div>

    </form>
{% endblock %}
