{% 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="checkout-summary card">
    <div class="card-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">{{ 'sylius.ui.qty'|trans }}</th>
                    <th class="sylius-table-column-subtotal">{{ 'sylius.ui.subtotal'|trans }}</th>
                </tr>
            </thead>
            <tbody>
                {% for item in order.items %}
                    <tr>
                        <td>{{ item.getVariant.product.name }}</td>

                        <td class="text-center">
                            {{ item.quantity }}
                        </td>

                        <td class="text-right" 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 %}
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="1">
                        <strong>{{ 'sylius.ui.items_total'|trans }}:</strong>
                    </td>

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

                <tr {% if taxIncluded and not taxExcluded %}class="tax-disabled" {% endif %}>
                    <td colspan="1">
                        <strong>{{ 'sylius.ui.taxes_total'|trans }}:</strong>
                    </td>

                    <td colspan="2" class="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><small>({{ 'sylius.ui.included_in_price'|trans }})</small></div>
                            </div>
                        {% endif %}
                    </td>
                </tr>

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

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

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

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

{% if order.checkoutState != 'cart' %}
    <div class="checkout-summary card">
        <div class="card-body">
            <h4>{{ 'sylius.ui.billing_address'|trans }}</h4>

            {% include '@SyliusShop/Common/_address.html.twig' with {'address': order.billingAddress} %}

            <hr>

            <h4>{{ 'sylius.ui.shipping_address'|trans }}</h4>

            {% 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="checkout-summary card">
        <div class="card-body">
                <h4>{{ 'sylius.ui.shipping_method'|trans }}</h4>

                <p>{{ shipment.method }}</p>
            </div>
        </div>
    {% endif %}
{% endif %}
