{% extends "oscar/customer/baseaccountpage.html" %}
{% load currency_filters %}
{% load display_tags %}
{% load i18n %}
{% load reviews_tags %}

{% block extra_breadcrumbs %}
    <li class="breadcrumb-item">
        <a href="{% url 'customer:order-list' %}">{% trans 'Order history' %}</a>
    </li>
{% endblock %}

{% block tabcontent %}
    {% if order.status %}
    <h2>{% trans 'Status' %}</h2>
    <p>{{ order.status }}</p>
    <hr>
    {% endif %}

    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>{% trans 'Product' %}</th>
                <th>{% trans 'Qty' %}</th>
                <th>{% trans 'Line price excl. tax' %}</th>
                <th>{% trans 'Line price incl. tax' %}</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            {% for line in order.lines.all %}
                {% with product=line.product %}
                    <tr>
                        <td>
                            {% if product %}

                                {% if product.is_public %}
                                <p>
                                    <a href="{{ product.get_absolute_url }}">{{ line.description }}</a>
                                </p>
                                {% else %}
                                <p>{{ line.description }}</p>
                                {% endif %}

                                {% iffeature "reviews" %}
                                    {% if product|is_review_permitted:user %}
                                        <a class="btn btn-secondary" href="{% url 'catalogue:reviews-add' product_slug=product.slug product_pk=product.id %}#addreview">{% trans 'Write a review' %}</a>
                                    {% endif %}
                                {% endiffeature %}
                            {% else %}
                                <p>
                                    {{ line.description }}
                                </p>
                            {% endif %}
                        </td>
                        <td>{{ line.quantity }}</td>
                        <td>{{ line.line_price_before_discounts_excl_tax|currency:order.currency }}</td>
                        <td>{{ line.line_price_before_discounts_incl_tax|currency:order.currency }}</td>
                        <td width="90">
                            {% if product and product.is_public %}
                                <form id="line_form_{{ line.id }}" action="{% url 'customer:order-line' order_number=order.number line_id=line.id %}" method="POST">
                                    {% csrf_token %}
                                    <input type="hidden" name="action" value="reorder" />
                                    <button id="reorder_line_{{ line.id }}" class="btn btn-success" type="submit" data-loading-text="{% trans 'Re-ordering...' context 'Buy again' %}">{% trans 'Re-order' context 'Buy again' %}</button>
                                </form>
                            {% else %}
                                {% trans 'Not available anymore' %}
                            {% endif %}
                        </td>
                    </tr>
                {% endwith %}
            {% endfor %}

            {% with discounts=order.basket_discounts %}
                {% block discount_totals %}
                    {% if discounts %}
                        <tr>
                            <td colspan="4">{% trans "Basket total (excl. discounts)" %}</td>
                            <td colspan="2">{{ order.basket_total_before_discounts_incl_tax|currency:order.currency }}</td>
                        </tr>
                        {% for discount in discounts %}
                        <tr>
                            <td colspan="4">
                                <span class="badge badge-success">{% trans "Discount" %}</span>
                                {{ discount.offer_name }}
                            </td>
                            <td colspan="2">- {{ discount.amount|currency:order.currency }}</td>
                        </tr>
                        {% endfor %}
                    {% endif %}
                {% endblock discount_totals %}

                {% block basket_total %}
                    {% if discounts %}
                        <tr>
                            <th colspan="4">{% trans "Basket total (inc. discounts)" %}</th>
                            <td colspan="2">{{ order.basket_total_incl_tax|currency:order.currency }}</td>
                        </tr>
                    {% else %}
                        <tr>
                            <th colspan="4">{% trans "Basket total" %}</th>
                            <td colspan="2">{{ order.basket_total_incl_tax|currency:order.currency }}</td>
                        </tr>
                    {% endif %}
                {% endblock basket_total %}
            {% endwith %}

            {% block shipping_totals %}
                {% if order.has_shipping_discounts %}
                    <tr>
                        <td colspan="4">{% trans "Shipping total (excl. discounts)" %}</td>
                        <td colspan="2">{{ order.shipping_before_discounts_incl_tax|currency:order.currency }}</td>
                    </tr>
                    {% for discount in order.shipping_discounts %}
                    <tr>
                        <td colspan="4">
                            <span class="badge badge-success">{% trans "Discount" %}</span>
                            {{ discount.offer_name }}
                        </td>
                        <td colspan="2">- {{ discount.amount|currency:order.currency }}</td>
                    </tr>
                    {% endfor %}
                    <tr>
                        <th colspan="4">{% trans "Shipping total (inc. discounts)" %}</th>
                        <td colspan="2">{{ order.shipping_incl_tax|currency:order.currency }}</td>
                    </tr>
                {% else %}
                    <tr>
                        <th colspan="4">{% trans "Shipping total" %}</th>
                        <td colspan="2">{{ order.shipping_incl_tax|currency:order.currency }}</td>
                    </tr>
                {% endif %}
            {% endblock %}

            {% block order_total %}
                <tr>
                    <th colspan="4">{% trans 'Order total' %}</th>
                    <td colspan="2">{{ order.total_incl_tax|currency:order.currency }}</td>
                </tr>
            {% endblock order_total %}
        </tbody>
    </table>

    {% block order_actions %}
        <form id="order_form_{{ order.id }}" action="{% url 'customer:order' order_number=order.number %}" method="post">
            {% csrf_token %}
            <input type="hidden" name="order_id" value="{{ order.id }}" />
            <input type="hidden" name="action" value="reorder" />
            <button type="submit" class="btn btn-success" data-loading-text="{% trans 'Re-ordering...' context 'Buy again' %}">{% trans 'Re-order all items' context 'Buy again' %}</button>
        </form>
    {% endblock order_actions %}

    <hr>

    <h2>{% trans 'Shipping Method' %}</h2>
    <p>{{ order.shipping_method }}</p>

    <hr>

    <h2>{% trans 'Shipping Address' %}</h2>

    <table class="table table-striped table-bordered">
        <tr>
            <th>{% trans 'Address' %}</th>
            <th>{% trans 'Contact Number' %}</th>
            <th>{% trans 'Shipping Notes' %}</th>
        </tr>
        <tr>
            <td>
                    {% for field in order.shipping_address.active_address_fields %}
                        {{ field }}<br/>
                    {% endfor %}
            </td>
            <td>{{ order.shipping_address.phone_number|default:"-" }}</td>
            <td>{{ order.shipping_address.notes|linebreaks }}</td>
        </tr>
    </table>
{% endblock tabcontent %}
