{% extends "pretixcontrol/event/base.html" %}
{% load i18n %}
{% load eventurl %}
{% load urlreplace %}
{% load bootstrap3 %}
{% block title %}
    {% blocktrans with name=checkinlist.name %}Check-in list: {{ name }}{% endblocktrans %}
{% endblock %}
{% block content %}
    <h1>
        {% blocktrans with name=checkinlist.name %}Check-in list: {{ name }}{% endblocktrans %}
        {% if 'can_change_event_settings' in request.eventpermset %}
            <a href="{% url "control:event.orders.checkinlists.edit" event=request.event.slug organizer=request.event.organizer.slug list=checkinlist.pk %}"
                    class="btn btn-default">
                <span class="fa fa-wrench"></span>
                {% trans "Edit list configuration" %}
            </a>
        {% endif %}
        <a href="{% url "control:event.orders.checkinlists.simulator" event=request.event.slug organizer=request.event.organizer.slug list=checkinlist.pk %}"
           class="btn btn-default">
            <span class="fa fa-flask"></span>
            {% trans "Check-in simulator" %}
        </a>
        <a href="{% url "control:event.orders.export" event=request.event.slug organizer=request.event.organizer.slug %}?identifier=checkinlistpdf&checkinlistpdf-list={{ checkinlist.pk }}{% if "status" in request.GET %}&checkinlistpdf-status={{ request.GET.status|urlencode }}{% endif %}"
                class="btn btn-default" target="_blank">
            <span class="fa fa-download"></span>
            {% trans "PDF" %}
        </a>
        <a href="{% url "control:event.orders.export" event=request.event.slug organizer=request.event.organizer.slug %}?identifier=checkinlist&checkinlist-list={{ checkinlist.pk }}{% if "status" in request.GET %}&checkinlist-status={{ request.GET.status|urlencode }}{% endif %}"
                class="btn btn-default" target="_blank">
            <span class="fa fa-download"></span>
            {% trans "CSV" %}
        </a>
    </h1>
    <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-md-6 col-sm-6 col-xs-12">
                    {% bootstrap_field filter_form.user %}
                </div>
                <div class="col-md-3 col-sm-6 col-xs-12">
                    {% bootstrap_field filter_form.status %}
                </div>
                <div class="col-md-3 col-sm-6 col-xs-12">
                    {% bootstrap_field filter_form.item %}
                </div>
                {% if filter_form.subevent %}
                    <div class="col-md-6 col-sm-6 col-xs-12">
                        {% bootstrap_field filter_form.subevent %}
                    </div>
                    <div class="col-md-3 col-sm-6 col-xs-12">
                        {% bootstrap_field filter_form.subevent_from %}
                    </div>
                    <div class="col-md-3 col-sm-6 col-xs-12">
                        {% bootstrap_field filter_form.subevent_until %}
                    </div>
                {% endif %}
            </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 entries|length == 0 %}
        <div class="empty-collection">
            <p>
                {% blocktrans trimmed %}
                    No attendee record was found.
                {% endblocktrans %}
            </p>
        </div>
    {% else %}
        <form method="post" action="{% url "control:event.orders.checkinlists.bulk_action" event=request.event.slug organizer=request.event.organizer.slug list=checkinlist.pk %}" data-asynctask>
            {% for field in filter_form %}
                {{ field.as_hidden }}
            {% endfor %}
            <input name="returnquery" type="hidden" value="{{ request.META.QUERY_STRING }}">
           {% csrf_token %}
            <div class="table-responsive">
                <table class="table table-condensed table-hover">
                    <thead>
                    <tr>
                        <th>
                            {% if "can_change_orders" in request.eventpermset or "can_checkin_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>
                        <th>{% trans "Order 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 "Item" %} <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 and not checkinlist.subevent %}
                            <th>{% trans "Date" context "subevents" %} <a href="?{% url_replace request 'ordering' '-date'%}"><i class="fa fa-caret-down"></i></a>
                                <a href="?{% url_replace request 'ordering' 'date'%}"><i class="fa fa-caret-up"></i></a></th>
                        {% endif %}
                        {% if seats %}
                            <th>{% trans "Seat" %} <a href="?{% url_replace request 'ordering' '-seat'%}"><i class="fa fa-caret-down"></i></a>
                                <a href="?{% url_replace request 'ordering' 'seat'%}"><i class="fa fa-caret-up"></i></a></th>
                        {% endif %}
                        <th>{% trans "Email" %} <a href="?{% url_replace request 'ordering' '-email'%}"><i class="fa fa-caret-down"></i></a>
                      <a href="?{% url_replace request 'ordering' 'email'%}"><i class="fa fa-caret-up"></i></a></th>
                        <th>{% trans "Name" %} <a href="?{% url_replace request 'ordering' '-name'%}"><i class="fa fa-caret-down"></i></a>
                      <a href="?{% url_replace request 'ordering' 'name'%}"><i class="fa fa-caret-up"></i></a></th>
                        <th>{% trans "Ticket code" %}</th>
                        <th>{% trans "Status" %} <a href="?{% url_replace request 'ordering' '-status'%}"><i class="fa fa-caret-down"></i></a>
                      <a href="?{% url_replace request 'ordering' 'status'%}"><i class="fa fa-caret-up"></i></a></th>
                        <th>{% trans "Timestamp" %} <a href="?{% url_replace request 'ordering' '-timestamp'%}"><i class="fa fa-caret-down"></i></a>
                      <a href="?{% url_replace request 'ordering' 'timestamp'%}"><i class="fa fa-caret-up"></i></a></th>
                    </tr>
                    {% if 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="8">
                                <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 or "can_checkin_orders" in request.eventpermset %}
                                    <input type="checkbox" name="checkin" id="id_checkin" class="" value="{{ e.pk }}"/>
                                {% endif %}
                            </td>
                            <td>
                                <strong><a href="{% url "control:event.order" event=request.event.slug organizer=request.event.organizer.slug code=e.order.code %}">{{ e.order.code }}</a></strong>
                                {% if e.order.status == "n" %}
                                    <span class="label label-warning">{% trans "unpaid" %}</span>
                                {% endif %}
                                {% if e.order.testmode %}
                                    <span class="label label-warning">{% trans "TEST MODE" %}</span>
                                {% endif %}
                            </td>
                            <td>{{ e.item }}{% if e.variation %} – {{ e.variation }}{% endif %}</td>
                            {% if request.event.has_subevents and not checkinlist.subevent %}
                                <td>
                                    {{ e.subevent.name }} – {{ e.subevent.get_date_range_display }} {{ e.subevent.date_from|date:"TIME_FORMAT" }}
                                </td>
                            {% endif %}
                            {% if seats %}
                                <td>{{ e.seat|default_if_none:"" }}</td>
                            {% endif %}
                            <td>
                                {% if e.addon_to and e.addon_to.attendee_email %}
                                    {{ e.addon_to.attendee_email }}
                                {% elif e.attendee_email %}
                                    {{ e.attendee_email }}
                                {% else %}
                                    {{ e.order.email|default_if_none:"" }}
                                {% endif %}
                            </td>
                            <td>
                                {% if e.addon_to and not e.attendee_name %}
                                    {{ e.addon_to.attendee_name }}
                                {% elif e.attendee_name %}
                                    {{ e.attendee_name }}
                                {% endif %}
                            </td>
                            <td>
                                {{ e.secret|slice:":10" }}…
                            </td>
                            <td>
                                {% if not e.last_entry %}
                                    <span class="label label-danger">{% trans "Not checked in" %}</span>
                                {% else %}
                                    {% if e.last_exit and e.last_exit_aware > e.last_entry_aware %}
                                        <span class="label label-success">{% trans "Checked in but left" %}</span>
                                    {% else %}
                                        <span class="label label-success">{% trans "Checked in" %}</span>
                                        {% if e.auto_checked_in %}
                                            <span class="fa fa-magic text-muted"
                                                  data-toggle="tooltip" title="{% trans "Checked in automatically" %}"></span>
                                        {% endif %}
                                    {% endif %}
                                {% endif %}
                            </td>
                            <td>
                                {% if e.last_entry %}
                                    {{ e.last_entry_aware|date:"SHORT_DATETIME_FORMAT" }}
                                {% endif %}
                                {% if e.last_exit %}
                                    <small><br>
                                        {% blocktrans trimmed with date=e.last_exit_aware|date:"SHORT_DATETIME_FORMAT" %}
                                            Exit: {{ date }}
                                        {% endblocktrans %}
                                    </small>
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <div class="batch-select-actions">
                {% if "can_change_orders" in request.eventpermset or "can_checkin_orders" in request.eventpermset %}
                    <button type="submit" class="btn btn-primary btn-save">
                        <span class="fa fa-sign-in" aria-hidden="true"></span>
                        {% trans "Check-In selected attendees" %}
                    </button>
                    <button type="submit" class="btn btn-default btn-save" name="checkout" value="true">
                        <span class="fa fa-sign-out" aria-hidden="true"></span>
                        {% trans "Check-Out selected attendees" %}
                    </button>
                {% endif %}
                {% if "can_change_orders" in request.eventpermset %}
                    <button type="submit" class="btn btn-danger btn-save" name="revert"
                            formaction="{% url "control:event.orders.checkinlists.bulk_revert" event=request.event.slug organizer=request.event.organizer.slug list=checkinlist.pk %}"
                            data-no-asynctask
                            value="true">
                        <span class="fa fa-trash" aria-hidden="true"></span>
                        {% trans "Delete all check-ins of selected attendees" %}
                    </button>
                {% endif %}
            </div>
        </form>
        {% include "pretixcontrol/pagination.html" %}
    {% endif %}
{% endblock %}
