function incrementQuantity(anSubNo, anResursQId, isSml = false) {
    let input;
    if (isSml) {
        input = document.getElementById(anSubNo + '-' + anResursQId + '-quantity-sml');
    } else {
        input = document.getElementById(anSubNo + '-' + anResursQId + '-quantity');
    }

    input.value = parseInt(input.value) + 1;

    updateQuantity(anSubNo, anResursQId, input.value, isSml);
}

function decrementQuantity(anSubNo, anResursQId, isSml = false) {
    let input;
    if (isSml) {
        input = document.getElementById(anSubNo + '-' + anResursQId + '-quantity-sml');
    } else {
        input = document.getElementById(anSubNo + '-' + anResursQId + '-quantity');
    }

    if (parseInt(input.value) > 0) {
        input.value = parseInt(input.value) - 1;
        updateQuantity(anSubNo, anResursQId, input.value, isSml);
    }
}

function updateQuantity(anSubNo, anResursQId, value, isSml = false) {
    $.ajax({
        cache: false,
        type: "POST",
        url: $('#UpdateToolQuantityURL').val(),
        data: {
            anSubNo: anSubNo,
            anResursQId: anResursQId,
            quantity: value,
            isSml: isSml
        },
        error: function (error) {
            console.error(error)
        }
    });
}

function saveMaterial(button) {

    var buttonId = $(button).attr('id');

    var receiveURL = $("#receiveURL").val();
    var buttonText = $("#buttonText").val();
    var succesText = $("#succesText").val();
    var qty = $("#qty").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'
                }
            ]
        });
        customModalForMobile();
        return false;
    };

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

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

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

    $.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();
                            }
                        }
                    ]
                });
                customModalForMobile();

            } 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'
                        }
                    ]
                });
                customModalForMobile();

            }

            swapLoadingIcon(false, buttonId, "fa fa-check");
        });

}