function initializeUsers() {
    $dataTable = $('#grid-data');
    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url'),
            data: function (d) {
                d.Keywords = $("#Keywords").val();
                d.Status = $("#Status").val();
                d.Granules = $("#Granules").val();
            }
        },
        columns: [
            {
                data: "Active", name: "AcActive",
                render: $.fn.dataTable.render.toggle(true, true),
                className: "dt-body-center"
            },
            {
                data: "Username", name: "AcUserId", filtertype: "text", render: $.fn.dataTable.render.link("Id")
            },
            { data: "Name", name: "AcName", filtertype: "text" },
            { data: "Subject", name: "AcSubject", filtertype: "text" },
            { data: "TaxNo", name: "AcCode", filtertype: "text" },
            { data: "Granules", name: "Granules" }
        ]

    }).on('xhr.dt', function (e, settings, json, xhr) {
        if (json) {
            $('.grid-data-total-items').html(json.recordsTotal);
        }
    });

    $("select").off("change");
    $("select:not(#GranulesFilter)").on("change", function () {
        $.fn.dataTable.reload($dataTable);
    });

    var typingTimer;
    var doneTypingInterval = 600;
    $('#Keywords').off('keyup');
    $('#Keywords').on('keyup', function () {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(function () { $.fn.dataTable.reload($dataTable); }, doneTypingInterval);
    });

    $("#GranulesFilter").on("change", function () {
        var granules = "";
        if ($("#GranulesFilter").val())
            granules = $("#GranulesFilter").val().join(",");

        $("#Granules").val(granules);
        $.fn.dataTable.reload($dataTable);
    });
}

async function updateGranules(element, url) {
    $(element).find("i").addClass("fa-spinner fa-spin");

    await fetch(url, {
        method: "POST",
        headers: { 'Content-Type': 'application/json' }
    }).then((response) => response.json()
    ).then(async (updateData) => {
        if (!updateData || updateData.success === false) throw "Error";

        $.fn.dataTable.reload($('#grid-data'));
        $(element).find("i").removeClass("fa-spinner fa-spin");
    }).catch(async (error) => {
        $().customModal({
            destroyOnClose: true,
            showCloseButton: true,
            type: "error",
            text: $("#GeneralErrorMessage").val(),
            buttons: [
                {
                    icon: 'fa fa-check',
                    text: $("#ConfirmLoc").val(),
                    closeOnAction: true,
                    type: 'primary'
                }
            ]
        });
        $(element).find("i").removeClass("fa-spinner fa-spin");
    });
}

//Edit
function initializeUsersEdit() {
    $('.input-validation-error').parents('.form-group').addClass('has-error');
    $('.field-validation-error').addClass('text-danger');

    elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch-small'));
    elems.forEach(function (html) {
        var switchery = new Switchery(html, { size: 'small' });
    });

    elems = Array.prototype.slice.call(document.querySelectorAll('.permission'));
    elems.forEach(function (html) {
        var switchery = new Switchery(html, { size: 'small' });

        if (($("#username").val() && $("#username").val().trim() !== "AD") && $("#isFirstUser").val() !== "True")
            switchery.disable();
    });

    elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch-parent input[type=checkbox]'));
    var switches = [];
    var counter = 0;
    elems.forEach(function (html) {
        var switchery = new Switchery(html, { size: 'small' });
        switches[counter] = {
            value: html.value,
            switchery: switchery,
            html: html
        };
        counter++;
    });

    $('.js-switch-parent input[type=checkbox]').on('click', function () {
        if (this.checked) {
            var clickedValue = this.value;
            switches.forEach(function (switchCheckboxData) {
                if (switchCheckboxData.value !== clickedValue && switchCheckboxData.html.checked) {
                    switchCheckboxData.switchery.setPosition(true);
                }
            });
        }
        else {
            return false;
        }
    });

    initializePartnerTypeahead($("#SearchPartnersUrl").val(), $('#Subject'), $("#Subject"));

    $("#saveButton").on('click', function () {
        if (!$("#Subject").val()) {
            $().customModal({
                destroyOnClose: true,
                showCloseButton: true,
                type: "error",
                text: $("#subjectIsRequiredLoc").val(),
                buttons: [
                    {
                        icon: 'fa fa-check',
                        text: $("#ConfirmLoc").val(),
                        closeOnAction: true,
                        type: 'primary'
                    }
                ]
            });
        } else
            $('#userForm').submit();
    });
}

function initializeUsersCreate() {
    initializeUsersEdit();
}

function initializeChangePassword() {
    $('.input-validation-error').parents('.form-group').addClass('has-error');
    $('.field-validation-error').addClass('text-danger');
}