var $mailBoxesDataTable;

function initializeClassifications() {
    $dataTable = $('#grid-data');
    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url'),
            data: function (d) {
                d.Keywords = $("#Keywords").val();
            }
        },
        columns: [
            { data: "Type", name: "Type" },
            { data: "TypeDesc", name: "TypeDesc", render: $.fn.dataTable.render.ellipsis(50) },
            {
                data: "DocKind", name: "DocKind", render: function (data, type, full) {
                    if (type === 'display') {
                        switch (data) {
                            case 'O': return $("#OtherMaterialLoc").val();
                            case 'A': return $("#ArchiveMaterialLoc").val();
                            case 'T': return $("#PermanentDocumentedMaterialLoc").val();
                            default: return "";
                        }
                    }
                }
            },
            { data: "WorkflowName", name: "WorkflowId" },
            { data: "ValidValue", name: "ValidValue", className: 'dt-body-right' },
            {
                data: "ValidType", name: "ValidType", render: function (data, type, full) {
                    if (type === 'display') {
                        switch (data) {
                            case 'Y': return $("#YearsFilterLoc").val();
                            case 'M': return $("#MonthsFilterLoc").val();
                            case 'D': return $("#WeeksLoc").val();
                            default: return "";
                        }
                    }
                }
            },
            { data: "Clerk", name: "Clerk" }
        ]
    });

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

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

function openImportClassificationModal() {
    let tableTitles = [$("#CodeLoc").val(), $("#MaterialTypeDescLoc").val(), $("#MaterialTypeLoc").val(), $("#ValidityLoc").val(), $("#ValidityTypeLoc").val(), $("#NoteLoc").val()];
    let tableDataNames = ["Type", "TypeDesc", "DocKindText", "ValidValue", "ValidTypeText", "Note" ];
    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($("#ImportClassificationsLoc").val(), tableTitles, tableDataNames,
        $("#GetImportClassificationsUrl").val(), $("#ImportClassificationsUrl").val(), $("#ImportSettingsClassificationUrl").val(),
        localization,
        $("#CurrentUserLocalization").val(),
        function () {
            $.fn.dataTable.reload($('#grid-data'));
        },
        function (data) {
            data.group = $(".codebook-group").val();
            data.code = $(".codebook-code").val();
            data.desc = $(".codebook-description").val();
        },
        "<div class='col-sm-8 m-t-xs'><label>" + $("#GroupLoc").val() + "</label><select class='form-control codebook-group'></select></div>" +
        "<div class='col-sm-8 m-t-xs'><label>" + $("#TheCodeLoc").val() + "</label><input class='form-control codebook-code' /></div>" +
        "<div class='col-sm-8 m-t-xs'><label>" + $("#DescriptionLoc").val() + "</label><input class='form-control codebook-description' /></div>",
        function (data) {
            if (data.ClassificationGroup && $(".codebook-group").children().length === 0) {
                for (let i = 0; i < data.ClassificationGroup.length; i++) {
                    let classificationGroup = data.ClassificationGroup[i];
                    let displayText = classificationGroup.AcName === null ? classificationGroup.AcID : classificationGroup.AcID + " - " + classificationGroup.AcName;
                    $(".codebook-group").append("<option value='" + (classificationGroup.Group === null ? "" : classificationGroup.AcID) + "'>" + displayText + "</option>");
                }
            }
        }
    );
}

var heightInitialized = false;
function initializeTableHeight() {

    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);
}