$(document).ready(function () {

    $('.fa.fa-plus').click(function () {

        var name = this.id.substring(1)
        var value = parseInt($('#' + name).val());
        value = value + 1;
        $("#" + name).val(formatDisplay($("#WoExDecPlacesProd").val(), value));

    });

    $('.fa.fa-minus').click(function () {

        var name = this.id.substring(1)
        var value = parseInt($('#' + name).val());
        value = value - 1;
        $("#" + name).val(formatDisplay($("#WoExDecPlacesProd").val(), value));
    });

    $('image').each(function (i) {
        var self = $(this);
        var src = self.attr('src');

        if (!src.trim())
            self.addClass('far fa-image');
    });

});


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(formatDisplay($("#WoExDecPlacesProd").val(), value));
}


function saveReceive() {

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

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

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

        return false;
    };

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

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

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

            if (response.Success) {

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

            } else {

                let errorText = response.Error;

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

}