{#
/**
 * 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 %}{{ "Logs"|trans }} | {% endblock %}

{% block actionMenu %}
    <div class="widget-action-menu pull-right">
        <button class="btn btn-warning XiboFormButton" title="{% trans "Truncate the Log" %}" href="{{ url_for("log.truncate.form") }}" data-auto-submit="true" data-commit-url="{{ url_for("log.truncate") }}" data-commit-method="DELETE"><i class="fa fa-scissors" aria-hidden="true"></i> {% trans "Truncate" %}</button>
        <button class="btn btn-success" id="refreshLog" title="{% trans "Refresh the Log" %}" href="#"><i class="fa fa-refresh" aria-hidden="true"></i> {% trans "Refresh" %}</button>
    </div>
{% endblock %}


{% block pageContent %}
    <div class="widget">
        <div class="widget-title">{% trans "Logs" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}" data-grid-name="logView">
                <div class="XiboFilter card mb-3 bg-light">
                    <div class="FilterDiv card-body" id="Filter">
                        <ul class="nav nav-tabs" role="tablist">
                            <li class="nav-item"><a class="nav-link active" href="#general" role="tab" data-toggle="tab">{% trans "General" %}</a></li>
                            <li class="nav-item"><a class="nav-link" href="#advanced" role="tab" data-toggle="tab">{% trans "Advanced" %}</a></li>
                        </ul>
                        <form class="form-inline">
                            <div class="tab-content">
                                <div class="tab-pane active" id="general">
                                    {% set title %}{% trans "Level" %}{% endset %}
                                    {{ inline.input("level", title) }}

                                    {% set title %}{% trans "Interval" %}{% endset %}
                                    {% set secondsTrans %}{% trans "Seconds" %}{% endset %}
                                    {% set minutesTrans %}{% trans "Minutes" %}{% endset %}
                                    {% set hoursTrans %}{% trans "Hours" %}{% endset %}
                                    {% set intervalTypeOptions = [
                                    { id: 1, value: secondsTrans },
                                    { id: 60, value: minutesTrans },
                                    { id: 3600, value: hoursTrans },
                                    ] %}
                                    {{ inline.dropdown("intervalType", "single", title, 1, intervalTypeOptions, "id", "value") }}

                                    {% set title %}{% trans "Duration back" %}{% endset %}
                                    {{ inline.number("seconds", title, 120) }}

                                    {% set title %}{% trans "Run" %}{% endset %}
                                    {{ inline.input("runNo", title) }}

                                    {% set title %}{% trans "User" %}{% endset %}
                                    {% set attributes = [
                                        { name: "data-width", value: "200px" },
                                        { name: "data-allow-clear", value: "true" },
                                        { name: "data-placeholder--id", value: null },
                                        { name: "data-placeholder--value", value: "" },
                                        { name: "data-search-url", value: url_for("user.search") },
                                        { name: "data-search-term", value: "userName" },
                                        { name: "data-search-term-tags", value: "tags" },
                                        { name: "data-id-property", value: "userId" },
                                        { name: "data-text-property", value: "userName" },
                                        { name: "data-initial-key", value: "userId" },
                                    ] %}
                                    {{ inline.dropdown("userId", "single", title, "", null, "userId", "userName", helpText, "pagedSelect", "", "", "", attributes) }}
                                </div>
                                <div class="tab-pane" id="advanced">
                                    {% set title %}{% trans "From Date" %}{% endset %}
                                    {{ inline.date("fromDt", title) }}

                                    {% set title %}{% trans "Channel" %}{% endset %}
                                    {{ inline.input("channel", title) }}

                                    {% set title %}{% trans "Page" %}{% endset %}
                                    {{ inline.input("page", title) }}

                                    {% set title %}{% trans "Function" %}{% endset %}
                                    {% set getTrans %}{% trans "GET" %}{% endset %}
                                    {% set postTrans %}{% trans "POST" %}{% endset %}
                                    {% set putTrans %}{% trans "PUT" %}{% endset %}
                                    {% set deleteTrans %}{% trans "DELETE" %}{% endset %}
                                    {% set headTrans %}{% trans "HEAD" %}{% endset %}
                                    {% set patchTrans %}{% trans "PATCH" %}{% endset %}
                                    {% set functionOptions = [
                                    { id: "", value: "" },
                                    { id: "GET", value: getTrans },
                                    { id: "POST", value: postTrans},
                                    { id: "PUT", value: putTrans},
                                    { id: "DELETE", value: deleteTrans},
                                    { id: "HEAD", value: headTrans},
                                    { id: "PATCH", value: patchTrans},
                                    ] %}
                                    {{ inline.dropdown("function", "single", title, "", functionOptions, "id", "value") }}

                                    {% set title %}{% trans "Display" %}{% endset %}
                                    {% set attributes = [
                                        { name: "data-width", value: "200px" },
                                        { name: "data-allow-clear", value: "true" },
                                        { name: "data-placeholder--id", value: null },
                                        { name: "data-placeholder--value", value: "" },
                                        { name: "data-search-url", value: url_for("display.search") },
                                        { name: "data-search-term", value: "display" },
                                        { name: "data-search-term-tags", value: "tags" },
                                        { name: "data-id-property", value: "displayId" },
                                        { name: "data-text-property", value: "display" }
                                    ] %}
                                    {{ inline.dropdown("displayId", "single", title, "", null, "displayId", "display", "", "pagedSelect", "", "d", "", attributes) }}

                                    <div class="form-group mr-1 mb-1">
                                        <label class="control-label mr-1" title="" for="display" accesskey="">{% trans "Display Name" %}</label>
                                        <div>
                                            <div class="input-group">
                                                <input class="form-control" name="display" type="text" id="display" value="">
                                                <div class="input-group-append input-group-addon">
                                                    <div class="input-group-text">
                                                        <input title="{% trans "Use Regex?" %}" type="checkbox" id="useRegexForName" name="useRegexForName">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    {% set title %}{% trans "Display Group" %}{% endset %}
                                    {% set attributes = [
                                        { name: "data-width", value: "200px" },
                                        { name: "data-allow-clear", value: "true" },
                                        { name: "data-placeholder--id", value: null },
                                        { name: "data-placeholder--value", value: "" },
                                        { name: "data-search-url", value: url_for("displayGroup.search") },
                                        { name: "data-search-term", value: "displayGroup" },
                                        { name: "data-id-property", value: "displayGroupId" },
                                        { name: "data-text-property", value: "displayGroup" }
                                    ] %}
                                    {{ inline.dropdown("displayGroupId", "single", title, "", null, "displayGroupId", "displayGroup", "", "pagedSelect", "", "d", "", attributes) }}

                                    {% set title %}{% trans "Message" %}{% endset %}
                                    {{ inline.input("message", title) }}

                                    {% set title %}{% trans "Exclude logs common to each request?" %}{% endset %}
                                    {{ inline.checkbox("excludeLog", title, 1) }}
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="XiboData card pt-3">
                    <table id="logs" class="table table-striped" data-state-preference-name="logGrid">
                        <thead>
                            <tr>
                                <th>{% trans "ID" %}</th>
                                <th>{% trans "Run" %}</th>
                                <th>{% trans "Date" %}</th>
                                <th>{% trans "Channel" %}</th>
                                <th>{% trans "Function" %}</th>
                                <th>{% trans "Level" %}</th>
                                <th>{% trans "Display" %}</th>
                                <th>{% trans "Page" %}</th>
                                <th style="width: 50%">{% trans "Message" %}</th>
                            </tr>
                        </thead>
                        <tbody>

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

{% block javaScript %}
    <script type="text/javascript">
        $(document).ready(function() {
            var table = $("#logs").DataTable({
                "language": dataTablesLanguage,
                dom: dataTablesTemplate,
                serverSide: true,
                stateSave: true,
                stateDuration: 0,
                stateLoadCallback: dataTableStateLoadCallback,
                stateSaveCallback: dataTableStateSaveCallback,
                filter: false,
                searchDelay: 3000,
                "order": [[0, "desc"]],
                ajax: {
                    url: "{{ url_for("log.search") }}",
                    "data": function (d) {
                        $.extend(d, $("#logs").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                "columns": [
                    {"data": "logId"},
                    {"data": "runNo"},
                    {"data": "logDate", "render": dataTableDateFromIso},
                    {"data": "channel"},
                    {"data": "function"},
                    {"data": "type"},
                    {"data": "display"},
                    {"data": "page"},
                    {"data": "message"}
                ]
            });

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

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