{% extends "pretixcontrol/items/base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% load escapejson %}
{% load eventsignal %}
{% block title %}{% blocktrans with name=quota.name %}Quota: {{ name }}{% endblocktrans %}{% endblock %}
{% block inside %}
    <h1>
        {% blocktrans with name=quota.name %}Quota: {{ name }}{% endblocktrans %}
        {% if 'can_change_items' in request.eventpermset %}
            <a href="{% url "control:event.items.quotas.edit" event=request.event.slug organizer=request.event.organizer.slug quota=quota.pk %}"
               class="btn btn-default">
                <span class="fa fa-edit"></span>
                {% trans "Edit quota" %}
            </a>
        {% endif %}
    </h1>
    {% if quota.subevent %}
        <p>
            <span class="fa fa-calendar"></span> {{ quota.subevent.name }} – {{ quota.subevent.get_date_range_display }}
        </p>
    {% endif %}

    <form action="" method="post">
        {% csrf_token %}
        {% if quota.closed %}
            {% if closed_and_sold_out %}
                <div class="alert alert-info">
                    <button type="submit" class="btn btn-default pull-right flip" name="disable" value="true">
                        {% trans "Open quota and disable closing" %}
                    </button>
                    {% trans "This quota is sold out and closed. Even if tickets become available e.g. through cancellations, they will not become available again unless you manually re-open the quota on this page." %}
                    <div class="clearfix"></div>
                </div>
            {% else %}
                <div class="alert alert-warning">
                    <button type="submit" class="btn btn-primary pull-right flip" name="reopen"
                            value="true">{% trans "Open quota" %}</button>
                    {% trans "This quota is closed since it has been sold out before. Tickets are theoretically available, but will not be sold unless you manually re-open the quota." %}
                    <div class="clearfix"></div>
                </div>
            {% endif %}
        {% endif %}
    </form>
    <div class="row" id="quota-stats">
        <div class="col-md-5 col-xs-12">
            <legend>{% trans "Usage overview" %}</legend>
            <div class="chart" id="quota_chart">

            </div>
            <script type="application/json" id="quota-chart-data">{{ quota_chart_data|escapejson }}</script>
        </div>
        <div class="col-md-5 col-xs-12">
            <legend>{% trans "Availability calculation" %}</legend>
            <div class="row">
                <div class="col-xs-9">{% trans "Total quota" %}</div>
                <div class="col-xs-3 flip text-right">
                    {% if quota.size == None %}{% trans "Infinite" %}{% else %}{{ quota.size }}{% endif %}
                </div>
            </div>
            {% for row in quota_table_rows %}
                <div class="row">
                    <div class="col-xs-9">
                        {% if row.strong %}<strong>{% endif %}
                        {{ row.label }}
                        {% if row.strong %}</strong>{% endif %}
                    </div>
                    <div class="col-xs-3 flip text-right">
                        {% if row.strong %}
                            <strong>{{ row.value }}</strong>
                            {% else %}
                            {% if row.value >= 0 %}– {{ row.value }}{% else %}+ {{ row.value_abs }}{% endif %}
                        {% endif %}
                    </div>
                </div>
            {% endfor %}
            {% if has_plugins > 0 %}
                <div class="alert alert-warning">
                    {% blocktrans trimmed with num=quota_overbooked %}
                        A plugin is active that might modify the actual result of this quota from what you see here.
                    {% endblocktrans %}
                </div>
            {% endif %}
            {% if quota_overbooked > 0 %}
                <div class="alert alert-warning">
                    {% blocktrans trimmed with num=quota_overbooked %}
                        This quota is currently overbooked by {{ num }} tickets.
                    {% endblocktrans %}
                </div>
            {% endif %}
            {% if has_ignore_vouchers %}
                <div class="alert alert-warning">
                    {% blocktrans trimmed %}
                        Your event contains vouchers that affect products covered by this quota and that
                        allow a user to buy products even if this quota is sold out.
                    {% endblocktrans %}
                </div>
            {% endif %}
        </div>
        <div class="col-md-2 col-xs-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        {% trans "Quota history" %}
                    </h3>
                </div>
                {% include "pretixcontrol/includes/logs.html" with obj=quota %}
            </div>
        </div>
    </div>
    {% eventsignal request.event "pretix.control.signals.quota_detail_html" quota=quota %}
{% endblock %}
