var woExDecPlacesProd = $("#WoExDecPlacesProd").val();

function initializeWorkOrderReceiving() {

    $(".datepicker").datepicker({
        format: 'dd.mm.yyyy',
        language: $('#languageDate').val()
    }).on('changeDate', function () {
        $(this).datepicker('hide');
    });
    $('.card-work-reg').find('input[type=number]').val(round(0, woExDecPlacesProd));

    $('.btn-plus-minus').click(function () {

        var control = $(this).parents("div").children(".form-control");

        if ($(this).children().hasClass("fa-plus")) {
            control.val(roundToPercision(woExDecPlacesProd, Number(control.val()) + 1));
        };

        if ($(this).children().hasClass("fa-minus")) {
            if (Number(control.val()) > 0)
                control.val(roundToPercision(woExDecPlacesProd, Number(control.val()) - 1));
        };
        control.trigger('change');

    });

    $('#finished').on("change", function () {
        if (Number($("#RestQty").val().replace(',', '.')) <= Number($("#finished").val())) {
            if (!$("#finish-task").hasClass('active')) {
                $("#finish-task").addClass("active");
                $("#finish-task :input").prop('checked', true);
            }
        } else {
            if ($("#finish-task").hasClass('active')) {
                $("#finish-task").removeClass("active");
                $("#finish-task :input").prop('checked', false);
            }
        }
    });

    $('#options').on("click", function () {
        $('#sideNavBar>.tab-pane').removeClass("active");
        $('#tabNav>.divided-tab-2').removeClass("active");
        $('#tabNav>#homeTab').addClass("active");
        $('#sideNavBar>#additional-settings').addClass("fade").addClass("in").addClass("active");
        $(".divided-content").addClass("divided-content-side-opened").removeClass("divided-content-side-closed");

        var caption = $("#additional-settings").data('tab-caption');
        $("#tabCaption").text(caption);

    });

    $('#warehouse').on("click", function () {
        $('#sideNavBar>.tab-pane').removeClass("active");
        $('#sideNavBar>.nav>.nav-item').removeClass("active");
        $('#sideNavBar>#warehouse-list').addClass("fade").addClass("in").addClass("active");
        $(".divided-content").addClass("divided-content-side-opened").removeClass("divided-content-side-closed");

        var caption = $("#warehouse-list").data('tab-caption');
        $("#tabCaption").text(caption);

    });

    $('#apply').on("click", function () {
        $(".divided-content").addClass("divided-content-side-closed").removeClass("divided-content-side-opened");

    });

    $('#back').on("click", function () {
        $(".divided-content").addClass("divided-content-side-closed").removeClass("divided-content-side-opened");

    });

    $('#image').on("click", function () {
        $("#imageModal").modal('show');
    }).hover(function () {
        $(this).css('cursor', 'pointer');
    });

    $('#finish-task').on("click", function () {
        if ($("#finish-task").hasClass('active')) {
            $("#finish-task").removeClass("active");
            $("#finish-task :input").prop('checked', false);

        } else {
            $("#finish-task").addClass("active");
            $("#finish-task :input").prop('checked', true);
        }
    });

    $('.warehouse-list-item').on("click", function () {
        $('.warehouse-list-item').find('i').removeClass('fa-check');
        $('.warehouse-list-item').removeClass('active');

        $('#receivingWarehouse').val($(this).data("value"));

        $(this).find('i').toggleClass('fa-check');
        $(this).addClass("active");

    });

    if ($("#Picture").val() == "") {

        $("#image").addClass('hidden');
        $("#noImage").removeClass('hidden');

    }

    if ($("#productionTypeMF").val() === $("#ProdType").val()) {
        setGlobalExampleHelpUrl($("#manufacturingExampleMF").val());
    } else if ($("#productionTypeSE").val() === $("#ProdType").val()) {
        setGlobalExampleHelpUrl($("#manufacturingExampleSE").val());
    }
};

function saveReceive(isMobile) {

    var qty = $("#finished").val();

    if (qty === "" || qty == 0) {

        $().customModal({
            destroyOnClose: true,
            showCloseButton: false,
            id: "requiedInput",
            type: "alert",
            text: $("#requiedQty").val(),
            buttons: [
                {
                    icon: 'fa fa-check',
                    text: $("#buttonText").val(),
                    closeOnAction: true,
                    type: 'primary'
                }
            ]
        });
        customModalForMobile(isMobile);
        return false;
    };

    var formData = $("#workOrderReceiving").serializeArray();

    $.each(formData, function () {
        if (!isNaN(this.value)) {
            this.value = this.value.replace('.', ',');
        }

        if (this.name == "Finished") {
            this.value = 'true'
        }
    });

    swapLoadingIcon(true, "confirmButton", "fa fa-check");

    $.post($("#receiveURL").val(), formData)
        .done(function (response) {

            if (response.Success) {

                $().customModal({
                    destroyOnClose: true,
                    showCloseButton: false,
                    id: "displayPopupSuccessModal",
                    type: "success",
                    text: $("#succesText").val(),
                    buttons: [
                        {
                            icon: 'fa fa-check',
                            text: $("#buttonText").val(),
                            closeOnAction: true,
                            type: 'primary',
                            action: function () {
                                location.reload();
                            }
                        }
                    ]
                });

                customModalForMobile(isMobile);


            } else {

                let errorText = response.Error;

                $().customModal({
                    destroyOnClose: true,
                    showCloseButton: true,
                    id: "displayPopupErrorModal",
                    type: "alert",
                    text: errorText,
                    buttons: [
                        {
                            icon: 'fa fa-check',
                            text: $("#buttonText").val(),
                            closeOnAction: true,
                            type: 'primary'
                        }
                    ]
                });
                customModalForMobile(isMobile);
            }
            swapLoadingIcon(false, "confirmButton", "fa fa-check");
        });

}

function getReceivingWarehouse() {
    return $('.warehouse-list-item.active').data('value');
};

function addRest(context) {
    var name = context.id.substring(1);
    var value = 0;
    if (name === 'finished')
        value = parseInt($("#RestQty").val());
    if (name === 'waste')
        value = parseInt($("#RestWasteQty").val());
    if (name === 'scrap')
        value = parseInt($("#RestScrapQty").val());

    $("#" + name).val(roundToPercision(woExDecPlacesProd, value));

}

function customModalForMobile(isMobile) {
    if (isMobile) {
        $(".custom-modal-footer>div>button").addClass("btn-rounded");
        $(".custom-modal-body-content>span").addClass("h4");
        $(".custom-modal-title").css("font-size", 24);
    }
}