function initializeWorkOrders() {

    setFromDailyPlanButtonState();

    var urlString = window.location.href;
    var url = new URL(urlString);
    var dateISO = decodeURI(url.searchParams.get("date"));
    var productionType = $("#ProductionType").val();
    var datafromDP = productionType === "MF" ? $("#data-from-daily-plan").is(":checked") : false;
    $("#DocTypes option[value='']").attr('selected', 'selected');

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

    initializeIdentBasicTypeahead(
        $('#IdentFilterSearch').attr('dataUrl'),
        $('#IdentFilterSearch'),
        $('#IdentFilterSearch'),
        { item: $("#identFilterItem").val(), name: $("#identFilterName").val() },
        null,
        function (item) {           
            $("#IdentFilterSearch").val(item.id);
            $.fn.dataTable.reload($mainGridTable);
            return { selected: true, value: item.id };

    });


    $(".datepicker").datepicker({
        format: 'dd.mm.yyyy',
        language: $('#languageDate').val()
    }).on('changeDate', function () {
        $(this).datepicker('hide');
        $.fn.dataTable.reload($mainGridTable);
    });

    $mainGridTable = $('#mainGrid');

    var mainTable = $mainGridTable.DataTable({
        ajax: {
            
            url: $mainGridTable.attr('data-url'),
            data: function (d) {
                d.DocTypes = $("#DocTypes").val();
                d.Statuses = $("#Status").val();
                d.Product = $("#IdentFilterSearch").val();
                d.ProductionType = productionType;
                d.DataFromDailyPlan = datafromDP;
                d.DateFrom = reformatDate($("#Filter_DateFrom").val());
                d.DateTo = reformatDate($("#Filter_DateTo").val());
                d.Resources = $("#ResursID").val();
            },
            
        },
        columns: [
            {
                data: function (data) {
                    return "<span style='width: 200px;position: relative;text-align: center;vertical-align: middle;display: table-cell;height: 150px;'> <img src='" + data.Picture + "' /> </span>";
                },
                orderable: false,
                render: function (data) {
                    return data;
                },
                createdCell: function (td, cellData, full) {
                    $(td)
                        .attr('data-toggle', 'tooltip')
                        .attr('data-placement', 'top')
                        .attr('data-original-title', full.Product);

                }
            },
            {
                data: "Product",
                name: "acName",
                filtertype: "text",
                render: function (data, type) {
                    return type === 'display' ?
                        '<p style="font-size: 14px;font-weight: bold;overflow: hidden;text-overflow: ellipsis; margin:0px auto;">' + data + ' </p> ': data;
                },

                createdCell: function (td, cellData) {
                    $(td)
                        .attr('data-toggle', 'tooltip')
                        .attr('data-placement', 'top')
                        .attr('data-original-title', cellData);

                }
            },
            {
                data: "AcKeyDisplay",
                name: "AcKey",
                filtertype: "text",
                className: "dt-body-left",

                render: function (data, type, full) {
                    return type === 'display' ?
                        '<span style="margin-right:2px"> <i class="far fa-hashtag"></i> </span> <span>' + data + '</span>' : data;
                }
            },
            {
                data: 'Date',
                name: "adDate",
                render: function (data, type, full) {
                    return type === 'display' ?
                        '<span style="margin-right:2px"> <i class="far fa-calendar alt"></i> </span> <span>' + data + '</span>' : data;
                }
            },
            {
                data: 'CompletedPrc',
                render: function (data, type, row, meta) {
                    return type === 'display' ?
                        '<progress value=' + data + ' max="100" style="width: 100%"></progress>' : data;
                }
            },
            {
                data: 'Status',
                render: function (data, type, full) {
                    var completedPrc = $.fn.dataTable.render.number('.', ',', 0, '', "").display(full.CompletedPrc);
                    var planQty = $.fn.dataTable.render.number('.', ',', 0, '', "").display(full.PlanQty);
                    var producedQty = $.fn.dataTable.render.number('.', ',', 0, '', "").display(full.ProducedQty);
                    return type === 'display' ?
                        '<span>' + data + ' (' + completedPrc + '%)</span>  <span class="pull-right">' + producedQty + '/' + planQty + '</span>' : data;
                }
            },

        ],
        createdRow: function (row, data, dataIndex) {
            $(row).css('cursor', 'pointer');
        },
        drawCallback: function () {
            $("[data-toggle='tooltip']").tooltip({
                container: 'body'
            });
        }

    });

    $("select").off("change.tableReload");
    $("select").on("change.tableReload", function () {
        $.fn.dataTable.reload($mainGridTable);
    });

    mainTable.on('click', 'tr', function () {
        var data = mainTable.row($(this)).data();
        window.location.href = "Manufacturing/CompletWork?key=" + data.AcKey + '&type=' + productionType;
    });


    $identTable = $('#stockGrid');
    $identTable.DataTable({
        ajax: {
            url: $identTable.attr('data-url'),
            data: function (d) {
                d.Ident = $("#IdentFilterSearch").val();    
            }
        },
        columns: [
            { data: "Ident", name: "AcIdent", filtertype: "text", render: $.fn.dataTable.render.link("Ident") },
            { data: "Name", name: "AcIdentNavigation.AcName", filtertype: "text", render: $.fn.dataTable.render.link("Ident") }
        ]
    });

};


function SetDataFromDP() {
    var datafromDP = $("#data-from-daily-plan").is(":checked");
    localStorage.setItem('WoFromDailyPlan', datafromDP);
    location.reload();
}


function setFromDailyPlanButtonState() {
    var fromDailyPlan = localStorage.getItem('WoFromDailyPlan');
    if (fromDailyPlan && fromDailyPlan.toString() === "true") {
        $("#data-from-daily-plan").prop('checked', true);
    }
}
