{% extends "pretixcontrol/items/base.html" %}
{% load i18n %}
{% load urlreplace %}
{% load bootstrap3 %}
{% block title %}{% trans "Quotas" %}{% endblock %}
{% block inside %}
    <h1>{% trans "Quotas" %}</h1>
    <p>
        {% blocktrans trimmed %}
            To make your products actually available, you also need quotas. Quotas define, how many instances of
            your product pretix will sell. This way, you can configure whether your event can take an unlimited
            number of attendees or the number of attendees is limited. You can assign a product to multiple quotas
            to fulfill more complex requirements, e.g. if you want to limit the total number of tickets sold and the
            number of a specific ticket type at the same time.
        {% endblocktrans %}
    </p>
    {% if quotas|length == 0 and not filter_form.filtered %}
        <div class="empty-collection">
            <p>
                {% blocktrans trimmed %}
                    You haven't created any quotas yet.
                {% endblocktrans %}
            </p>

            {% if 'event.items:write' in request.eventpermset %}
                <a href="{% url "control:event.items.quotas.add" organizer=request.event.organizer.slug event=request.event.slug %}"
                        class="btn btn-primary btn-lg"><i class="fa fa-plus"></i> {% trans "Create a new quota" %}</a>
            {% endif %}
        </div>
    {% else %}

        <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="{% if not filter_form.subevent %}col-lg-6{% else %}col-lg-2{% endif %} col-md-6 col-sm-6 col-xs-12">
                        {% bootstrap_field filter_form.query %}
                    </div>
                    {% if filter_form.subevent %}
                        <div class="col-lg-2 col-md-6 col-md-2 col-sm-6 col-xs-12">
                            {% bootstrap_field filter_form.subevent %}
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-6 col-xs-12">
                            {% bootstrap_field filter_form.date_from %}
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-6 col-xs-12">
                            {% bootstrap_field filter_form.date_until %}
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-6 col-xs-12">
                            {% bootstrap_field filter_form.time_from %}
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-6 col-xs-12">
                            {% bootstrap_field filter_form.time_until %}
                        </div>
                        <div class="col-xs-12 one-line-checkboxes">
                            {% bootstrap_field filter_form.weekday %}
                        </div>
                    {% endif %}
                </div>

                <div class="text-right flip">
                    <button class="btn btn-primary btn-lg" type="submit">
                        <span class="fa fa-filter"></span>
                        {% trans "Filter" %}
                    </button>
                </div>
            </form>
        </div>
        {% if 'event.items:write' in request.eventpermset %}
            <p>
                <a href="{% url "control:event.items.quotas.add" organizer=request.event.organizer.slug event=request.event.slug %}" class="btn btn-default"><i class="fa fa-plus"></i> {% trans "Create a new quota" %}
                </a>
            </p>
        {% endif %}
        <form action="{% url "control:event.items.quotas.bulkaction"  organizer=request.event.organizer.slug event=request.event.slug %}" method="post">
            {% csrf_token %}
            {% for field in filter_form %}
                {{ field.as_hidden }}
            {% endfor %}
            <div class="table-responsive">
                <table class="table table-hover table-quotas">
                    <thead>
                    <tr>
                        {% if "event.items:write" in request.eventpermset %}
                            <th>
                                <label aria-label="{% trans "select all rows for batch-operation" %}" class="batch-select-label"><input type="checkbox" data-toggle-table/></label>
                            </th>
                        {% endif %}
                        <th>{% trans "Quota name" %}
                            <a href="?{% url_replace request 'filter-ordering' '-name' %}"><i class="fa fa-caret-down"></i></a>
                            <a href="?{% url_replace request 'filter-ordering' 'name' %}"><i class="fa fa-caret-up"></i></a>
                        </th>
                        <th>{% trans "Products" %}</th>
                        {% if request.event.has_subevents %}
                            <th>{% trans "Date" context "subevent" %}
                                <a href="?{% url_replace request 'filter-ordering' '-date' %}"><i class="fa fa-caret-down"></i></a>
                                <a href="?{% url_replace request 'filter-ordering' 'date' %}"><i class="fa fa-caret-up"></i></a>
                            </th>
                        {% endif %}
                        <th>{% trans "Total capacity" %}
                            <a href="?{% url_replace request 'filter-ordering' '-size' %}"><i class="fa fa-caret-down"></i></a>
                            <a href="?{% url_replace request 'filter-ordering' 'size' %}"><i class="fa fa-caret-up"></i></a>
                        </th>
                        <th>{% trans "Capacity left" %}</th>
                        <th class="action-col-2"></th>
                    </tr>
                    {% if "event.items:write" in request.eventpermset and page_obj.paginator.num_pages > 1 %}
                        <tr class="table-select-all warning hidden">
                            <td>
                                <input type="checkbox" name="__ALL" id="__all" data-results-total="{{ page_obj.paginator.count }}">
                            </td>
                            <td colspan="6">
                                <label for="__all">
                                    {% trans "Select all results on other pages as well" %}
                                </label>
                            </td>
                        </tr>
                    {% endif %}
                    </thead>
                    <tbody>
                    {% for q in quotas %}
                        <tr>
                            {% if "event.items:write" in request.eventpermset %}
                                <td>
                                    <label aria-label="{% trans "select row for batch-operation" %}" class="batch-select-label"><input type="checkbox" name="quota" class="batch-select-checkbox" value="{{ q.pk }}"/></label>
                                </td>
                            {% endif %}
                            <td>
                                <strong><a href="{% url "control:event.items.quotas.show" organizer=request.event.organizer.slug event=request.event.slug quota=q.id %}">{{ q.name }}</a></strong>
                                {% if q.ignore_for_event_availability %}
                                    <span class="fa fa-eye-slash text-muted" data-toggle="tooltip" title="{% trans "Ignore this quota when determining event availability" %}"></span>
                                {% endif %}
                            </td>
                            <td>
                                <ul>
                                    {% for item in q.cached_items %}
                                        {% if not item.has_variations %}
                                            <li><a href="{% url "control:event.item" organizer=request.event.organizer.slug event=request.event.slug item=item.id %}">{{ item }}</a></li>
                                        {% endif %}
                                    {% endfor %}
                                    {% for v in q.variations.all %}
                                        <li><a href="{% url "control:event.item" organizer=request.event.organizer.slug event=request.event.slug item=v.item.id %}#tab-0-3-open">
                                            {{ v.item }} – {{ v }}</a></li>
                                    {% endfor %}
                                </ul>
                            </td>
                            {% if request.event.has_subevents %}
                                <td>
                                    {{ q.subevent.name }} – {{ q.subevent.get_date_range_display_with_times }}
                                </td>
                            {% endif %}
                            <td>{% if q.size == None %}Unlimited{% else %}{{ q.size }}{% endif %}</td>
                            <td>{% include "pretixcontrol/items/fragment_quota_availability.html" with availability=q.cached_avail closed=q.closed %}</td>
                            <td class="text-right flip">
                                {% if 'event.items:write' in request.eventpermset %}
                                    <a href="{% url "control:event.items.quotas.edit" organizer=request.event.organizer.slug event=request.event.slug quota=q.id %}" class="btn btn-default btn-sm"><i class="fa fa-edit"></i></a>
                                    <a href="{% url "control:event.items.quotas.add" organizer=request.event.organizer.slug event=request.event.slug %}?copy_from={{ q.id }}"
                                       class="btn btn-sm btn-default" title="{% trans "Clone" %}" data-toggle="tooltip">
                                        <span class="fa fa-copy"></span>
                                    </a>
                                    <a href="{% url "control:event.items.quotas.delete" organizer=request.event.organizer.slug event=request.event.slug quota=q.id %}" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            {% if "event.items:write" in request.eventpermset %}
                <div class="batch-select-actions">
                    <button type="submit" class="btn btn-danger btn-save" name="action" value="delete">
                        <i class="fa fa-trash"></i>{% trans "Delete selected" %}
                    </button>
                    <button type="submit" class="btn btn-primary btn-save" name="action" value="edit"
                            formaction="{% url "control:event.items.quotas.bulkedit"  organizer=request.event.organizer.slug event=request.event.slug %}">
                        <i class="fa fa-edit"></i>{% trans "Edit selected" %}
                    </button>
                </div>
            {% endif %}
        </form>
    {% endif %}
    {% include "pretixcontrol/pagination.html" %}
{% endblock %}
