{% comment %}
    Renders a product card using "Grid" style
    Accepts:
    - max_height: {Number} Maximum height of the product's image (required)
    - product: {Object} Product Liquid object (required)
    - show_vendor: {Boolean} Show the product's vendor depending on the section setting (optional)

    Usage:
    {% include 'product-card-grid', max_height: max_height, product: product, show_vendor: section.settings.show_vendor %}
{% endcomment %}
<div class="grid-view-item{% unless product.available %} grid-view-item--sold-out{% endunless %} product-card">
  {% comment %}
  <a class="grid-view-item__link grid-view-item__image-container full-width-link" href="{{ product.url | within: collection }}">
    <span class="visually-hidden">{{ product.title }}</span>
  </a>
  {% endcomment %}

  {% capture img_id %}ProductCardImage-{{ section.id }}-{{ product.id }}{% endcapture %}
  {% capture wrapper_id %}ProductCardImageWrapper-{{ section.id }}-{{ product.id }}{% endcapture %}
  {%- assign preview_image = product.featured_media.preview_image -%}
  {%- assign img_url = preview_image | img_url: '320x'  -%}

  {% unless preview_image == blank %}
    {% include 'image-style', image: preview_image, height: max_height, wrapper_id: wrapper_id, img_id: img_id %}
  {% endunless %}

  <div class="product-card__image-with-placeholder-wrapper" data-image-loading-animation>
    <div id="" class="grid-view-item__image-wrapper product-card__image-wrapper js">
      <div style="animatable fadeInUp">
        <a href="{{ product.url | within: collection }}">
          <img id="" class="grid-view-item__image owl-lazy image-{{product.id}}"
              alt="{{ preview_image.alt }}"
              data-src="{{ img_url }}"> </a>
      </div>
    </div>
    {% if product.tags contains 'sale' %}
    <div class="sale-label"><p>Sale</p></div>
    {% elsif compare_at_price > price %}
    <div class="sale-label"><p>{{ 'products.product.on_sale' | t }}</p></div>
    {% if product.available == false %}
    <div class="sold-out">
      <span>{{ 'products.product.sold_out' | t }}</span>
    </div>
    {% endif %}
    {% endif %}
    {% if product.tags contains 'new' %}
    <div class="new-label"><p>New</p></div>
    {% endif %}
    <div class="quick-view-button"><a class="quick-view" data-handle="{{ product.handle }}" href="javascript:void(0);"><i class="fa fa-eye" aria-hidden="true"></i></a></div>
    {% if section.settings.add_cart_show%}
    {% endif %}
  </div>

  <noscript>
    {% capture image_size %}{{ max_height }}x{{ max_height }}{% endcapture %}
    <img class="grid-view-item__image" src="{{ preview_image | img_url: image_size, scale: 2 }}" alt="{{ preview_image.alt }}" style="max-width: {{ max_height | times: preview_image.aspect_ratio }}px;">
  </noscript>

  <div class="product_info_details">
    <div class="h4 grid-view-item__title product-card__title" aria-hidden="true"><a href="{{ product.url | within: collection }}">{{ product.title }}</a></div>

    {% comment %}
    <span class="shopify-product-reviews-badge" data-id="{{ product.id }}"></span>
    {% endcomment %}

    {% include 'product-price-listing', product: product, show_vendor: show_vendor %}

    {% comment %}
    <div class="description">
      <p>{{ product.description | truncate: 75 }}</p>
    </div>
    {% endcomment %}


    {% if section.settings.swatch_image %}
    <form method="post" action="/cart/add" class="{% if section.settings.show_variant == blank %}add_btn{% endif %}" >
      <div class="product-form__controls-group">
        {% unless product.has_only_default_variant %}
        {% for option in product.options_with_values %}
        <div class="selector-wrapper js product-form__item {{ option.name }}">
          <div class="swatch_item">
            {% for value in option.values limit: 6 %}
            {% if option.name contains "Color" %}
            {% for variant in product.variants %}
            {%- assign display = "block" -%}
            {% if variant.image and variant.title contains value%}
            {%- assign img = variant.image | img_url: "640x" -%}
            {%- assign imgswatch = variant.image | img_url: "70x" -%}
            {%- assign display = "none" -%}
            {% endif %}
            {% endfor %}

            {% if option.name contains "Color" %}
            <span onclick="colorSwatchHandler('{{value}}');"  data-bg="{{ imgswatch }}" style="background-size: cover;" data-pro-org-img="{{img}}" data-color-pro-id="{{product.id}}" class="color-swatch lazyload  color-swatch-{{value}}" title="{{value}}">
            </span>
            {% endif %}
            {% endif %}
            {% endfor %}
          </div>

          <select style="display:none" class="single-option-selector single-option-selector-{{ section.id }} product-form__input" id="SingleOptionSelector-{{ forloop.index0 }}" data-index="option{{ forloop.index }}">
            {% for value in option.values %}
            <option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
            {% endfor %}
          </select>

        </div>
        {% endfor %}
        {% endunless %}

        <script type="text/javascript">
          function colorSwatchHandler(color) {
            var colorOptionElm = document.querySelector('select option[value="' + color + '"]'),
                colorSpanElm = document.querySelector('span[title="' + color + '"]'),
                activeSwatch = document.querySelector('.color-swatch.active'),
                change = new Event('change', {bubbles: true, cancelable: false});
            if (activeSwatch) {
              activeSwatch.className = activeSwatch.className.replace(' active', '');
            }
            colorOptionElm.selected = "selected";
            colorSpanElm.className += ' active';
            colorOptionElm.parentElement.dispatchEvent(change);
          }

        </script>
      </div>
    </form>
    {% endif %}


    <div class="bottom_btn">
      {% comment %}
      <a class="" href="{{ product.url | within: collection }}"></a>
      {% endcomment %}
      <form method="post" action="/cart/add" class="{% if section.settings.show_variant == blank %}add_btn{% endif %}" >
        <input type="hidden" name="id" value="{{ product.variants.first.id }}" />
        <input type="hidden" min="1" type="number" id="quantity" name="quantity" value="1"/>
        <button type="submit" class="btn buttonAddtoCart">Adicionar ao carrinho</button>
      </form>
    </div>
  </div>


</div>


