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

{% block actionMenu %}
    <div class="widget-action-menu pull-right">
        <button class="btn btn-success XiboFormButton" title="{% trans "Export raw data to CSV" %}" href="{{ url_for("auditLog.export.form") }}"><i class="fa fa-cloud-upload" aria-hidden="true"></i> {% trans "Export" %}</button>
        <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 "Audit Log" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}" data-grid-name="auditView">
                <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 "To Date" %}{% endset %}
                            {{ inline.date("toDt", title) }}

                            {% set title %}{% trans "User" %}{% endset %}
                            {{ inline.input("user", title) }}

                            {% set title %}{% trans "Entity" %}{% endset %}
                            {{ inline.input("entity", title) }}

                            {% set title %}{% trans "Entity ID" %}{% endset %}
                            {{ inline.input("entityId", title) }}

                            {% set title %}{% trans "IP Address" %}{% endset %}
                            {{ inline.input("ipAddress", title) }}

                            {% set title %}{% trans "Message" %}{% endset %}
                            {{ inline.input("message", title) }}
                        </form>
                    </div>
                </div>
                <div class="XiboData card pt-3">
                    <table id="logs" class="table table-striped">
                        <thead>
                            <tr>
                                <th>{% trans "ID" %}</th>
                                <th>{% trans "Date" %}</th>
                                <th>{% trans "User" %}</th>
                                <th>{% trans "Entity" %}</th>
                                <th>{% trans "Entity ID" %}</th>
                                <th>{% trans "IP Address" %}</th>
                                <th>{% trans "Message" %}</th>
                                <th>{% trans "Object" %}</th>
                            </tr>
                        </thead>
                        <tbody>

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

{% block javaScript %}
    {% verbatim %}
    <script type="text/x-handlebars-template" id="table-array-viewer">
        <a class="arrayViewerToggle" href="#"><span class="fa fa-search"></span></a>
        <table class="arrayViewer table table-bordered" data-state-preference-name="auditlogGrid">
            <thead>
                <tr>
                    <th>{{ col1 }}</th>
                    <th>{{ col2 }}</th>
                </tr>
            </thead>
            <tbody>
                {{#each items}}
                <tr>
                    <td>{{ @key }}</td>
                    <td>{{ this }}</td>
                </tr>
                {{/each}}
            </tbody>
        </table>
    </script>
    {% endverbatim %}
    <script type="text/javascript" nonce="{{ cspNonce }}">

        $(document).ready(function() {
            var arrayViewer = Handlebars.compile($("#table-array-viewer").html());

            var table = $("#logs").DataTable({
                "language": dataTablesLanguage,
                dom: dataTablesTemplate,
                serverSide: true,
                stateSave: true,
                responsive: true,
                stateDuration: 0,
                stateLoadCallback: dataTableStateLoadCallback,
                stateSaveCallback: dataTableStateSaveCallback,
                filter: false,
                searchDelay: 3000,
                "order": [[0, "desc"]],
                ajax: {
                    url: "{{ url_for("auditLog.search") }}",
                    "data": function (d) {
                        $.extend(d, $("#logs").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                "columns": [
                    {"data": "logId", responsivePriority: 2},
                    {"data": "logDate", "render": dataTableDateFromUnix, responsivePriority: 2},
                    {"data": "userName", responsivePriority: 2},
                    {"data": "entity", responsivePriority: 2},
                    {
                        "name": "entityId",
                        responsivePriority: 2,
                        "data" : function (data) {
                            if (data.entityId === 0) {
                                return ''
                            }

                            return data.entityId;
                        }
                    },
                    {"data": "ipAddress", responsivePriority: 2},
                    {"data": "message", responsivePriority: 1},
                    {
                        "data": function (data, type, row, meta) {
                            if (type != "display")
                                return "";

                            return arrayViewer({"col1": "{% trans "Property" %}", "col2": "{% trans "Value" %}", "items": data.objectAfter});
                        },
                        "sortable": false,
                         responsivePriority: 1
                    }
                ]
            });

            table.on('draw', function (e, settings) {
                dataTableDraw(e, settings);

                $(".arrayViewerToggle").click(function () {
                    $(this).parent().find(".arrayViewer").toggle();
                });
            });
            table.on('processing.dt', dataTableProcessing);
            dataTableAddButtons(table, $('#logs_wrapper').find('.dataTables_buttons'));

            $("#refreshGrid").click(function () {
                table.ajax.reload();
            });
        });

        function auditLogExportFormSubmit() {
            $("#auditLogExportForm").submit();
            XiboDialogClose();
        }

    </script>
{% endblock %}