{#
/**
 * 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-info XiboRedirectButton" href="{{ url_for("savedreport.view") }}"><i class="fa fa-eye" aria-hidden="true"></i> {% trans "Saved Reports" %}</button>
    </div>
{% endblock %}

{% block pageContent %}

    <div class="widget">
        <div class="widget-title">
            <i class="fa fa-list"></i>
            {{ metadata.title }}
                <span class="small">({% trans "Generated on: " %}{{ metadata.generatedOn }})</span>
            <div><span class="small">{% trans "From" %} {{ metadata.periodStart }} {% trans "To" %} {{ metadata.periodEnd }}</span></div>
            <div class="clearfix"></div>
        </div>

        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}">

                <div class="XiboData card pt-3">
                    <table id="libraryUsage" class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "ID" %}</th>
                            <th>{% trans "User" %}</th>
                            <th>{% trans "Usage" %}</th>
                            <th>{% trans "Count Files" %}</th>
                        </tr>
                        </thead>
                        <tbody>

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

    <div class="row">
        <div class="col-md-6">
            <div class="widget">
                <div class="widget-title">
                    <i class="fa fa-tasks"></i>
                    {% if libraryLimitSet != "" %}
                        {% trans %}Library Usage. Limit {{ libraryLimit }}{% endtrans %}
                    {% else %}
                        {% trans "Library Usage" %}
                    {% endif %}
                    <div class="clearfix"></div>
                </div>
                <div class="widget-body medium no-padding">
                    <canvas id="libraryChart" style="clear:both;" width="350" height="220"></canvas>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="widget">
                <div class="widget-title">
                    <i class="fa fa-user"></i>
                    {% trans "User Percentage Usage" %}
                    <div class="clearfix"></div>
                </div>
                <div class="widget-body medium no-padding">
                    <canvas id="userChart" style="clear:both;" width="350" height="220"></canvas>
                </div>
            </div>
        </div>
    </div>

{% endblock %}

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

        var outputData = {{ table|json_encode|raw }};

        // Grid
        var table = $("#libraryUsage").DataTable({
            "language": dataTablesLanguage,
            "dom": dataTablesTemplate,
            "paging":   false,
            "ordering": false,
            "info":     false,
            "order": [[1, "asc"]],
            "searching": false,
            data: outputData,
            columns: [
                { data: "userId" },
                { data: "userName" },
                { data: "bytesUsedFormatted" },
                { data: "numFiles" }
            ]
        });

        table.on('draw', dataTableDraw);
        table.on('processing.dt', dataTableProcessing);

        // User Percentage Usage
        var userChart = new Chart($("#userChart"), {{ chart.User_Percentage_Usage|json_encode|raw }});

        // Library Usage
        var libraryChart = new Chart($("#libraryChart"), {{ chart.Library_Usage|json_encode|raw }});


    </script>
{% endblock %}