var ids_delete        = [];
var ids_custom_delete = [];
var ids_to_buy        = [];

$(document).ready(function() {

    $(':input:checked').each(function () {
        ids_delete += $(this).attr('rel');
    });

    $('.btn-lista-compra').on('click', function (e) {
        /*get_checkeds();*/
        get_all();
        $.ajax({
            url: "lista-compra/useingredient",
            type: "POST",
            method:"post",
            data: {
                ids_used: ids_to_buy
            },
            success: function (data) {


                if(data != "" && data != 0)
                {
                    // Modal success
                    $('#modalSuccess').modal('show');
                }
                else
                {
                    // Modal error
                }

                setTimeout(function(){
                    location.reload();
                }, 2000);

            }
        });
        window.location.href = 'http://www.delsuper.es/inutralia?ids='+ids_to_buy.toString()
    });

    $('.btn-delete-checked').on('click', function (e) {
        get_checkeds();

        var reload = false;
        $.ajax({
            url: "lista-compra/delete",
            type: "POST",
            method:"post",
            data: {
                ingredients_ids: ids_delete
            },
            success: function (data) {
                if(reload)
                {
                    location.reload();
                }
                else
                {
                    reload = true
                }
            }
        });

        $.ajax({
            url: "lista-compra/deleteIds",
            type: "POST",
            method:"post",
            data: {
                ingredients_ids: ids_custom_delete
            },
            success: function (data) {


                if(data != "" && data != 0)
                {
                    // Modal success
                    $('#modalSuccess').modal('show');
                }
                else
                {
                    // Modal error
                }

                if(reload)
                {
                    location.reload();
                }
                else
                {
                    reload = true
                }
            }
        });
    });

    $('.btn-add-product').on('click', function () {

        var product = $('#product').val();

        $.ajax({
            url: "lista-compra/addcustomingredient",
            type: "POST",
            method:"post",
            data: {
                ingredient: product
            },
            success: function (data) {

                if(data != "" && data != 0){
                    // Modal success
                    $('#modalSuccess').modal('show');
                }else{
                    // Modal error
                }

                setTimeout(function(){
                    location.reload();
                }, 2000);

            }
        })
    })

});



function get_checkeds()
{

        $(':input:checked').each(function () {

        let id_val = $(this).attr('rel');

        if(id_val=="" || id_val==null)
        {
            let id_real_val = $(this).attr('rel2');
            if( jQuery.inArray(id_real_val, ids_custom_delete)<0)
            {
                ids_custom_delete.push(id_real_val);
            }
        }
        else
        {
            if( jQuery.inArray(id_val, ids_delete)<0)
            {
                ids_delete.push(id_val);
            }

            if( jQuery.inArray(id_val, ids_to_buy)<0 &&  $(this).attr('used')=="N")
            {
                ids_to_buy.push(id_val);
            }
        }
    });
}

function get_all()
{

    $(':input').each(function () {

        let id_val = $(this).attr('rel');

        if(id_val=="" || id_val==null)
        {
            let id_real_val = $(this).attr('rel2');
            if( jQuery.inArray(id_real_val, ids_custom_delete)<0)
            {
                ids_custom_delete.push(id_real_val);
            }
        }
        else
        {
            if( jQuery.inArray(id_val, ids_delete)<0)
            {
                ids_delete.push(id_val);
            }

            if( jQuery.inArray(id_val, ids_to_buy)<0 &&  $(this).attr('used')=="N")
            {
                ids_to_buy.push(id_val);
            }
        }
    });
}