{% 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>
    {% 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>
                        {% 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 method="post" action="?next={{ request.get_full_path|urlencode }}">
        {% csrf_token %}
        <div class="table-responsive">
            <table class="table table-condensed table-hover">
                <thead>
                <tr>
                    <th>{% trans "User" %}</th>
                    <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>
                </thead>
                <tbody>
                {% for e in entries %}
                    <tr>
                        <td>{{ e.email }}</td>
                        <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 %}
                                <span class="label label-warning">
                                {% blocktrans with num=e.availability.1 %}
                                    Waiting, product {{ num }}x available
                                {% endblocktrans %}
                            </span>
                            {% 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 %}
                                <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>
                                <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>
    </form>
    {% include "pretixcontrol/pagination.html" %}
{% endblock %}
