{#
/**
 * Copyright (C) 2020 Xibo Signage Ltd
 *
 * Xibo - Digital Signage - http://www.xibo.org.uk
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
#}
{% extends "authed.twig" %}
{% import "inline.twig" as inline %}

{% block title %}{{ "Sessions"|trans }} | {% endblock %}

{% block actionMenu %}
    <div class="widget-action-menu pull-right">
        <button class="btn btn-primary" id="refreshGrid" title="{% trans "Refresh the Table" %}" href="#"><i class="fa fa-refresh" aria-hidden="true"></i></button>
    </div>
{% endblock %}

{% block pageContent %}
    <div class="widget">
        <div class="widget-title">{% trans "Sessions" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}" data-grid-name="sessionsView">
                <div class="XiboFilter card mb-3 bg-light">
                    <div class="FilterDiv card-body" id="Filter">
                        <form class="form-inline">
                            {% set title %}{% trans "From Date" %}{% endset %}
                            {{ inline.date("fromDt", title) }}

                            {% set title %}{% trans "Type" %}{% endset %}
                            {% set activeTrans %}{% trans "Active" %}{% endset %}
                            {% set guestTrans %}{% trans "Guest" %}{% endset %}
                            {% set expiredTrans %}{% trans "Expired" %}{% endset %}
                            {% set typeOptions = [
                            { id: null, value: "" },
                            { id: "active", value: activeTrans },
                            { id: "guest", value: guestTrans},
                            { id: "expired", value: expiredTrans}
                            ] %}
                            {{ inline.dropdown("type", "single", title, "", typeOptions, "id", "value") }}

                        </form>
                    </div>
                </div>
                <div class="XiboData card pt-3">
                    <table id="sessions" class="table table-striped">
                        <thead>
                            <tr>
                                <th>{% trans "Last Accessed" %}</th>
                                <th>{% trans "Active" %}</th>
                                <th>{% trans "User Name" %}</th>
                                <th>{% trans "IP Address" %}</th>
                                <th>{% trans "Browser" %}</th>
                                <th>{% trans "Expires At" %}</th>
                                <th class="rowMenu"></th>
                            </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block javaScript %}
    <script type="text/javascript" nonce="{{ cspNonce }}">
        $(document).ready(function() {
            var table = $("#sessions").DataTable({
                "language": dataTablesLanguage,
                dom: dataTablesTemplate,
                serverSide: true,
                stateSave: true,
                responsive: true,
                stateDuration: 0,
                stateLoadCallback: dataTableStateLoadCallback,
                stateSaveCallback: dataTableStateSaveCallback,
                filter: false,
                searchDelay: 3000,
                "order": [[1, "asc"]],
                ajax: {
                    "url": "{{ url_for("sessions.search") }}",
                    "data": function (d) {
                        $.extend(d, $("#sessions").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                "columns": [
                    {"data": "lastAccessed", "render": dataTableDateFromIso, responsivePriority: 2},
                    {"data": "isExpired", "render": dataTableTickCrossInverseColumn, responsivePriority: 2},
                    {"data": "userName", responsivePriority: 3},
                    {"data": "remoteAddress", responsivePriority: 4},
                    {"data": "userAgent", responsivePriority: 4},
                    {"data": "expiresAt", "render": dataTableDateFromUnix, responsivePriority: 3},
                    {
                        "orderable": false,
                         responsivePriority: 1,
                        "data": dataTableButtonsColumn
                    }
                ]
            });

            table.on('draw', dataTableDraw);
            table.on('processing.dt', dataTableProcessing);
            dataTableAddButtons(table, $('#sessions_wrapper').find('.dataTables_buttons'));

            $("#refreshGrid").click(function () {
                table.ajax.reload();
            });
        });
    </script>
{% endblock %}