function initAddToCart() {
  $(document).on('click', '.js-btn-fidelitycart', function(e) {
    e.preventDefault();

    var product_id = $(this).data('product-id'),
      layout_id = $(this).data('layout-id'),
      quantity = $(this)
        .parent()
        .find('input')
        .val(),
      grid = $(this).data('grid'),
      grid_status = $(this).data('grid-status'),
      grid_id = $(this).data('grid-id'),
      list_grid = $(this).data('list-grid');

    /*Final de versão - Precisa arrumar esse monte de variavel de grid*/

    quantity = typeof quantity !== 'undefined' ? quantity : 1;
    grid = typeof grid !== 'undefined' ? grid : '';
    grid_status = typeof grid_status !== 'undefined' ? grid_status : '';

    addToCart(product_id, quantity, layout_id, grid, grid_status, grid_id, list_grid);
  });
}

function addToCart(product_id, quantity, layout_id, grid, grid_status, grid_id, list_grid) {
  var url = 'index.php?route=fidelity/cart/add',
    data = {},
    btnCart = '';

  if (grid_id) {
    if (!list_grid) {
      btnCart = $(
        '.js-btn-cart[data-grid-id=' +
          grid_id +
          '][data-product-id=' +
          product_id +
          '], .js-btn-quick-view[data-grid-id=' +
          grid_id +
          '][data-product-id=' +
          product_id +
          ']'
      );
    } else {
      btnCart = $('.js-btn-cart[data-grid-id=' + grid_id + '], .js-btn-quick-view[data-grid-id=' + grid_id + ']');
    }
  } else {
    btnCart = $(
      '.js-btn-cart[data-product-id=' + product_id + '], .js-btn-quick-view[data-product-id=' + product_id + ']'
    );
  }

  var btnCartNotification = btnCart.siblings('.js-btn-cart-notification');

  data['product_id'] = product_id;
  data['quantity'] = typeof quantity != 'undefined' ? quantity : 1;
  data['layout_id'] = typeof layout_id != 'undefined' ? layout_id : false;

  if (grid_status) {
    grid = typeof grid == 'string' ? JSON.parse(grid) : grid;
    grid = window.objectToParam(grid);
    grid = '&' + grid;
    url = url + grid;
  }

  $.ajax({
    url: url,
    type: 'post',
    data: data,
    dataType: 'json',
    error: function(xhr, ajaxOptions, thrownError) {
      console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      alert('Desculpe! Não conseguimos adicionar o produto ao carrinho.');
    },
    success: function(json) {
      $('.success, .warning, .attention, .information, .error').remove();
      if (json['redirect']) {
        location = json['redirect'];
        return false;
      }

      if (json['error']) {
        $.each(json['error'], function(index, value) {
          window.notificationMsg(value, 'warning');
        });
        return false;
      }

      if (json['success']) {
        if (json['redirect']) {
          location = json['redirect'];
        } else {
          window.notificationMsg(json['success'], 'success');

          if (btnCartNotification.length > 0) {
            btnCartNotification.addClass('have-in-cart');
          }

          $('.js-header__cart-total').html(json['total_qtd']);
        }
      }
    },
  });
}

initAddToCart();
