function initializeOutstandingItems() {

    initializePartnerTypeahead($('#SubjectId').attr('dataUrl'), $('#SubjectId'), $("#SubjectId"), function (item) {
        $("#SubjectId").val(item.value);
        $.fn.dataTable.reload($dataTable);
        return { selected: true, value: item.id };
    });

    $dataTable = $('#outstandingitems');

    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url'),
            data: function (d) {
                d.Keywords = $("#Keywords").val();
                d.DateFrom = reformatDate($("#DateFrom").val());
                d.SubjectId = $("#SubjectId").val();
                d.Department = $("#Department").val();
                d.CostDrv = $("#CostDrv").val();
                d.DetailedByBill = $("#DetailedByBill").val();
                d.OpenItem = $("#OpenItem").val();
            }
        },
        columns: [
            { data: "LinkDoc", name: "LinkDoc", filtertype: "text" },
            {
                data: "SubjectId",
                name: "SubjectId",
                filtertype: "text",
                render: function (data) {
                    if (data === null || data === "") {
                        return "";
                    } else {
                        return '<a href="Subjects/Edit/?id=' + data + '">' + data + '</a>';
                    }
                }
            },
            { data: "KeyView", name: "KeyView", filtertype: "text" },
            { data: "Account", name: "Account", filtertype: "text" },
            {
                data: "DateDue", name: "DateDue", filtertype: "text", render: function (data) {
                    if (data === null) {
                        return "";
                    } else {
                        return moment(data).format('DD.MM.YYYY');
                    }
                }
            },
            {
                data: "DateDoc", name: "DateDoc", filtertype: "text", render: function (data) {
                    if (data === null) {
                        return "";
                    } else {
                        return moment(data).format('DD.MM.YYYY');
                    }
                }
            },
            //{ data: "Doc", name: "Doc", filtertype: "text" },        
            { data: "Debit", name: "Debit", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
            { data: "Credit", name: "Credit", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
            { data: "Balance", name: "Balance", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
            { data: "Currency", name: "Currency", filtertype: "text" },
            //{ data: "Name2", name: "Name2", filtertype: "text" },
            //{ data: "Note", name: "Note", filtertype: "text" }
        ],
        colReorder: {
            fixedColumnsLeft: 1,
        },
    }).on('xhr.dt', function (e, settings, json, xhr) {
        //reloadTotals(json.prop);
    });

    $("#lblOpenDate").css("display", "block");
    $("#lblDueDate").css("display", "none");
    $(".SubjSubject").css("display", "block");
    $(".SubjSupplier").css("display", "none");
    $(".SubjBuyer").css("display", "none");
    $("#printButton").prop("disabled", false);
    $("#printButton + button").prop("disabled", false);
}

$('#OpenItem').on('select2:select', function (e) {
    if (e.params.data.id == 'due') {
        $("#lblDueDate").css("display", "block");
        $("#lblOpenDate").css("display", "none");
    }
    else {
        $("#lblOpenDate").css("display", "block");
        $("#lblDueDate").css("display", "none");
    }
});

$('#DetailedByBill').on('select2:select', function () {
    var value = $("#DetailedByBill").val();
    var table = $('#outstandingitems').DataTable();
    if (value === '32Q') {
        table.columns('.08P').visible(false);
    }
    else {
        table.columns('.08P').visible(true);
    }
});

$('#Type').on('select2:select', function (e) {
    if (e.params.data.id == 0) {
        $("#SubjSubject").first().css("display", "none");
        $("#SubjSupplier").first().css("display", "none");
        $("#SubjBuyer").first().css("display", "block");
        $("#thSubjectType").text($("#LblBuyer").val());
    }
    else if (e.params.data.id == 1) {
        $("#SubjBuyer").first().css("display", "none");
        $("#SubjSupplier").first().css("display", "block");
        $("#SubjSubject").first().css("display", "none");
        $("#thSubjectType").text($("#LblSupplier").val());
    }
    else {
        $("#SubjBuyer").first().css("display", "block");
        $("#SubjSupplier").first().css("display", "none");
        $("#SubjSubject").first().css("display", "none");
        $("#thSubjectType").text($("#LblSubject").val());
    }
});

let printWindow;
function printOutstandingItems(reportCode) {
    var url = $("#OutstandingItemsPrintUrl").val() + "?Date=" + reformatDate($("#DateFrom").val());

    var subject = $("#SubjectId").val();
    // TODO: Add filters when available

    if (subject)
        url = url + "&Subject=" + subject;
    if (reportCode)
        url = url + "&ReportType=" + reportCode;

    url = url + "&filterColumn=" + $("#filterColumn").val()
        + "&filterCriteria=" + $("#filterCriteria").val()
        + "&filterValue=" + $("#filterValue").val()
        + "&filterValue2=" + $("#filterValue2").val();

    printWindow = window.open();
    printWindow.document.write(generatePrintingPage());
    printPAAS("", false, false, url, false);
}