{#
/**
 * 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 actionMenu %}
    <div class="widget-action-menu pull-right">
        <button class="btn btn-success XiboFormButton" title="Schedule" id="reportAddBtn" href="{{ url_for("reportschedule.add.form") }}?reportName=bandwidth"><i class="fa fa-plus-circle" aria-hidden="true"></i> {% trans "Schedule" %}</button>
        <button class="btn btn-info XiboRedirectButton" href="{{ url_for("savedreport.view") }}?reportName=bandwidth"><i class="fa fa-eye" aria-hidden="true"></i> {% trans "Saved Reports" %}</button>
        <button class="btn btn-primary XiboRedirectButton" href="{{ url_for("reportschedule.view") }}?reportName=bandwidth"><i class="fa fa-th-list" aria-hidden="true"></i> {% trans "Report Schedules" %}</button>
    </div>
{% endblock %}

{% block pageContent %}

    <div class="widget">
        <div class="widget-title">
            <span>{% trans "Bandwidth" %}</span>
        </div>
        <div class="widget-navigation-menu">
            <ul class="nav nav-pills">
                <li role="presentation" class="nav-item"><a class="nav-link" href="{{ url_for("report.view") }}">{% trans "All Reports" %}</a></li>
                <li role="presentation" class="nav-item dropdown">
                    <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> Reports <span class="caret"></span> </a>
                    <div class="dropdown-menu">
                        {% for reports in defaults.availableReports %}
                            {% for report in reports %}
                                {% if report.hidden == 0 and report.category == 'Display'%}
                                    <a class="dropdown-item" href="{{ url_for("report.form", {name: report.name}) }}">{{ report.description }}</a>
                                {% endif %}
                            {% endfor %}
                        {% endfor %}
                    </div>
                </li>
            </ul>
        </div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}">
                <div class="XiboFilterCustom card bg-light mb-3">
                    <div class="FilterDiv card-body" id="bandwidth">
                        <form class="form-inline">
                            {% set title %}{% trans "From Date" %}{% endset %}
                            {{ inline.dateMonth("bandwidthFromDt", title, defaults.toDate, "", "", "", "") }}

                            {% set title %}{% trans "To Date" %}{% endset %}
                            {{ inline.dateMonth("bandwidthToDt", title, defaults.toDate, "", "", "", "") }}

                            {% 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="w-100">
                                <a id="applyBtn" class="btn btn-success">
                                    <span>{% trans "Apply" %}</span>
                                </a>
                                <span id="applyWarning" class="text-warning" style="display:none; padding-left: 10px">{% trans "Warning: This may return a lot of data and may take several minutes to process." %}</span>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="XiboData card pt-3 d-block">
                    <canvas id="reportChart" style="clear:both; margin-top:25px;" height="70%"></canvas>
                    <img id="imageLoader" style="display: block; margin: auto;" src="{{ theme.uri("img/loader.gif") }}">
                </div>
            </div>
        </div>
    </div>

{% endblock %}

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

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

        var reportChart = null;
        var imageLoader = $("#imageLoader");

        function setReport() {

            imageLoader.show();
            $.ajax({
                type: "get",
                url: "{{ url_for("report.data", {name: reportName}) }}",
                cache: false,
                dataType: "json",
                data: $("#bandwidth").find("form").serialize(),
                success: function(response) {

                    setTimeout(function() {
                        $("#applyBtn").removeClass('disabled');
                    }, 300);

                    imageLoader.hide();
                    if (reportChart !== undefined && reportChart !== null) {
                        reportChart.destroy();
                    }

                    if (!response.extra.hasData) {
                        return;
                    }

                    // Create our chart
                    reportChart = new Chart($("#reportChart"), response.extra.chart);
                }
            });
        }

        $(document).ready(function() {

            // Init
            var applyBtn = $("#applyBtn");
            imageLoader.hide();

            // Apply
            applyBtn.click(function () {
                setReport();
            });
        });

    </script>
{% endblock %}