{% extends "pretixcontrol/event/base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% load urlreplace %}
{% load money %}
{% block title %}{% trans "Vouchers" %}{% endblock %}
{% block content %}
    <h1>{% trans "Vouchers" %}</h1>
    <p>
        {% blocktrans trimmed %}
            Vouchers allow you to assign tickets to specific persons for a lower price. They also enable you to
            reserve some quota for your very special guests.
        {% endblocktrans %}
    </p>
    <form class="form-inline"
            action="{% url "control:event.vouchers.go" event=request.event.slug organizer=request.event.organizer.slug %}">
        <p class="input-group">
            <input type="text" name="code" class="form-control" placeholder="{% trans "Voucher code" %}" autofocus>
            <span class="input-group-btn">
                    <button class="btn btn-primary" type="submit">{% trans "Go!" %}</button>
                </span>
        </p>
    </form>
    <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-lg-2 col-sm-6 col-xs-6">
                    {% bootstrap_field filter_form.search %}
                </div>
                {% if request.event.has_subevents %}
                    <div class="col-lg-2 col-sm-6 col-xs-6">
                        {% bootstrap_field filter_form.tag %}
                    </div>
                    <div class="col-lg-2 col-sm-6 col-xs-6">
                        {% bootstrap_field filter_form.subevent %}
                    </div>
                {% else %}
                    <div class="col-lg-4 col-sm-6 col-xs-6">
                        {% bootstrap_field filter_form.tag %}
                    </div>
                {% endif %}
                <div class="col-lg-2 col-sm-6 col-xs-6">
                    {% bootstrap_field filter_form.status %}
                </div>
                <div class="col-lg-2 col-sm-6 col-xs-6">
                    {% bootstrap_field filter_form.itemvar %}
                </div>
                <div class="col-lg-2 col-sm-6 col-xs-6">
                    {% bootstrap_field filter_form.qm %}
                </div>
            </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>
    {% if vouchers|length == 0 %}
        <div class="empty-collection">
            <p>
                {% if request.GET.search or request.GET.tag or request.GET.status or request.GET.subevent %}
                    {% trans "Your search did not match any vouchers." %}
                {% else %}
                    {% blocktrans trimmed %}
                        You haven't created any vouchers yet.
                    {% endblocktrans %}
                {% endif %}
            </p>

            {% if "can_change_vouchers" in request.eventpermset %}
                <a href="{% url "control:event.vouchers.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 voucher" %}</a>
                <a href="{% url "control:event.vouchers.bulk" organizer=request.event.organizer.slug event=request.event.slug %}"
                        class="btn btn-primary btn-lg"><i class="fa fa-plus"></i> {% trans "Create multiple new vouchers" %}</a>
                <a href="{% url "control:event.vouchers.import" organizer=request.event.organizer.slug event=request.event.slug %}"
                   class="btn btn-default btn-lg"><i class="fa fa-upload"></i> {% trans "Import vouchers" %}</a>
            {% endif %}
        </div>
    {% else %}
        <p>
            {% if "can_change_vouchers" in request.eventpermset %}
                <a href="{% url "control:event.vouchers.add" organizer=request.event.organizer.slug event=request.event.slug %}"
                        class="btn btn-default"><i class="fa fa-plus"></i> {% trans "Create a new voucher" %}</a>
                <a href="{% url "control:event.vouchers.bulk" organizer=request.event.organizer.slug event=request.event.slug %}"
                        class="btn btn-default"><i class="fa fa-plus"></i>
                    {% trans "Create multiple new vouchers" %}</a>
                <a href="{% url "control:event.vouchers.import" organizer=request.event.organizer.slug event=request.event.slug %}" 
                   class="btn btn-default"><i class="fa fa-upload"></i>
                    {% trans "Import vouchers" %}</a>
            {% endif %}
            <a href="?{% url_replace request "download" "yes" %}"
                    class="btn btn-default"><i class="fa fa-download"></i>
                {% trans "Download list" %}</a>
        </p>
        <form action="{% url "control:event.vouchers.bulkaction"  organizer=request.event.organizer.slug event=request.event.slug %}" method="post">
            {% csrf_token %}
            <div class="table-responsive">
                <table class="table table-hover table-quotas">
                    <thead>
                    <tr>
                        {% if "can_change_vouchers" 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 "Voucher code" %}
                            <a href="?{% url_replace request 'ordering' '-code' %}"><i class="fa fa-caret-down"></i></a>
                            <a href="?{% url_replace request 'ordering' 'code' %}"><i class="fa fa-caret-up"></i></a>
                        </th>
                        <th>
                            {% trans "Redemptions" %}
                            <a href="?{% url_replace request 'ordering' '-redeemed' %}"><i class="fa fa-caret-down"></i></a>
                            <a href="?{% url_replace request 'ordering' 'redeemed' %}"><i class="fa fa-caret-up"></i></a>
                        </th>
                        <th>
                            {% trans "Expiry" %}
                            <a href="?{% url_replace request 'ordering' '-valid_until' %}"><i class="fa fa-caret-down"></i></a>
                            <a href="?{% url_replace request 'ordering' 'valid_until' %}"><i class="fa fa-caret-up"></i></a>
                        </th>
                        <th>
                            {% trans "Tag" %}
                            <a href="?{% url_replace request 'ordering' '-tag' %}"><i class="fa fa-caret-down"></i></a>
                            <a href="?{% url_replace request 'ordering' 'tag' %}"><i class="fa fa-caret-up"></i></a>
                        </th>
                        <th>
                            {% trans "Product" %}
                            <a href="?{% url_replace request 'ordering' '-item' %}"><i class="fa fa-caret-down"></i></a>
                            <a href="?{% url_replace request 'ordering' 'item' %}"><i class="fa fa-caret-up"></i></a>
                        </th>
                        {% if request.event.has_subevents %}
                            <th>
                                {% trans "Date" context "subevent" %}
                                <a href="?{% url_replace request 'ordering' '-subevent' %}"><i class="fa fa-caret-down"></i></a>
                                <a href="?{% url_replace request 'ordering' 'subevent' %}"><i class="fa fa-caret-up"></i></a>
                            </th>
                        {% endif %}
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for v in vouchers %}
                        <tr>
                            {% if "can_change_vouchers" in request.eventpermset %}
                                <td>
                                    <label aria-label="{% trans "select row for batch-operation" %}" class="batch-select-label">
                                        <input type="checkbox" name="voucher" class="batch-select-checkbox" value="{{ v.pk }}"/>
                                    </label>
                                </td>
                            {% endif %}
                            <td>
                                {% if not v.is_active %}<del>{% endif %}
                                <strong><a href="{% url "control:event.voucher" organizer=request.event.organizer.slug event=request.event.slug voucher=v.id %}">{{ v.code }}</a></strong>
                                {% if not v.is_active %}</del>{% endif %}
                            </td>
                            <td>
                                {{ v.redeemed }} / {{ v.max_usages }}
                                {% if v.budget|default_if_none:"NONE" != "NONE" %}
                                    <br>
                                    <small class="text-muted">
                                        {{ v.budget_used_orders|money:request.event.currency }} / {{ v.budget|money:request.event.currency }}
                                    </small>
                                {% endif %}
                            </td>
                            <td>{{ v.valid_until|date }}</td>
                            <td>
                                {{ v.tag }}
                            </td>
                            <td>
                                {% if v.item %}
                                    {{ v.item }}
                                    {% if v.variation %}
                                        – {{ v.variation }}
                                    {% endif %}
                                {% elif v.quota %}
                                    {% blocktrans trimmed with quota=v.quota.name %}
                                        Any product in quota "{{ quota }}"
                                    {% endblocktrans %}
                                {% endif %}
                                {% if v.seat %}<br><small class="text-muted">{{ v.seat }}</small>{% endif %}
                            </td>
                            {% if request.event.has_subevents %}
                                <td>
                                    {{ v.subevent.name }} – {{ v.subevent.get_date_range_display }}
                                    {% if request.event.settings.show_times %}
                                        {{ v.subevent.date_from|date:"TIME_FORMAT" }}
                                    {% endif %}
                                </td>
                            {% endif %}
                            <td class="text-right flip">
                                {% if "can_change_vouchers" in request.eventpermset %}
                                    <a href="{% url "control:event.vouchers.bulk" organizer=request.event.organizer.slug event=request.event.slug %}?copy_from={{ v.id }}"
                                       class="btn btn-sm btn-default" title="{% trans "Use as a template for new vouchers" %}" data-toggle="tooltip">
                                        <span class="fa fa-copy"></span>
                                    </a>
                                    <a href="{% url "control:event.voucher.delete" organizer=request.event.organizer.slug event=request.event.slug voucher=v.id %}" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            {% if "can_change_vouchers" in request.eventpermset %}
                <div class="batch-select-actions">
                    <button type="submit" class="btn btn-danger" name="action" value="delete">
                        <i class="fa fa-trash" aria-hidden="true"></i>
                        {% trans "Delete selected" %}
                    </button>
                </div>
            {% endif %}
        </form>
        {% include "pretixcontrol/pagination.html" %}
    {% endif %}
{% endblock %}
