{% extends "pretixcontrol/items/base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% load escapejson %}
{% load formset_tags %}
{% block title %}{% blocktrans with name=question.question %}Question: {{ name }}{% endblocktrans %}{% endblock %}
{% block inside %}
    <h1>
        {% blocktrans with name=question.question %}Question: {{ name }}{% endblocktrans %}
        {% if 'event.items:write' in request.eventpermset %}
            <a href="{% url "control:event.items.questions.edit" event=request.event.slug organizer=request.event.organizer.slug question=question.pk %}"
                    class="btn btn-default">
                <span class="fa fa-edit"></span>
                {% trans "Edit question" %}
            </a>
        {% endif %}
    </h1>

    {% if 'event.orders:read' in request.eventpermset %}
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">{% trans "Filter" %}</h3>
            </div>
            <form class="panel-body filter-form" action="" method="get">
                <div class="row">
                     <div class="col-md-2 col-xs-6">
                         {% bootstrap_field form.status %}
                     </div>
                     <div class="col-md-3 col-xs-6">
                         {% bootstrap_field form.item %}
                     </div>
                {% if has_subevents %}
                    <div class="col-md-3 col-xs-6">
                        {% bootstrap_field form.subevent %}
                    </div>
                    <div class="col-md-4 col-xs-6">
                        {% bootstrap_field form.date_range %}
                    </div>
                {% endif %}
                </div>
                <div class="text-right">
                    <button class="btn btn-primary btn-lg" type="submit">
                        <span class="fa fa-filter"></span>
                        {% trans "Filter" %}
                    </button>
                </div>
            </form>
        </div>
    {% endif %}

    <div class="row">
        {% if 'event.orders:read' not in request.eventpermset %}
            <div class="empty-collection col-md-10 col-xs-12">
                <p>
                    {% blocktrans trimmed %}
                        No permission to view answers.
                    {% endblocktrans %}
                </p>
            </div>
        {% elif not stats %}
            <div class="empty-collection col-md-10 col-xs-12">
                <p>
                    {% blocktrans trimmed %}
                        No matching answers found.
                    {% endblocktrans %}
                </p>
                {% if not items %}
                    <p>
                        {% trans "You need to assign the question to a product to collect answers." %}
                    </p>

                    <a href="{% url "control:event.items.questions.edit" event=request.event.slug organizer=request.event.organizer.slug question=question.pk %}"
                            class="btn btn-primary btn-lg"><i class="fa fa-edit"></i> {% trans "Edit question" %}</a>
                {% endif %}
            </div>
        {% else %}
            <div class="col-md-5 col-xs-12">
                <div class="chart" id="question_chart" data-type="{{ question.type }}">

                </div>
                {{ stats|json_script:"question-chart-data" }}
            </div>
            <div class="col-md-5 col-xs-12">
                <table class="table table-bordered table-hover">
                    <thead>
                    <tr>
                        <th>{% trans "Answer" %}</th>
                        <th class="text-right">{% trans "Count" %}</th>
                        <th class="text-right">{% trans "% of answers" %}</th>
                        <th class="text-right">{% trans "% of tickets" %}</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for stat in stats %}
                        <tr>
                            <td>
                                <a href="{% url "control:event.orders" event=request.event.slug organizer=request.event.organizer.slug %}?status={{ request.GET.status|default:"np" }}&item={{ request.GET.item }}&subevent={{ request.GET.subevent }}&question={{ question.pk }}&answer={{ stat.alink|default:stat.answer|urlencode }}">
                                    {{ stat.answer }}
                                </a>
                            </td>
                            <td class="text-right">{{ stat.count }}</td>
                            <td class="text-right">{{ stat.percentage|floatformat:1 }} %</td>
                            <td class="text-right">{{ stat.percentage_attendees|floatformat:1 }} %</td>
                        </tr>
                    {% endfor %}
                    </tbody>
                    <tfoot>
                        <tr>
                            <td><strong>{% trans "Sum" %}</strong></td>
                            <td class="text-right"><strong>{{ total }}</strong></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        {% endif %}
        <div class="col-xs-12 col-lg-2">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        {% trans "Question history" %}
                    </h3>
                </div>
                {% include "pretixcontrol/includes/logs.html" with obj=question %}
            </div>
        </div>
    </div>
{% endblock %}
