{% load i18n %}
{% load currency_filters %}

<div id="basket_totals">
    <table class="table table-condensed">
        <tbody>
        <tr>
            <th colspan="2"><h4>{% trans "Basket" %}</h4></th>
        </tr>
        {% with offer_discounts=basket.offer_discounts voucher_discounts=basket.grouped_voucher_discounts %}
        {% if offer_discounts or voucher_discounts %}
            {# Basket total will be discounted so we show a before and after version #}
            <tr>
                <td>{% trans "Basket total (before discounts)" %}</td>
                <td class="align-right">{{ basket.total_incl_tax_excl_discounts|currency }}</td>
            </tr>

            {% if offer_discounts %}
                {% for discount in offer_discounts %}
                <tr>
                    <td class="basket-discount"><span class="label label-success">{% trans "Discount" %}</span> {{ discount.name }}
                        {% if discount.description %}
                        <br/><small>{{ discount.description }}</small>
                        {% endif %}
                    </td>
                    <td class="align-right">-{{ discount.discount|currency }}</td>
                </tr>
                {% endfor %}
            {% endif %}

            {% if voucher_discounts %}
                <tr>
                    <th colspan="2"><h4>{% trans "Vouchers" %}</h4></th>
                </tr>
                {% for discount in voucher_discounts %}
                <tr>
                    <td>
                        {{ discount.voucher.name }} ({{ discount.voucher.code }})
                        {% if editable %}
                        <form action="{% url basket:vouchers-remove discount.voucher.id %}" method="POST">
                            {% csrf_token %}
                            <input type="submit" value="{% trans "Remove" %}" class="btn btn-small btn-danger"/>
                        </form>
                        {% endif %}
                    </td>
                    <td>-{{ discount.discount|currency }}</td>
                </tr>
                {% endfor %}
            {% endif %}

            <tr>
                <th class="total">{% trans "Basket total (after discounts)" %}</th>
                <th class="total align-right">{{ basket.total_incl_tax|currency }}</th>
            </tr>
        {% else %}
            {# No discounts to basket #}
            <tr>
                <th class="total">{% trans "Basket total" %}</th>
                <th class="total align-right">{{ basket.total_incl_tax|currency }}</th>
            </tr>
        {% endif %}
        {% endwith %}

        <tr>
            <th>&nbsp;</th>
            <td></td>
        </tr>

        <tr>
            <th colspan="2">
                <h4>{% trans "Shipping" %}</h4>
                {% if shipping_methods|length > 1 and editable %}
                <small>{% trans "Alternative shipping methods can be chosen during checkout" %}</small>
                {% endif %}
            </th>
        </tr>
        {% if not shipping_method.is_discounted %}
            <tr>
                <th class="total">
                    {% trans "Shipping" %} - {{ shipping_method.name }}
                </th>
                <th class="total align-right">{{ shipping_method.basket_charge_incl_tax|currency }}</th>
            </tr>
        {% else %}
            {# As shipping is discounted, we break it down into its original charge and a discount #}
            <tr>
                <th class="total">{% trans "Shipping method" %}</th>
                <td class="total align-right">{{ shipping_method.name }}</td>
            </tr>
            <tr>
                <td>{% trans "Shipping total (before discounts)" %}</td>
                <td class="align-right">{{ shipping_method.basket_charge_incl_tax_before_discount|currency }}</td>
            </tr>
            <tr>
                {% with discount=shipping_method.get_discount %}
                <td class="basket-discount"><span class="label label-success">{% trans "Discount" %}</span> {{ discount.name }}</td>
                <td class="align-right">-{{ discount.discount|currency }}</td>
                {% endwith %}
            </tr>
            <tr>
                <th class="total">
                    {% trans "Shipping total (after discounts)" %}
                </th>
                <th class="total align-right">{{ shipping_method.basket_charge_incl_tax|currency }}</th>
            </tr>
        {% endif %}

        {% if basket.post_order_actions %}
            <tr>
                <th>&nbsp;</th>
                <td></td>
            </tr>
            <tr>
                <th><h4>{% trans "Post order actions" %}</h4><small>{% trans "These will be applied once your order is placed." %}</small></th>
                <td></td>
            </tr>
            {% for discount in basket.post_order_actions %}
            <tr>
                <td class="total" colspan="2">
                    <span class="label label-success">{{ discount.name }}</span><br/>
                    <p>{{ discount.description }}</p>
                </td>
            </tr>
            {% endfor %}
        {% endif %}

        <tr>
            <th>&nbsp;</th>
            <td></td>
        </tr>
        <tr>
            <th class="total"><h4>{% trans "Order total" %}</h4></th>
            <td class="total align-right"><h4 class="price_color">{{ order_total_incl_tax|currency }}</h4></td>
        </tr>
        </tbody>
    </table>
</div>
