{% extends "pretixcontrol/event/base.html" %}
{% load i18n %}
{% load eventurl %}
{% load money %}
{% load urlreplace %}
{% block title %}{% trans "Waiting list" %}{% endblock %}
{% block content %}
    <h1>{% trans "Waiting list" %}</h1>
    {% if not request.event.settings.waiting_list_enabled %}
        <div class="alert alert-danger">
            {% trans "The waiting list is disabled, so if the event is sold out, people cannot add themselves to this list. If you want to enable it, go to the event settings." %}
        </div>
    {% elif not request.event.waiting_list_active and not request.event.has_subevents %}
        <div class="alert alert-danger">
            {% trans "The waiting list is no longer active for this event. The waiting list no longer affects quotas and no longer notifies waiting users." %}
        </div>
    {% endif %}
    <div class="row">
        {% if 'can_change_orders' in request.eventpermset %}
            <form method="post" class="col-md-6"
                    action="{% url "control:event.orders.waitinglist.auto" event=request.event.slug organizer=request.organizer.slug %}"
                    data-asynctask>
                <div class="panel panel-default">
                    <div class="panel-heading">
                        {% trans "Send vouchers" %}
                    </div>
                    <div class="panel-body form-inline">
                        {% csrf_token %}
                        {% if request.event.settings.waiting_list_auto %}
                            <p>
                                {% blocktrans trimmed %}
                                    You have configured that vouchers will automatically be sent to the persons on this list who waited
                                    the longest as soon as capacity becomes available. It might take up to half an hour for the
                                    vouchers to be sent after the capacity is available, so don't worry if entries do not disappear
                                    here immediately. If you want, you can also send them out manually right now.
                                {% endblocktrans %}
                            </p>
                            {% if not running %}
                                <div class="alert alert-warning">
                                    {% trans "Currently, no vouchers will be sent since your event is not live or is not selling tickets." %}
                                </div>
                            {% endif %}
                        {% else %}
                            <p>
                                {% blocktrans trimmed %}
                                    You have configured that vouchers will <strong>not</strong> be sent automatically.
                                    You can either send them one-by-one in an order of your choice by clicking the
                                    buttons next to a line in this table (if sufficient quota is available) or you can
                                    press the big button below this text to send out as many vouchers as currently
                                    possible to the persons who waited longest.
                                {% endblocktrans %}
                            </p>
                        {% endif %}
                        {% if request.event.has_subevents %}
                            <select name="subevent" class="form-control">
                                <option value="">{% trans "All dates" context "subevent" %}</option>
                                {% for se in request.event.subevents.all %}
                                    <option value="{{ se.id }}"
                                            {% if request.GET.subevent|add:0 == se.id %}selected="selected"{% endif %}>
                                        {{ se.name }} – {{ se.get_date_range_display }}
                                    </option>
                                {% endfor %}
                            </select>
                        {% endif %}
                        <button class="btn btn-large btn-primary" type="submit">
                            {% trans "Send as many vouchers as possible" %}
                        </button>
                    </div>
                </div>
            </form>
        {% endif %}
        <div class="{% if 'can_change_orders' in request.eventpermset %}col-md-6{% else %}col-md-12{% endif %}">
            <div class="panel panel-default">
                <div class="panel-heading">
                    {% trans "Sales estimate" %}
                </div>
                <div class="panel-body">
                    {% blocktrans trimmed with amount=estimate|default:0|money:request.event.currency %}
                        If you can make enough room at your event to fit all the persons on the waiting list in, you
                        could sell tickets worth an additional <strong>{{ amount }}</strong>.
                    {% endblocktrans %}
                </div>
            </div>
        </div>
    </div>

    <p>
    <form class="form-inline helper-display-inline" action="" method="get">
        <select name="status" class="form-control">
            <option value="a"
                    {% if request.GET.status == "p" %}selected="selected"{% endif %}>{% trans "All entries" %}</option>
            <option value="w"
                    {% if request.GET.status == "w" or not request.GET.status %}selected="selected"{% endif %}>
                {% trans "Waiting for a voucher" %}</option>
            <option value="s"
                    {% if request.GET.status == "s" %}selected="selected"{% endif %}>{% trans "Voucher assigned" %}</option>
            <option value="v"
                    {% if request.GET.status == "v" %}selected="selected"{% endif %}>
                {% trans "Waiting for redemption" %}</option>
            <option value="r"
                    {% if request.GET.status == "r" %}selected="selected"{% endif %}>
                {% trans "Successfully redeemed" %}</option>
            <option value="e"
                    {% if request.GET.status == "e" %}selected="selected"{% endif %}>
                {% trans "Voucher expired" %}</option>
        </select>
        <select name="item" class="form-control">
            <option value="">{% trans "All products" %}</option>
            {% for item in items %}
                <option value="{{ item.id }}"
                        {% if request.GET.item|add:0 == item.id %}selected="selected"{% endif %}>
                    {{ item }}
                </option>
            {% endfor %}
        </select>
        {% if request.event.has_subevents %}
            <select name="subevent" class="form-control">
                <option value="">{% trans "All dates" context "subevent" %}</option>
                {% for se in request.event.subevents.all %}
                    <option value="{{ se.id }}"
                            {% if request.GET.subevent|add:0 == se.id %}selected="selected"{% endif %}>
                        {{ se.name }} – {{ se.get_date_range_display }}
                    </option>
                {% endfor %}
            </select>
        {% endif %}
        <button class="btn btn-primary" type="submit">{% trans "Filter" %}</button>
        <a href="?{% url_replace request "download" "yes" %}"
                class="btn btn-default"><i class="fa fa-download"></i>
            {% trans "Download list" %}</a>
    </form>
    </p>
    <form action="{% url "control:event.orders.waitinglist.action"  organizer=request.event.organizer.slug event=request.event.slug %}?next={{ request.get_full_path|urlencode }}" method="post">
        {% csrf_token %}
        <input name="subevent" type="hidden" value="{{ request.GET.subevent }}">
        <input name="item" type="hidden" value="{{ request.GET.item }}">
        <input name="status" type="hidden" value="{{ request.GET.status }}">
        <div class="table-responsive">
            <table class="table table-condensed table-hover">
                <thead>
                <tr>
                    <th>
                        {% if "can_change_orders" in request.eventpermset %}
                            <label aria-label="{% trans "select all rows for batch-operation" %}" class="batch-select-label"><input type="checkbox" data-toggle-table/></label>
                        {% endif %}
                    </th>
                    {% if request.event.settings.waiting_list_names_asked %}
                    <th>{% trans "Name" %}</th>
                    {% endif %}
                    <th>{% trans "Email" %}</th>
                    {% if request.event.settings.waiting_list_phones_asked %}
                    <th>{% trans "Phone number" %}</th>
                    {% endif %}
                    <th>{% trans "Product" %}</th>
                    {% if request.event.has_subevents %}
                        <th>{% trans "Date" context "subevent" %}</th>
                    {% endif %}
                    <th>{% trans "On the list since" %}</th>
                    <th>{% trans "Status" %}</th>
                    <th>{% trans "Voucher" %}</th>
                    <th></th>
                </tr>
                {% if "can_change_orders" 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="5">
                            <label for="__all">
                                {% trans "Select all results on other pages as well" %}
                            </label>
                        </td>
                    </tr>
                {% endif %}
                </thead>
                <tbody>
                {% for e in entries %}
                    <tr>
                        <td>
                            {% if "can_change_orders" in request.eventpermset %}
                                <label aria-label="{% trans "select row for batch-operation" %}" class="batch-select-label"><input type="checkbox" name="entry" class="batch-select-checkbox" value="{{ e.pk }}"/></label>
                            {% endif %}
                        </td>
                        {% if request.event.settings.waiting_list_names_asked %}
                        <td>{{ e.name_all_components|default:"" }}</td>
                        {% endif %}
                        <td>{{ e.email }}</td>
                        {% if request.event.settings.waiting_list_phones_asked %}
                        <td>{{ e.phone|default:"" }}</td>
                        {% endif %}
                        <td>
                            {{ e.item }}
                            {% if e.variation %}
                                – {{ e.variation }}
                            {% endif %}
                        </td>
                        {% if request.event.has_subevents %}
                            <td>{{ e.subevent.name }} – {{ e.subevent.get_date_range_display }}</td>
                        {% endif %}
                        <td>
                            {{ e.created|date:"SHORT_DATETIME_FORMAT" }}
                            {% if e.priority != 0 %}
                                <span class="label label-info" data-toggle="tooltip"
                                    title="{% trans "This entry has a modified priority. The higher this number is, the earlier this person will be assigned a voucher." %}">
                                    {% if e.priority > 0 %}+{% endif %}{{ e.priority }}
                                </span>
                            {% endif %}
                        </td>
                        <td>
                            {% if e.voucher %}
                                {% if e.voucher.redeemed >= e.voucher.max_usages %}
                                    <span class="label label-success">{% trans "Voucher redeemed" %}</span>
                                {% elif not e.voucher.is_active %}
                                    <span class="label label-danger">{% trans "Voucher expired" %}</span>
                                {% else %}
                                    <span class="label label-warning">{% trans "Voucher assigned" %}</span>
                                {% endif %}
                            {% elif e.availability.0 == 100 %}
                                {% if e.availability.1|default_if_none:"none" == "none" %}
                                    <span class="label label-danger" data-toggle="tooltip"
                                        title="{% trans "For safety reasons, the waiting list does not run if the quota is set to unlimited."  %}">
                                        <span class="fa fa-ban" aria-hidden="true"></span>
                                        {% blocktrans trimmed %}
                                            Quota unlimited
                                        {% endblocktrans %}
                                    </span>
                                {% else %}
                                    <span class="label label-warning">
                                        {% blocktrans with num=e.availability.1 %}
                                            Waiting, product {{ num }}x available
                                        {% endblocktrans %}
                                    </span>
                                {% endif %}
                            {% else %}
                                <span class="label label-danger">{% trans "Waiting, product unavailable" %}</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if e.voucher %}
                                <a href="{% url "control:event.voucher" organizer=request.event.organizer.slug event=request.event.slug voucher=e.voucher.pk %}">
                                    {{ e.voucher }}
                                </a>
                            {% elif not e.voucher and e.availability.0 == 100 and e.availability.1|default_if_none:"none" != "none" %}
                                <button name="assign" value="{{ e.pk }}" class="btn btn-default btn-xs">
                                    {% trans "Send a voucher" %}
                                </button>
                            {% endif %}
                        </td>
                        <td class="text-right flip">
                            {% if not e.voucher %}
                                <button name="move_top" value="{{ e.pk }}" class="btn btn-default btn-sm"
                                        data-toggle="tooltip" title="{% trans "Move to the top of the list" %}">
                                    <span class="fa fa-thumbs-up"></span>
                                </button>
                                <button name="move_end" value="{{ e.pk }}" class="btn btn-default btn-sm"
                                    data-toggle="tooltip" title="{% trans "Move to the end of the list" %}">
                                    <span class="fa fa-thumbs-down"></span>
                                </button>
                                {% if request.event.has_subevents %}
                                    <a href="{% url "control:event.orders.waitinglist.transfer" organizer=request.event.organizer.slug event=request.event.slug entry=e.id %}"
                                       class="btn btn-default btn-sm" title="{% trans "Transfer to other date" context "subevent" %}"
                                        data-toggle="tooltip">
                                        <i class="fa fa-calendar" aria-hidden="true"></i>
                                    </a>
                                {% endif %}
                                <a href="{% url "control:event.orders.waitinglist.delete" organizer=request.event.organizer.slug event=request.event.slug entry=e.id %}?next={{ request.get_full_path|urlencode }}" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>
                            {% else %}
                                <button class="btn btn-default btn-sm disabled">
                                    <span class="fa fa-thumbs-up"></span>
                                </button>
                                <button class="btn btn-default btn-sm disabled">
                                    <span class="fa fa-thumbs-down"></span>
                                </button>
                                <span class="btn btn-danger btn-sm disabled"><i class="fa fa-trash"></i></span>
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {% if "can_change_orders" 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>
            </div>

        {% endif %}
    </form>
    {% include "pretixcontrol/pagination.html" %}
{% endblock %}
