$(document).ready(function() {
  $('.ftr-js-btn-cart-all').on('click', function(){

    var 
      data  = [],
      $this = $(this);
   
    $('.ftr-js-grid-color .ftr-js-quantity').each(function(index) {
      if ($(this).val() > 0) {
        var infos = {};
        infos['quantity']   = $(this).val();
        infos['product_id'] = $(this).data('product-id');
        data.push(infos);
      }
    });

    $('.ftr-js-product-card__notification').remove();
    $('[class^=ftr-grid]').removeClass('ftr-outline--red ftr-outline--green');
    $('.ftr-loading-bar').removeClass('ftr-none');
    $('.ftr-js-btn-cart-all').prop('disabled', true);

    $.ajax({
      url : 'index.php?route=checkout/cart/addlistcolor',
      type: 'post',
      data: objectToParam(data),
      dataType: 'json',
      error: function(json) {
        alert('Erro!');
      },
      success: function(json) {
        if (json['error']) {
          $.each(json['error'], function(index, value){   
            var $element = $('.ftr-js-grid-color .ftr-js-quantity[data-product-id = ' + index + ']');
            $element.closest('[class^=ftr-grid]').addClass('ftr-outline--red');
            $element.closest('[class^=ftr-grid]').prepend('<div class="ftr-product-card__notification ftr-js-product-card__notification hide">' + value + '<div class="ftr-text--center"><i class="material-icons">&#xe316;</i></div></div>');
            setTimeout(function () {
              $('.ftr-js-product-card__notification').removeClass('hide');
            }, 100);
          });
          $('.ftr-js-product-card__notification').on('click', function(){
            if ($(this).hasClass('retract')) {
              $(this).removeClass('retract');              
            } else {
              $(this).addClass('retract');
            }
          });
        }

        if (json['success']) {
          $.each(json['success'], function(index, value){
            var $element = $('.ftr-js-grid-color .ftr-js-quantity[data-product-id = ' + index + ']');
            $element.closest('[class^=ftr-grid]').addClass('ftr-outline--green');
            $element.val(0);
          });
          updateCartApp(json['total_qtd']);
        }        
        $('.ftr-loading-bar').addClass('ftr-none');
        $('.ftr-js-btn-cart-all').removeAttr('disabled');
      }
    });
    
  });
});