{#
/**
 * 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 %}{{ "Commands"|trans }} | {% endblock %}

{% block actionMenu %}
    {% if currentUser.featureEnabled("command.add") %}
    <div class="widget-action-menu pull-right">
        <button class="btn btn-success XiboFormButton" href="{{ url_for("command.add.form") }}"><i class="fa fa-terminal" aria-hidden="true"></i> {% trans "Add Command" %}</button>
    </div>
    {% endif %}
{% endblock %}


{% block pageContent %}
    <div class="widget">
        <div class="widget-title">{% trans "Commands" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}">
                <div class="XiboFilter card mb-3 bg-light">
                    <div class="FilterDiv card-body" id="Filter">
                        <form class="form-inline">
                            {% set title %}{% trans "Command" %}{% endset %}
                            {{ inline.input("command", title) }}

                            {% set title %}{% trans "Code" %}{% endset %}
                            {{ inline.input("code", title) }}
                        </form>
                    </div>
                </div>
                <div class="XiboData card pt-3">
                    <table id="commands" class="table table-striped" data-state-preference-name="commandGrid">
                        <thead>
                            <tr>
                                <th>{% trans "Name" %}</th>
                                <th>{% trans "Code" %}</th>
                                <th>{% trans "Available On" %}</th>
                                <th>{% trans "Description" %}</th>
                                <th>{% trans "Sharing" %}</th>
                                <th class="rowMenu"></th>
                            </tr>
                        </thead>
                        <tbody>

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

{% block javaScript %}
    <script type="text/javascript">
        var table = $("#commands").DataTable({ "language": dataTablesLanguage,
            dom: dataTablesTemplate,
            serverSide: true,
            stateSave: true,
            stateDuration: 0,
            responsive: true,
            stateLoadCallback: dataTableStateLoadCallback,
            stateSaveCallback: dataTableStateSaveCallback,
            filter: false,
            searchDelay: 3000,
            "order": [[ 1, "asc"]],
            ajax: {
                "url": "{{ url_for("command.search") }}",
                "data": function(d) {
                    $.extend(d, $("#commands").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                }
            },
            "columns": [
                { "data": "command", "render": dataTableSpacingPreformatted , responsivePriority: 2},
                { "data": "code" , responsivePriority: 2},
                {
                    "data": "availableOn",
                     responsivePriority: 3,
                    "render": function(data, type) {

                        if (type !== "display")
                            return data;

                        var returnData = '';

                        if (typeof data !== undefined && data != null) {
                            var arrayOfTags = data.split(',');

                            returnData += '<div class="permissionsDiv">';

                            for (var i = 0; i < arrayOfTags.length; i++) {
                                var name = arrayOfTags[i];
                                if (name !== '') {
                                    returnData += '<li class="badge ' + ((name === 'lg') ? '' : 'capitalize') + '">'
                                        + name.replace("lg", "webOS").replace("sssp", "Tizen") + '</span></li>'
                                }
                            }

                            returnData += '</div>';
                        }

                        return returnData;
                    }
                },
                { "data": "description", responsivePriority: 3 },
                {
                    "data": "groupsWithPermissions",
                     responsivePriority: 3,
                    "render": dataTableCreatePermissions
                },
                {
                    "orderable": false,
                     responsivePriority: 1,
                    "data": dataTableButtonsColumn
                }
            ]
        });

        table.on('draw', dataTableDraw);
        table.on('processing.dt', dataTableProcessing);
        dataTableAddButtons(table, $('#commands_wrapper').find('.dataTables_buttons'));
    </script>
{% endblock %}