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

<div id="basket_totals" class="basket-totals">
    <table class="table table-smaller table-borderless totals-table">
        <tbody>
            {% with offer_discounts=basket.offer_discounts voucher_discounts=basket.grouped_voucher_discounts %}
            {% block discount_totals %}
            {% if offer_discounts or voucher_discounts %}
            <tr>
                {% if offer_discounts or voucher_discounts %}
                <td>{% trans "Basket total (before discounts)" %}</td>
                {% else %}
                <td>{% trans "Basket total" %}</td>
                {% endif %}
                <td>
                    {% if basket.is_tax_known and not show_tax_separately %}
                    {{ basket.total_incl_tax_excl_discounts|currency:basket.currency }}
                    {% else %}
                    {{ basket.total_excl_tax_excl_discounts|currency:basket.currency }}
                    {% endif %}
                </td>
            </tr>

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

            {% if voucher_discounts %}
            {% for discount in voucher_discounts %}
            <tr>
                <td>
                    {% if editable %}
                    <form class="removevoucher" action="{% url 'basket:vouchers-remove' pk=discount.voucher.id %}" method="POST">
                        {% csrf_token %}
                        <label for="removevoucher"><i class="fa fa-trash text-danger"></i></label>
                        <input type="submit" id="removevoucher" name="removevoucher" value="{% trans 'Remove' %}" class="remove-voucher" >
                    </form>
                    {% endif %}
                    {% if discount.voucher.voucher_set %}
                    {{ discount.voucher.voucher_set.name }} ({{ discount.voucher.code }})
                    {% else %}
                    {{ discount.voucher.name }} ({{ discount.voucher.code }})
                    {% endif %}
                </td>
                <td>- {{ discount.discount|currency:basket.currency }}</td>
            </tr>
            {% endfor %}
            {% endif %}
            {% endif %}
            {% endblock %}

            {% block basket_total %}
            {% endblock %}
            {% endwith %}

            {% block shipping_totals %}
            {% if shipping_methods|length > 1 and editable %}
            <tr>
                <td colspan="2">
                    <small class="shipping_alert" aria-live="polite">{% trans "Alternative shipping methods can be chosen during checkout" %}</small>
                </td>
            </tr>
            {% endif %}
            {% if not shipping_method.is_discounted %}
            <tr>
                <td class="total">
                    {{ shipping_method.name }}
                </td>
                <td class="total">
                    {{ shipping_charge.incl_tax|currency:basket.currency }}
                </td>
            </tr>
            {% else %}
            {% shipping_charge_discount shipping_method basket as shipping_discount %}
            {% shipping_charge_excl_discount shipping_method basket as shipping_charge_excl_discount %}
            <tr>
                <td class="total">{{ shipping_method.name }}</td>
                <td class="total">
                    {% if not show_tax_separately and shipping_charge_excl_discount.is_tax_known %}
                    {{ shipping_charge_excl_discount.incl_tax|currency:basket.currency }}
                    {% else %}
                    {{ shipping_charge_excl_discount.excl_tax|currency:basket.currency }}
                    {% endif %}
                </td>
            </tr>
            <tr>
                <td class="basket-discount"><span class="label label-success">{% trans "Discount" %}</span> {{ shipping_method.discount_name }}</td>
                <td>- {{ shipping_discount|currency:basket.currency }}</td>
            </tr>
            {% endif %}
            {% endblock %}

            {% block surcharges %}
            {% if surcharges %}
            {% for charge in surcharges %}
            <tr>
                <td>{{ charge.surcharge.name }}</td>
                <td>
                    {% if not show_tax_separately and shipping_charge.is_tax_known %}
                    {{ charge.price.incl_tax|currency:basket.currency }}
                    {% else %}
                    {{ charge.price.excl_tax|currency:basket.currency }}
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
            {% endif %}
            {% endblock %}

            {% block tax_totals %}
            {% if show_tax_separately %}
            <tr>
                <td colspan="2">
                    <h3>{% trans "Tax" %}</h3>
                </td>
            </tr>
            <tr>
                <td class="total">{% trans "Basket" %}</td>
                <td class="total">
                    {{ basket.total_tax|currency:basket.currency }}
                </td>
            </tr>
            <tr>
                <td class="total">{% trans "Shipping" %}</td>
                <td class="total">
                    {{ shipping_charge.tax|currency:basket.currency }}
                </td>
            </tr>
            {% endif %}
            {% endblock %}

            {% block post_order_action_totals %}
            {% if basket.post_order_actions %}
            <tr>
                <td><h3>{% trans "Post order actions" %}</h3><small>{% trans "These will be applied once your order is placed." %}</small></td>
                <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 %}
            {% endblock %}

            {% block order_total %}
            <tr class="table-border"><td colspan="2"></td></tr>
            <tr>
                <td class="total">
                    {% trans 'Subtotal' %}
                </td>
                <td class="total">
                      {{ order_total.incl_tax|currency:basket.currency }}
                </td>
            </tr>
            {% if not order_total.is_tax_known %}
            <tr>
                <td><small>{% trans "Taxes will be added during checkout." %}</small></td>
            </tr>
            {% endif %}
            {% endblock %}
        </tbody>
    </table>
    {% block order_summation_table %}
    <table class="table table-smaller table-borderless totals-table tax-table">
        {% block order_summation %}
        <tbody>
            {% if order_total.is_tax_known %}
            <tr>
                <td class="total">
                    {% trans 'Tax' %}
                </td>
                <td class="total">
                    {{ order_total.tax|currency:basket.currency }}
                </td>
            </tr>
            <tr>
                <td class="total">
                    {% trans 'Total excl. tax' %}
                </td>
                <td class="total">
                    {{ order_total.excl_tax|currency:basket.currency }}
                </td>
            </tr>
            {% endif %}
            <tr class="table-border"><td colspan="2"></td></tr>
            <tr>
                <th class="total">
                    {% trans 'Amount to pay' %}
                </th>
                <th class="total incl-tax">
                    {{ order_total.incl_tax|currency:basket.currency }}
                </th>
            </tr>
            {% if not order_total.is_tax_known %}
            <tr>
                <td><small>{% trans "Taxes will be added during checkout." %}</small></td>
            </tr>
            {% endif %}
        </tbody>
        {% endblock %}
    </table>
    {% endblock %}
</div>