{#
/**
 * 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 %}{% trans "Report: Time Connected Summary" %} | {% endblock %}

{% block actionMenu %}
    {% include "report-schedule-buttons.twig" %}
{% endblock %}

{% block pageContent %}

    <div class="widget">
        <div class="widget-title">
            <span>{% trans "Time Connected" %}</span>
        </div>

        {% include "report-selector.twig" %}

        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}">
                <div class="XiboFilter">
                    <div class="FilterDiv card-body" id="availabilityFilter">
                        <form class="form-inline">
                            {% set title %}{% trans "From Date" %}{% endset %}
                            {{ inline.date("availabilityFromDt", title, defaults.fromDate, "", "", "", "") }}

                            {% set title %}{% trans "To Date" %}{% endset %}
                            {{ inline.date("availabilityToDt", 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) }}

                            {% 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-search-term-tags", value: "tags" },
                                { name: "data-id-property", value: "displayGroupId" },
                                { name: "data-text-property", value: "displayGroup" }
                            ] %}
                            {{ inline.dropdown("displayGroupId", "single", title, "", null, "displayGroupId", "displayGroup", "", "pagedSelect", "", "d", "", attributes) }}

                            {% if currentUser.featureEnabled("tag.tagging") %}
                                {% set title %}{% trans "Tags" %}{% endset %}
                                {% set exactTagTitle %}{% trans "Exact match?" %}{% endset %}
                                {% set logicalOperatorTitle %}{% trans "When filtering by multiple Tags, which logical operator should be used?" %}{% endset %}
                                {% set helpText %}{% trans "A comma separated list of tags to filter by. Enter --no-tag to see items without tags." %}{% endset %}
                                {{ inline.inputWithTags("tags", title, null, helpText, null, null, null, "exactTags", exactTagTitle, logicalOperatorTitle) }}
                            {% endif %}

                            {% set title %}{% trans "Only show currently logged in?" %}{% endset %}
                            {{ inline.checkbox("onlyLoggedIn", title) }}
                        </form>
                    </div>
                </div>
                <br/>
                <div class="XiboData card pt-3">
                    <table id="stats" class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Display ID" %}</th>
                            <th>{% trans "Display" %}</th>
                            <th>{% trans "Time Disconnected" %}</th>
                            <th>{% trans "Time Connected" %}</th>
                            <th>{% trans "Units" %}</th>

                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="widget">
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}">
                <div class="XiboData" style="margin-top: 25px;">
                    <canvas id="availabilityChart" style="clear:both; margin-top:25px;" height="330"></canvas>
                </div>
            </div>
        </div>
    </div>

{% endblock %}

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

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

        var availabilityChart = null;

        $(document).ready(function() {
            var table = $("#stats").DataTable({
                "language": dataTablesLanguage,
                dom: dataTablesTemplate,
                serverSide: true,
                stateSave: true, stateDuration: 0,
                filter: false,
                searchDelay: 3000,
                "order": [[0, "asc"]],
                ajax: {
                    "url": "{{ url_for("report.data", {name: reportName}) }}",
                    "data": function (d) {
                        $.extend(d, $("#stats").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                "columns": [
                    {"data": "displayId"},
                    {"data": "display"},
                    {"data": "timeDisconnected", "sortable": false},
                    {"data": "timeConnected", "sortable": false},
                    {"data": "postUnits", "sortable": false}
                ]
            });

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

                if (!processing) {
                    // Render a Time Disconnected graph
                    if (availabilityChart !== undefined && availabilityChart !== null) {
                        availabilityChart.destroy();
                    }

                    // Organise our rows into datasets for the chart
                    var availabilityData = [];
                    var availabilityDataConnected = [];
                    var availabilityLabels = [];
                    var postUnits = "";
                    var dataSets = [];

                    $.each(table.data(), function(index, el) {
                        availabilityData.push(el.timeDisconnected);
                        availabilityDataConnected.push(el.timeConnected);
                        availabilityLabels.push(el.display);
                        postUnits = el.postUnits;
                    });
                    dataSets.push({
                        label: "{% trans "Downtime" %}",
                        backgroundColor: "rgb(11, 98, 164)",
                        data: availabilityData
                    });
                    if (availabilityDataConnected != null) {
                        dataSets.push({
                            label: "{% trans "Uptime" %}",
                            backgroundColor: "rgb(0, 255, 0)",
                            data: availabilityDataConnected
                        });
                    }

                    // Create our chart
                    availabilityChart = new Chart($("#availabilityChart"), {
                        type: "bar",
                        data: {
                            labels: availabilityLabels,
                            datasets: dataSets
                        },
                        options: {
                            scales: {
                                xAxes: [{
                                    stacked: true
                                }],
                                yAxes: [{
                                    scaleLabel: {
                                        display: true,
                                        labelString: postUnits,
                                    },
                                    stacked: true
                                }]
                            },
                            legend: {
                                display: false
                            },
                            maintainAspectRatio: false,
                        }
                    });
                }
            });

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

    </script>
{% endblock %}