function initializeOrderReport(showMultipleSelection) {
    $(document).ready(function () {
        var urlString = window.location.href;
        var url = new URL(urlString);
        var acKey = decodeURI(url.searchParams.get("acKey"));
        var dateISO = decodeURI(url.searchParams.get("date"));

        if (urlString.includes("date")) {
            var date = fromISOToStandardDateFormatString(dateISO);
            $("#Filter_DateFrom").val(date);
        }

        $('.datepicker').datepicker()
            .on('click', function (e) {
                $(this).select();
            }).on('changeDate', function () {
                $(this).datepicker('hide');
                $.fn.dataTable.reload($dataTable);
            });

        $dataTable = $('#order-grid');
        $dataTable.DataTable({
            ajax: {
                url: $dataTable.attr('data-url'),
                data: function (d) {
                    d.Keywords = $("#Keywords").val();
                    d.DateFrom = reformatDate($("#Filter_DateFrom").val());
                    d.DateTo = reformatDate($("#Filter_DateTo").val());
                    d.DocType = $("#DocType").val();
                    d.ItemDisplayFilter = $("#customDateEnum").val();
                }
            },
            columns: [
                {
                    data: function (data) {
                        return data.AcKey;
                    },
                    orderable: false,
                    className: 'dt-body-center',
                    render: function (data) { return showMultipleSelection ? '<input class="ackey" name="OrderAcKey" onclick=CheckBoxClicked("' + data + '") value="' + data + '" type="checkbox"/>' : '' }
                },
                { data: "", name: "", className: 'dt-body-center dt-head-center', render: $.fn.dataTable.render.previewReportTreeOptions("AcKey", null, null) },
                {
                    data: "FiscalNumber",
                    name: "AcHRFiskalNumber",
                    filtertype: "text",
                    visible: $("#OrderType").val() === $("#orderTypeBuyer").val() && $("#userLoc").val() === $("#hrLoc").val() ? true : false
                },
                { data: "AcKeyDisplay", name: "AcKey", filtertype: "text", render: $.fn.dataTable.render.link("AcKey") },
                { data: "Consignee", name: "AcConsignee", filtertype: "text", render: $.fn.dataTable.render.link("Consignee"), visible: $("#OurCompIsName2").val() === "False" },
                { data: "ConsigneeName", name: "AcConsigneeNavigation.AcName2", filtertype: "text", render: $.fn.dataTable.render.link("Consignee", false, null, 30), visible: $("#OurCompIsName2").val() === "True" },
                { data: "Receiver", name: "AcReceiver", filtertype: "text", render: $.fn.dataTable.render.link("Receiver"), visible: $("#OurCompIsName2").val() === "False" },
                { data: "ReceiverName", name: "AcReceiverNavigation.AcName2", filtertype: "text", render: $.fn.dataTable.render.link("Receiver", false, null, 30), visible: $("#OurCompIsName2").val() === "True" },
                { data: "Price", name: "AnValue", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
                { data: "PriceWithVAT", name: "AnForPay", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
                { data: "Date", name: "AdDate", filtertype: "dateTime" },
                { data: "ExpiryDate", name: "AdDateValid", filtertype: "dateTime" },
                { data: "DeliveryDate", name: "AdDeliveryDate", filtertype: "dateTime" },
                {
                    data: "Status",
                    name: "AcStatusNavigation.AcName",
                    filtertype: "text",
                    render: function (data, type, full) {
                        if (data === $("#orderStatusOffer").val()) {
                            return '<span class="label label-primary">' + full.StatusName + '</span>';
                        } else if (data === $("#orderStatusConfirmed").val() || data === $("#orderStatusShipped").val()) {
                            return '<span class="label label-success"> ' + full.StatusName + '</span>';
                        } else if (data === $("#orderStatusFinished").val()) {
                            return '<span class="label label-default"> ' + full.StatusName + '</span>';
                        } else {
                            return '<span class="label label-default"> ' + full.StatusName + '</span>';
                        }
                    }
                }
            ],
            colReorder: {
                fixedColumnsLeft: 2,
            },
            createdRow: function (row, data, dataIndex) {
                if (data.AcKey === acKey) {
                    $(row).addClass('side-menu-item-active').children(":first").addClass('side-menu-item-active');
                }

                $(row).find(".custom-dropdown").customDropdown();
                setPreviewDropdownToggle($(row).find(".custom-dropdown"), data.AcKey);
            }
        }).on('xhr.dt', function (e, settings, json, xhr) {
            if (json && json.prop) {
                $('#grid-data-total-sum').html($.fn.dataTable.render.number('.', ',', 2).display(json.prop.Sum));
                $('#grid-data-vat-total-sum').html($.fn.dataTable.render.number('.', ',', 2).display(json.prop.SumWithVat));
                $('.grid-data-total-items').html(json.recordsTotal);
            }
        });

        $("select").off("change.tableReload");
        $("select").on("change.tableReload", 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);
        });

        $('#ItemDisplayFilter').on('change', function () {
            if ($(this).find('option:selected').hasClass('customDateFilter'))
                $('.DateFilter').removeClass('hide');
            else
                $('.DateFilter').addClass('hide');
        });
    });
}

var selectedKeys = [];
function CheckBoxClicked(acKey) {
    if (!selectedKeys.includes(acKey)) {
        selectedKeys.push(acKey);
    }
    else {
        let index = selectedKeys.indexOf(acKey);
        selectedKeys.splice(index, 1);
    }

    if (selectedKeys.length > 0) {
        $('#actionsButton').removeClass('disabled');
    }
    else {
        $('#actionsButton').addClass('disabled');
    }
}

function printOrders() {
    var url = $("#Print").val();
    var params = JSON.stringify(selectedKeys);
    var finalUrl = url + "?acKeys=" + params;

    window.open(finalUrl, '_blank');
}

function openPdfPreview(url) {
    loadPdfPanelContentPAAS($("#pdfPanel"), url, true);
}