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

{% set itemsSubtotal = sylius_order_items_subtotal(order) %}
{% set taxIncluded = sylius_order_tax_included(order) %}
{% set taxExcluded = sylius_order_tax_excluded(order) %}

<div class="cart-totals checkout-summary card">
    <p class="card-header">{{ 'sylius.ui.summary'|trans }}</p>
    <div class="cart-totals-body">
        <table class="table" id="sylius-checkout-subtotal" {{ sylius_test_html_attribute('checkout-subtotal') }}>
            <thead>
                <tr>
                    <th class="sylius-table-column-item">{{ 'sylius.ui.item'|trans }}</th>
                    <th class="sylius-table-column-qty text-center">{{ 'sylius.ui.qty'|trans }}</th>
                    <th class="sylius-table-column-subtotal text-right">{{ 'sylius.ui.subtotal'|trans }}</th>
                </tr>
            </thead>
            <tbody>
                <td class="cart-detail" colspan="3">
                    <div data-simplebar data-simplebar-auto-hide="false">
                        <table>
                            {% for item in order.items %}
                                <tr>
                                    <td class="sylius-table-column-item">{{ item.getVariant.product.name }}</td>

                                    <td class="text-center sylius-table-column-qty">
                                        {{ item.quantity }}
                                    </td>

                                    <td class="price-sm text-right sylius-table-column-subtotal" id="sylius-item-{{ item.variant.product.slug }}-subtotal" {{ sylius_test_html_attribute('item-subtotal', item.variant.product.slug) }}>
                                        {{ money.convertAndFormat(item.subtotal) }}
                                    </td>
                                </tr>
                            {% endfor %}
                        </table>
                    </div>
                </td>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="1">
                        {{ 'sylius.ui.items_total'|trans }}
                    </td>

                    <td colspan="2" id="sylius-summary-items-subtotal" class="price-sm text-right">
                        {{ money.convertAndFormat(itemsSubtotal) }}
                    </td>
                </tr>

                <tr>
                    <td colspan="1">
                        {{ 'sylius.ui.discount'|trans }}
                    </td>

                    <td colspan="2" id="sylius-summary-promotion-total" class="price-sm text-right">
                        {{ money.convertAndFormat(order.orderPromotionTotal) }}
                    </td>
                </tr>

                {% if order.shipments is not empty %}
                    <tr>
                        <td>
                            {{ 'sylius.ui.shipping_estimated_cost'|trans }}
                        </td>
                        <td colspan="2" class="price-sm text-right">
                            <div class="price-wrapper-vertical">
                                {% if order.getAdjustmentsTotal('shipping') > order.shippingTotal %}
                                    <div class="price-sm-old">{{ money.convertAndFormat(order.getAdjustmentsTotal('shipping')) }}</div>
                                {% endif %}
                                <span id="sylius-summary-shipping-total" class="price-sm {% if order.getAdjustmentsTotal('shipping') > order.shippingTotal %}promo{% endif %}">{{ money.convertAndFormat(order.shippingTotal) }}</span>
                            </div>
                        </td>
                    </tr>
                {% endif %}

                <tr class="cart-totals-tr-total">
                    <td colspan="1">
                        <strong class="h4">{{ 'sylius.ui.order_total'|trans }}</strong>
                    </td>
                    <td colspan="2" id="sylius-summary-grand-total" class="price-md text-right">
                        {{ money.convertAndFormat(order.total) }}
                    </td>
                </tr>

                <tr class="cart-totals-tr-tax {% if taxIncluded and not taxExcluded %}tax-disabled{% endif %}">
                    <td colspan="1">
                        {{ 'sylius.ui.taxes_total'|trans }}
                        {% if taxIncluded %}
                            <small>({{ 'sylius.ui.included_in_price'|trans }})</small>
                        {% endif %}
                    </td>

                    <td colspan="2" class="price-sm text-right">
                        {% if not taxIncluded and not taxExcluded %}
                            <div id="sylius-summary-tax-none">{{ money.convertAndFormat(0) }}</div>
                        {% endif %}
                        {% if taxExcluded %}
                            <div id="sylius-summary-tax-excluded">{{ money.convertAndFormat(taxExcluded) }}</div>
                        {% endif %}
                        {% if taxIncluded %}
                            <div class="tax-disabled">
                                <span id="sylius-summary-tax-included">{{ money.convertAndFormat(taxIncluded) }}</span>
                            </div>
                        {% endif %}
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>

{% if order.checkoutState != 'cart' %}
    <div class="cart-totals address-summary card mb-0">
        <p class="card-header">{{ 'sylius.ui.billing_address'|trans }}</p>
        <div class="card-body">
            {% include '@SyliusShop/Common/_address.html.twig' with {'address': order.billingAddress} %}
        </div>
    </div>

    <div class="cart-totals address-summary card">
        <p class="card-header">{{ 'sylius.ui.shipping_address'|trans }}</p>
        <div class="card-body">
            {% include '@SyliusShop/Common/_address.html.twig' with {'address': order.shippingAddress} %}
        </div>
    </div>
{% endif %}

{% if order.checkoutState not in ['cart', 'addressed'] %}
    {% set shipment = order.shipments|first %}
    {% if shipment %}
        <div class="cart-totals address-summary card">
            <p class="card-header">{{ 'sylius.ui.shipping_method'|trans }}</p>
            <div class="card-body">
                <p>{{ shipment.method }}</p>
            </div>
        </div>
    {% endif %}
{% endif %}
