function fetchCities(elem)
{
    let country_id = $(elem).val()
    $.ajax
    ({
        url : url_fetch_cities,
        type: 'GET',
        data: 'country_id='+country_id,
        success:function(data)
        {
            $('#city_id').html(data)
        }
    })
}


function favorite(elem)
{
    if (user)
    {
        let product_id = $(elem).attr('data-id')
        $.ajax
        ({
            url : url_favorite,
            type: 'POST',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            data: 'product_id='+product_id,
            success:function(data)
            {
                $(elem).toggleClass(`active`);
                var url = $(location).attr('href'),
                    parts = url.split("/"),
                    last_part = parts[parts.length-1];
                if(last_part == 'favorite')
                {
                    $(elem).closest('.col-lg-3').fadeOut(1000);
                    setTimeout(
                        function()
                        {
                            location.reload();
                        }, 1100);
                }
                toasterSuccess(data.message);
            }
        })
    }
    else
    {
        toasterError(login_first);
    }
}

function addToCart(elem)
{
    let product_id = $(elem).attr('data-id')
    let count = $(elem).attr('count-id');
    if(count == null)
    {
        count = $('#count_product').val();
    }
    $.ajax
    ({
        url : url_add_to_cart,
        type: 'POST',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        data: 'product_id='+product_id+'&count='+count,
        success:function(data)
        {
            toasterSuccess(data.message);
        }
        ,error:function(data)
        {
            if (typeof data.responseJSON.errors == 'object')
            {
                toasterError(Object.values(data.responseJSON.errors)[0]);
            }
            else
            {
                toasterError(data.responseJSON.errors);
            }
        }
    })
}
