{% extends "base.html" %}

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

As surveys are automatically generated, this is only to to dsplay the pools and manage their
``is_displayed`` flags.

Context
-------
survey : instance of ``mtp.apps.surveys.models.Survey``
    The survey for which we display the polss.
form : instance of ``django.forms.models.ModelForm``
    The form to edit the survey. Does not include any editable fields.
pools_formset : instance of ``mtp.apps.surveys.forms.SurveyPoolsFormSet``
    The editable list of pools for the current survey.
pool_forms : list
    A list of two entries, each one being a tuple with::
    - a boolean indicating if the current entry is related to global pools or not
    - an extract of ``pools_formset`` containing only global or non global pools

Notes
-----

The list of pools is currently two lists, one with the global pools, one with the ones owned by
the user.

{% endcomment %}

{% load i18n %}

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

{% block subtitle %}
    {% blocktrans trimmed with training_name=survey.training.name %}
        Edit the survey for the training "{{ training_name }}"
    {% endblocktrans %}
{% endblock %}

{% block actions %}
    <a class="btn btn-default" href="{{ survey.training.get_content_url }}">{% trans 'Back to the training' %}</a>
    <a class="btn btn-default" href="{{ user.get_pools_url }}">{% trans 'Back to your pools' %}</a>
{% endblock %}

{% block main %}
    {% if form.errors or pools_formset.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="{{ survey.training.get_survey_url }}" method="post" enctype="multipart/form-data">{% csrf_token %}
        {{ form.media }}
        {{ pools_formset.management_form }}
        <table class="table table-hover">
            <tbody>

                {% for field in form.hidden_fields %}
                    {{  field }}
                {% endfor %}

                {% for field in form.visible_fields %}
                    <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>

        <h3>{% trans 'Choose the pools of questions you want to display for this training.' %}</h3>

        <div class="alert alert-info">
            <ul>
                <li>{% trans 'Only pools with at least one question are in the survey.' %}</li>
                <li>{% trans 'You can only hide pools that are not mandatory.' %}</li>
            </ul>
        </div>

        {% if pools_formset.non_field_errors %}
            <div class="alert alert-danger">
                {{ pools_formset.non_field_errors }}
            </div>
        {% endif %}

        {% for pool_form in pools_formset %}
            {% for field in pool_form.hidden_fields %}
                {{  field }}
            {% endfor %}
        {% endfor %}

        {% for is_global, forms in pool_forms %}

            <table class="table table-hover">
                <thead>
                    <tr>
                        <th colspan="3">
                            {% if is_global %}
                                {% trans 'Global pools' %}
                            {% else %}
                                {% trans 'Your pools' %}
                            {% endif %}
                        </th>
                    </tr>
                    <tr>
                        <th>{% trans 'Pool'%}</th>
                        <th>{% trans 'Mandatory'%}</th>
                        <th>{% trans 'Displayed'%}</th>
                    </tr>
                </thead>
                <tbody>
                    {% for pool_form in forms %}
                        {% with pool_survey=pool_form.instance pool=pool_form.instance.pool %}
                            <tr>
                                <td>
                                    {{ pool.name }}
                                    <em>({% blocktrans trimmed count nb_questions=pool_survey.count_questions %}
                                        {{ nb_questions }} question
                                    {% plural %}
                                        {{ nb_questions }} questions
                                    {% endblocktrans %})</em>
                                    {% if pool_form.instance.is_new %}
                                        <span class="label label-info">{% trans 'new' %}</span>
                                    {% endif %}
                                </td>
                                <td title="{% trans 'Read only' %}" class="read-only" width="20%">
                                    {%  if pool.is_mandatory %}
                                        <i class="fa fa-check-square-o"></i>
                                    {% else %}
                                        <i class="fa fa-remove"></i>
                                    {% endif %}
                                </td>
                                <td{% if pool.is_mandatory %} title="{% trans 'Read only' %}" class="read-only"{% endif %} width="20%">
                                    {%  if pool.is_mandatory %}
                                        <i class="fa fa-check-square-o"></i>
                                    {% else %}
                                        {{ pool_form.is_displayed }}
                                    {% endif %}
                                </td>
                            </tr>
                        {% endwith %}
                    {% endfor %}
                </tbody>
            </table>

        {% endfor %}

        <div class="well">
            <input class="btn btn-primary" type="submit" value="{% trans 'Save' %}" />
        </div>
    </form>
{% endblock %}
