var $mailBoxesDataTable;

function initializeMailboxes() {
    $dataTable = $('#grid-data');
    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url')
        },
        columns: [
            { data: "Name", name: "AcName", filtertype: "text", render: $.fn.dataTable.render.link("AnQid") },
            { data: "Subject", name: "AcSubject", filtertype: "text" },
            { data: "ClassificationName", name: "AnClassificationQid" },
            { data: "LoginName", name: "AnLoginsQid" }
        ]

    });

    $dataTable.on('xhr.dt', function (e, settings, json, xhr) {
        initializeMailboxTableHeight();
    });
}

function openImportMailboxModal() {
    let tableTitles = [$("#NameLoc").val(), $("#SubjectLoc").val(), $("#IconLoc").val(), $("#ClassificationQIdLoc").val(), $("#LoginQidLoc").val()];
    let tableDataNames = ["Name", "Subject", "Icon", "AnClassificationQId", "AnLoginsQId"];
    let localization = {
        importType: $("#ImportTypeLoc").val(),
        forCountry: $("#ForCountryLoc").val(),
        dowloadFromServer: $("#DownloadFromServerLoc").val(),
        importIntoCodebook: $("#ImportIntoCodebookLoc").val(),
        importTypeAdding: $("#ImportTypeAddingLoc").val(),
        importTypeAddEdit: $("#ImportTypeAddEditLoc").val(),
        yesNo: $("#YesLoc").val() + "/" + $("#NoLoc").val()
    };

    openImportCodebookModal($("#ImportMailboxesLoc").val(), tableTitles, tableDataNames,
        $("#GetImportMailboxesUrl").val(), $("#ImportMailboxesUrl").val(), $("#ImportMailboxesHelpUrl").val(),
        localization,
        $("#CurrentUserLocalization").val(),
        function () {
            $.fn.dataTable.reload($('#grid-data'));
        });
}

var heightInitialized = false;
function initializeMailboxTableHeight() {

    if (heightInitialized) {
        return;
    }
    heightInitialized = true;

    var rectTop = document.getElementById("grid-data").getBoundingClientRect().top;
    var rectBottom = document.body.scrollHeight;

    let offset = 3;
    let dtHeight = (rectBottom - rectTop - offset) + "px";

    $(".dataTables_scrollBody").css("height", dtHeight);
    $(".dataTables_scrollBody").css("min-height", dtHeight);
}