{#
/**
 * Copyright (C) 2021 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 %}{{ "User Groups"|trans }} | {% endblock %}

{% block actionMenu %}
    <div class="widget-action-menu pull-right">
        {% if currentUser.isSuperAdmin() %}
            <button class="btn btn-success XiboFormButton" title="{% trans "Add a new User Group" %}" href="{{ url_for("group.add.form") }}"><i class="fa fa-users" aria-hidden="true"></i> {% trans "Add User Group" %}</button>
        {% endif %}
        <button class="btn btn-primary refresh-grid" title="{% trans "Refresh the Table" %}" href="#"><i class="fa fa-refresh" aria-hidden="true"></i> {% trans "Refresh" %}</button>
    </div>
{% endblock %}


{% block pageContent %}
    <div class="widget">
        <div class="widget-title">{% trans "User Groups" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}" data-grid-name="userGroupView">
                <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('userGroup', title) }}
                        </form>
                    </div>
                </div>
                <div class="XiboData card pt-3">
                    <table id="userGroups" class="table table-striped" data-state-preference-name="userGroupGrid">
                        <thead>
                            <tr>
                                <th>{% trans "User Group" %}</th>
                                <th>{% trans "Description" %}</th>
                                <th>{% trans "Library Quota" %}</th>
                                <th>{% trans "Receive System Notifications?" %}</th>
                                <th>{% trans "Receive Display Notifications?" %}</th>
                                <th>{% trans "Is shown for Add User?" %}</th>
                                <th class="rowMenu"></th>
                            </tr>
                        </thead>
                        <tbody>

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

{% block javaScript %}
    <script type="text/javascript">
        $(document).ready(function() {
            var table = $("#userGroups").DataTable({
                "language": dataTablesLanguage,
                dom: dataTablesTemplate,
                serverSide: true,
                stateSave: true,
                stateDuration: 0,
                responsive: true,
                stateLoadCallback: dataTableStateLoadCallback,
                stateSaveCallback: dataTableStateSaveCallback,
                searchDelay: 3000,
                filter: false,
                "order": [[0, "asc"]],
                ajax: {
                    url: "{{ url_for("group.search") }}",
                    "data": function (d) {
                        $.extend(d, $("#userGroups").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                "columns": [
                    {"data": "group", "render": dataTableSpacingPreformatted, responsivePriority: 2 },
                    {"data": "description", "visible": false },
                    {
                        "name": "libraryQuota",
                        "data": null,
                        "render": {"_": "libraryQuota", "display": "libraryQuotaFormatted", "sort": "libraryQuota"}
                    },
                    {
                        "data": "isSystemNotification",
                        "render": dataTableTickCrossColumn
                    },
                    {
                        "data": "isDisplayNotification",
                        "render": dataTableTickCrossColumn
                    },
                    {
                        "data": "isShownForAddUser",
                        "render": dataTableTickCrossColumn
                    },
                    {
                        "orderable": false,
                        responsivePriority: 1,
                        "data": dataTableButtonsColumn
                    }
                ]
            });

            table.on('draw', dataTableDraw);
            table.on('processing.dt', dataTableProcessing);
            dataTableAddButtons(table, $('#userGroups_wrapper').find('.dataTables_buttons'));

            $(".refresh-grid").click(function() {
                table.ajax.reload();
            });
        });

        function handleLibraryQuotaField(libraryQuotaField, libraryQuotaUnitsField) {
            var libraryQuota = libraryQuotaField.val();

            if (libraryQuotaUnitsField.val() === 'mb') {
                libraryQuota = libraryQuota * 1024;
            } else if (libraryQuotaUnitsField.val() === 'gb') {
                libraryQuota = libraryQuota * 1024 * 1024;
            }

            // Set the field
            libraryQuotaField.prop('value', libraryQuota);
        }

        function userGroupFormOpen() {
            // Bind to the add form submit
            $(".UserGroupForm").validate({
                submitHandler: function (form) {
                    // Grab and alter the value in the library quota field
                    handleLibraryQuotaField(
                        $(form).find("input[name=libraryQuota]"),
                        $(form).find("select[name=libraryQuotaUnits]")
                    );

                    XiboFormSubmit(form);
                },
                errorElement: "span",
                highlight: function(element) {
                    $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
                },
                success: function(element) {
                    $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
                },
                invalidHandler: function(event, validator) {
                    // Remove the spinner
                    $(this).closest(".modal-dialog").find(".saving").remove();
                    // https://github.com/xibosignage/xibo/issues/1589
                    $(this).closest(".modal-dialog").find(".save-button").removeClass("disabled");
                }
            });
        }
    </script>
{% endblock %}