{% import "@SyliusShop/Common/Macro/money.html.twig" as money %}

{% set url = path(search_result_url_param(result).path, search_result_url_param(result).params) %}
{% set name = result.attribute('name').value | join %}
{% set brand = result.attribute('BRAND') %}
{% set reviews = result.attribute('reviews') %}
{% set price = result.priceByChannelAndCurrency(channel.code, currencyCode) %}
{% set originalPrice = result.originalPriceByChannelAndCurrency(channel.code, currencyCode) %}
{% set hasDiscount = originalPrice is not null and price!=originalPrice %}

<div class="product-box-container" {{ sylius_test_html_attribute('product') }}>
    {% if hasDiscount %}
        <div class="product-box-tag">
            <span class="badge badge-secondary">{{ 'app.ui.product.tag.promo'|trans }}</span>
        </div>
    {% endif %}

    <a href="{{ url }}" class="product-box-image-wrapper">
        {% if result.image is not empty %}
            {% set path = result.image|imagine_filter(filter|default('sylius_shop_product_thumbnail')) %}
        {% else %}
            {% set path = '//placehold.it/200x200' %}
        {% endif %}

        <img src="{{ path }}" {{ sylius_test_html_attribute('main-image') }} alt="{{ name }}" class="w-100" />
    </a>

    <div class="product-box-text-wrapper">

        <a href="{{ url }}" class="product-box-name" {{ sylius_test_html_attribute('product-name', name) }}>
            <h2 class="product-box-title">{{ name }}</h2>

            {% if brand is not null %}
                <h3 class="product-box-brand">{{ brand.value[0] }}</h3>
            {% endif %}
        </a>

        {% if reviews is not null %}
            <a href="{{ url }}#product-reviews" class="product-box-review review-rating">
                <div id="average-rating" class="ui star large rating" data-rating="{{ reviews.value[1]|round }}" data-max-rating="5" data-average-rating="{{ reviews.value[1]|round(2) }}" style="pointer-events: none;" {{ sylius_test_html_attribute('average-rating', reviews.value[1]|round(2)) }}></div>
                <span class="product-box-review-nb">({{ reviews.value[0] }})</span>
            </a>
        {% endif %}

        {% if result.price is not empty %}
            <div class="product-box-price-wrapper" {{ sylius_test_html_attribute('product-price') }}>
                {% if not hasDiscount %}
                    <span class="product-price">{{ money.format(price.value, currencyCode) }}</span>
                {% else %}
                    <span class="product-price promo">{{ money.format(price.value, currencyCode) }}</span>
                    <span class="product-price-old">{{ money.format(originalPrice.value, currencyCode) }}</span>
                {% endif %}
            </div>
        {% endif %}
    </div>
</div>
