{#
/*
 * Spring Signage Ltd - http://www.springsignage.com
 * Copyright (C) 2017 Spring Signage Ltd
 * (stats-proofofplay-page.twig)
 */

#}
{% extends "authed.twig" %}
{% import "inline.twig" as inline %}

{% block actionMenu %}
    <ul class="nav nav-pills pull-right">
        <li class="btn btn-primary btn-xs"><a class="XiboFormButton btns" title="{% trans "Export raw data to CSV" %}" href="{{ urlFor("stats.export.form") }}"> <i class="fa fa-cloud-download" aria-hidden="true"></i> {% trans "Export" %}</a></li>
        <li class="btn btn-warning btn-xs"><a class="btns" id="refreshGrid" title="{% trans "Refresh" %}" href="#"><i class="fa fa-refresh" aria-hidden="true"></i></a></li>
    </ul>
{% endblock %}


{% block pageContent %}

    <div class="widget">
        <div class="widget-title">{% trans "Proof of Play" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}" data-grid-name="statsView">
                <div class="XiboFilter well">
                    <div class="FilterDiv" id="Filter">
                        <form class="form-inline">
                            {% set title %}{% trans "From Date" %}{% endset %}
                            {{ inline.date("statsFromDt", title, defaults.fromDateOneDay, "", "", "", "") }}

                            {% set title %}{% trans "To Date" %}{% endset %}
                            {{ inline.date("statsToDt", 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: urlFor("display.search") },
                                { name: "data-search-term", value: "display" },
                                { name: "data-id-property", value: "displayId" },
                                { name: "data-text-property", value: "display" }
                            ] %}
                            {{ inline.dropdown("displayId", "single", title, "", null, "displayId", "display", "", "pagedSelect", "", "d", "", attributes) }}

                            {% 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: urlFor("layout.search") },
                                { name: "data-id-property", value: "layoutId" },
                                { name: "data-text-property", value: "layout" }
                            ] %}

                            {% set title %}{% trans "Layout" %}{% endset %}
                            {{ inline.dropdown("layoutId[]", "dropdownmulti", title, "", null, "layoutId", "layout", "", "pagedSelect", "", "l", "", attributes) }}

                            {% 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: urlFor("library.search") },
                                { name: "data-search-term", value: "media" },
                                { name: "data-id-property", value: "mediaId" },
                                { name: "data-text-property", value: "name" }
                            ] %}
                            {% set title %}{% trans "Media" %}{% endset %}
                            {{ inline.dropdown("mediaId[]", "dropdownmulti", title, "", null, "mediaId", "name", "", "pagedSelect", "", "m", "", attributes) }}

                            {% set title %}{% trans "Type" %}{% endset %}
                            {% set layout %}{% trans "Layout" %}{% endset %}
                            {% set media %}{% trans "Media" %}{% endset %}
                            {% set widget %}{% trans "Widget" %}{% endset %}
                            {% set options = [
                                { typeid: "", type: null },
                                { typeid: "layout", type: layout },
                                { typeid: "media", type: media },
                                { typeid: "widget", type: widget }
                            ] %}
                            {{ inline.dropdown("type", "single", title, "", options, "typeid", "type") }}
                        </form>
                    </div>
                </div>
                <div class="XiboData">
                    <table id="stats" class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Type" %}</th>
                            <th>{% trans "Display" %}</th>
                            <th>{% trans "Layout ID" %}</th>
                            <th>{% trans "Layout" %}</th>
                            <th>{% trans "Widget ID" %}</th>
                            <th>{% trans "Media" %}</th>
                            <th>{% trans "Number of Plays" %}</th>
                            <th>{% trans "Total Duration" %}</th>
                            <th>{% trans "Total Duration (s)" %}</th>
                            <th>{% trans "First Shown" %}</th>
                            <th>{% trans "Last Shown" %}</th>
                        </tr>
                        </thead>
                        <tbody>

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

{% block javaScript %}
    <script type="text/javascript">
        $(document).ready(function() {
            var table = $("#stats").DataTable({
                "language": dataTablesLanguage,
                serverSide: true,
                stateSave: true, stateDuration: 0,
                stateLoadCallback: function (settings, callback) {
                    var data;
                    $.ajax({
                        type: "GET",
                        async: false,
                        url: "{{ urlFor("user.pref") }}?preference=proofOfPlayGrid",
                        dataType: 'json',
                        success: function (json) {
                            try {
                                if (json.success) {
                                    data = JSON.parse(json.data.value);
                                }
                            } catch (e) {
                                // Do nothing
                            }
                        }
                    });
                    return data;
                },
                stateSaveCallback: function (settings, data) {
                    $.ajax({
                        type: "POST",
                        url: "{{ urlFor("user.pref") }}",
                        data: {
                            preference: [{
                                option: "proofOfPlayGrid",
                                value: JSON.stringify(data)
                            }]
                        }
                    });
                },
                filter: false,
                searchDelay: 3000,
                "order": [[1, "asc"]],
                ajax: {
                    "url": "{{ urlFor("stats.search") }}",
                    "data": function (d) {
                        $.extend(d, $("#stats").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                "columns": [
                    {"data": "type"},
                    {"data": "display"},
                    {"data": "layoutId"},
                    {"data": "layout"},
                    {"data": "widgetId"},
                    {"data": "media"},
                    {"data": "numberPlays"},
                    {
                        "data": "duration",
                        "render": function (data, type, row, meta) {
                            if (type != "display")
                                return "";

                            return moment().startOf("day").seconds(data).format("H:mm:ss")
                        }
                    },
                    {"data": "duration"},
                    {"data": "minStart"},
                    {"data": "maxEnd"}
                ]
            });

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

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

        function statsExportFormSubmit() {
            $("#statisticsExportForm").submit();
            XiboDialogClose();
        }
    </script>
{% endblock %}