{% 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 complementaryName = result.attribute('complementary_name') %}
{% set reviews = result.attribute('reviews') %}
{% set price = result.priceByChannelAndCurrency(channel.code, currencyCode) %}
{% set originalPrice = result.originalPriceByChannelAndCurrency(channel.code, currencyCode) %}

<div class="product-box-container" {{ sylius_test_html_attribute('product') }}>
    {% if originalPrice is not null %}
        <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 %}
        {% else %}
            {% set path = '//placehold.it/200x200' %}
        {% endif %}

        {{ dedi_sylius_image_optimizer_for_image(path, name|default(''), {
            '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 complementaryName is not null %}
                <p class="product-box-complementary-name">{{ complementaryName.value[0] }}</p>
            {% endif %}
        </a>

        {% if reviews is not null %}
            <a href="{{ url }}#product-reviews" class="product-box-review review-rating">
                <div 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 originalPrice is null %}
                    <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>
