
window.onload = function(){

	let url_products = "index.php?route=extension/module/outofstock/getProducts";
	
	$.ajax({
        url: url_products,
        type: 'GET',
        dataType: 'json',
        beforeSend: function() {
        },
        complete: function() {
        },
        success: function(json) {
            if(json.products){
              json.products.forEach((val) => findZeroPriceProduct(val, json.outofstock_btntext, json.outofstock_btncolor, json.outofstock_btntextcolor));
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });	
}

function findZeroPriceProduct(product, btntext, btncolor, btntextcolor){
		
    //console.log(product.product_id)
	$( `button[onclick^="cart.add('${product.product_id}'"]`).attr("onclick", `addname('${product.name}')`).attr("data-target", "#outofstock").attr("data-toggle", "modal").attr("tabindex", "-1").css({"background-color":btncolor, "color": btntextcolor }).html(btntext);
	
    let product_id = $("#product input[name='product_id']").val();
    
    if(product_id == product.product_id){
        $("#button-cart").off("click").attr("onclick", `addname('${product.name}')`).attr("data-target", "#outofstock").attr("data-toggle", "modal").attr("tabindex", "-1").removeClass("btn-primary").css({"background-color":btncolor, "color": btntextcolor, "border-color": btncolor }).html(btntext);
    }

}

function addname(product_name){
    $('#addproductname').val(product_name);
}