{#
/*
 * 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-info XiboRedirectButton" href="{{ url_for("savedreport.view") }}"><i class="fa fa-eye" aria-hidden="true"></i> {% trans "Saved Reports" %}</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">
            <span>{% trans "Report Schedule" %}</span>
            <span class="fa fa-info-circle widget-title-info px-1" data-toggle="popover" data-trigger="hover" data-placement="bottom" data-content="{% trans "The report will be available 6AM every day." %}"></span>
        </div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}" data-grid-name="reportscheduleView">
                <div class="XiboFilter card mb-3 bg-light">
                    <div class="FilterDiv card-body" id="Filter">
                        <form class="form-inline">
                            {% set title %}{% trans "ID" %}{% endset %}
                            {{ inline.number("reportScheduleId", title ) }}

                            {% set title %}{% trans "Name" %}{% endset %}
                            {{ inline.inputNameGrid('name', 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 schedules?" %}{% endset %}
                            {{ inline.checkbox("onlyMySchedules", title, 1) }}

                            {% set title %}{% trans "Only my schedules?" %}{% endset %}
                            {{ inline.checkbox("onlyMySchedules", title, 1) }}

                            {% set title %}{% trans "Only my schedules?" %}{% endset %}
                            {{ inline.checkbox("onlyMySchedules", title, 1) }}
                        </form>
                    </div>
                </div>
                <div class="XiboData card pt-3">
                    <table id="reportschedules" class="table table-striped" data-state-preference-name="reportscheduleGrid">
                        <thead>
                        <tr>
                            <th>{% trans "Name" %}</th>
                            <th>{% trans "Report Name" %}</th>
                            <th>{% trans "Schedule" %}</th>
                            <th>{% trans "Owner" %}</th>
                            <th>{% trans "Last Run" %}</th>
                            <th>{% trans "Next Run" %}</th>
                            <th>{% trans "Previous Run" %}</th>
                            <th>{% trans "Active" %}</th>
                            <th>{% trans "Start time" %}</th>
                            <th>{% trans "End time" %}</th>
                            <th>{% trans "Failed Message" %}</th>
                            <th>{% trans "Created" %}</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>

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

{% block javaScript %}
    <script type="text/javascript" nonce="{{ cspNonce }}">

        $(function () {
            $('[data-toggle="popover"]').popover();
        });

        var table;
        $(document).ready(function() {

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

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

            if (reportName !== undefined && reportName != null) {
                // Grid will be reloaded again
                $('#reportName').val(reportName).trigger('change');
            }

            table = $("#reportschedules").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("reportschedule.search") }}",
                    "data": function(d) {
                        $.extend(d, $("#reportschedules").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                "columns": [
                    { "data": "name"},
                    { "data": "reportName"},
                    { "data": "schedule"},
                    { "data": "owner"},
                    {
                        "data": "lastRunDt",
                        "render": dataTableDateFromUnix
                    },
                    {
                        "orderable": false,
                        "data": "nextRunDt",
                        "render": dataTableDateFromUnix
                    },
                    {
                        "data": "previousRunDt",
                        "render": dataTableDateFromUnix,
                        "visible": false
                    },
                    {
                        "data": "isActive",
                        "render": function (data, type, row) {
                            if (type !== "display")
                                return data;

                            var icon = "";
                            if (data == 1)
                                icon = "fa-check";
                            else
                                icon = "fa-times";

                            return '<span class="fa ' + icon + '" title="' + (row.isActiveDescription) + '"></span>';
                        },
                        "visible": false
                    },
                    {
                        "data": "fromDt",
                        "render": dataTableDateFromUnix,
                        "visible": false
                    },
                    {
                        "data": "toDt",
                        "render": dataTableDateFromUnix,
                        "visible": false
                    },
                    {
                        "data": "message",
                        "render": function (data, type, row) {
                            if (type !== "display")
                                return data;

                            var icon = "";
                            if (data !== null)
                                icon = "fa-exclamation-triangle";
                            else
                                icon = "";

                            return '<span class="fa ' + icon + '" title="' + (row.message) + '"></span>';
                        },
                        "visible": false
                    },
                    {
                        "data": "createdDt",
                        "render": dataTableDateFromUnix
                    },
                    {
                        "orderable": false,
                        "data": dataTableButtonsColumn
                    }
                ]
            });

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

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

    </script>
{% endblock %}