{#
/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 * Copyright (C) 2022 Xibo Signage Ltd
 *
 * 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 actionMenu %}
    <div class="widget-action-menu pull-right">
        <button class="btn btn-success XiboRedirectButton" href="{{ url_for("report.view") }}"><i class="fa fa-th-list" aria-hidden="true"></i> {% trans "Back to dashboard" %}</button>
        <button class="btn btn-primary XiboRedirectButton" href="{{ url_for("reportschedule.view") }}"><i class="fa fa-th-list" aria-hidden="true"></i> {% trans "Report Schedules" %}</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 "Saved reports" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}" data-grid-name="savedreportView">
                <div class="XiboFilter card mb-3 bg-light">
                    <div class="FilterDiv card-body" id="Filter">
                        <form class="form-inline">

                            {% set title %}{% trans "Name" %}{% endset %}
                            {{ inline.inputNameGrid('saveAs', title) }}

                            {% set title %}{% trans "Owner" %}{% endset %}
                            {% set helpText %}{% trans "Show items owned by the selected 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) }}

                            {% set title %}{% trans "Type" %}{% endset %}
                            {% set helpText %}{% trans "Show items belong to a report." %}{% endset %}
                            {% set attributes = [
                                { name: "data-allow-clear", value: "true" },
                                { name: "data-placeholder--id", value: null },
                                { name: "data-placeholder--value", value: "" },
                            ] %}
                            {{ inline.dropdown("reportName", "single", title, null, [{name:null, description:""}]|merge(availableReports), "name", "description", helpText, "selectPicker", "", "", "", attributes) }}

                            {% set title %}{% trans "Only my reports?" %}{% endset %}
                            {{ inline.checkbox("onlyMyReport", title, 1) }}
                        </form>
                    </div>
                </div>
                <div class="XiboData card pt-3">
                    <table id="savedreports" class="table table-striped" data-state-preference-name="savedreportGrid">
                        <thead>
                        <tr>
                            <th>{% trans "Report Schedule" %}</th>
                            <th>{% trans "Saved as" %}</th>
                            <th>{% trans "Report Type" %}</th>
                            <th>{% trans "Generated on" %}</th>
                            <th>{% trans "Owner" %}</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>

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

{% block javaScript %}
    <script type="text/javascript" nonce="{{ cspNonce }}">
        var table;
        $(document).ready(function() {

            var url_string = window.location.href;
            var url = new URL(url_string);
            var reportName = url.searchParams.get("reportName");

            if (reportName !== undefined && reportName != null) {
                $('#reportName').val(reportName).trigger('change');
            }

            table = $("#savedreports").DataTable({
                "language": dataTablesLanguage,
                dom: dataTablesTemplate,
                serverSide: true,
                stateSave: true,
                stateDuration: 0,
                stateLoadCallback: dataTableStateLoadCallback,
                stateSaveCallback: dataTableStateSaveCallback,
                filter: false,
                searchDelay: 3000,
                "order": [[ 1, "asc"]],
                ajax: {
                    "url": "{{ url_for("savedreport.search") }}",
                    "data": function(d) {
                        $.extend(d, $("#savedreports").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                "columns": [
                    { "data": "reportScheduleName"},
                    { "data": "saveAs"},
                    { "data": "reportName", "visible": false},
                    {
                        "data": "generatedOn",
                        "render": dataTableDateFromUnix
                    },
                    { "data": "owner"},
                    {
                        "orderable": false,
                        "data": dataTableButtonsColumn
                    }
                ]
            });
            table.on('draw', dataTableDraw);
            table.on('processing.dt', dataTableProcessing);
            dataTableAddButtons(table, $('#savedreports_wrapper').find('.dataTables_buttons'));

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