{#
/**
 * Copyright (C) 2020-2024 Xibo Signage Ltd
 *
 * Xibo - Digital Signage - https://xibosignage.com
 *
 * 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 %}
    <div class="widget-action-menu pull-right">
        {% if currentUser.featureEnabled("command.add") %}
            <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>
        {% endif %}
        <button class="btn btn-primary" id="refreshGrid" title="{% trans "Refresh the Table" %}" href="#"><i class="fa fa-refresh" aria-hidden="true"></i></button>
    </div>
{% 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 "Name" %}{% endset %}
                            {{ inline.inputNameGrid('command', title) }}

                            {% set title %}{% trans "Code" %}{% endset %}
                            {{ inline.inputNameGrid('code', title, null, 'useRegexForCode', 'logicalOperatorCode') }}
                        </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" nonce="{{ cspNonce }}">
        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'));

        $("#refreshGrid").click(function () {
            table.ajax.reload();
        });
    </script>
{% endblock %}