{% extends 'dashboard/layout.html' %}
{% load compress %}
{% load static %}
{% load currency_filters %}
{% load i18n %}

{% block body_class %}{{ block.super }} refunds{% endblock %}

{% block title %}
    {% blocktrans with id=refund.id %}Refund #{{ id }}{% endblocktrans %} | {{ block.super }}
{% endblock title %}

{% block extrascripts %}
    {{ block.super }}

    {# Translation support for JavaScript strings. #}
    <script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>

    {% compress js %}
    <script src="{% static 'bower_components/js-cookie/src/js.cookie.js' %}" type="text/javascript"></script>
    <script src="{% static 'vendor-extensions/oscar/js/add_message.js' %}" type="text/javascript"></script>
    <script src="{% static 'vendor-extensions/oscar/js/refund_list.js' %}" type="text/javascript"></script>
    {% endcompress %}
{% endblock extrascripts %}

{% block breadcrumbs %}
<ul class="breadcrumb">
    <li>
        <a href="{% url 'dashboard:index' %}">{% trans "Dashboard" %}</a>
    </li>
    <li>
        <a href="{% url 'dashboard:refunds:list' %}">{% trans "Refunds" %}</a>
    </li>
    <li class="active">#{{ refund.id }}</li>
</ul>
{% endblock breadcrumbs %}

{% block headertext %}
    {% blocktrans with id=refund.id %}Refund #{{ id }}{% endblocktrans %}
{% endblock headertext %}

{% block dashboard_content %}
    {% block customer_information %}
    <table class="table table-bordered table-hover">
        <caption><i class="icon-group icon-large"></i>{% trans "Customer Information" %}</caption>
        {% if refund.user %}
            <tr>
                <th>{% trans "Username" %}</th>
                <th>{% trans "Full name" %}</th>
                <th>{% trans "Email address" %}</th>
            </tr>
            <tr>
                <td><a href="{% url 'dashboard:user-detail' pk=refund.user.id %}">{{ refund.user.username }}</a></td>
                <td>{{ refund.user.get_full_name }}</td>
                <td>{{ refund.user.email }}</td>
            </tr>
        {% else %}
            <tr>
                <td>{% trans "Customer has deleted their account." %}</td>
            </tr>
        {% endif %}
    </table>
    {% endblock customer_information %}

    {% block refund_overview %}
    <table class="table table-striped table-bordered table-hover">
        <caption><i class="icon-shopping-cart icon-large"></i>{% trans "Refund Overview" %}</caption>
        <tr>
            <th>{% trans "Total Credit" %}</th>
            <th>{% trans "Created" %}</th>
            <th>{% trans "Status" %}</th>
            <th>{% trans "Associated Order" %}</th>
            <th>{% trans "Actions" %}</th>
        </tr>
        <tr data-refund-id="{{ refund.id }}">
            <td>{{ refund.total_credit_excl_tax|currency:refund.currency }}</td>
            <td>{{ refund.created }}</td>
            <td class="refund-status">{{ refund.status }}</td>
            <td>
                <a href="{% url 'dashboard:order-detail' number=refund.order.number %}">{{ refund.order.number }}</a>
            </td>
            <td>
                {% include "dashboard/partials/refund_buttons.html" %}
            </td>
        </tr>
    </table>
    {% endblock refund_overview %}

    {% block refund_details %}
    {% include "dashboard/partials/refund_action_modal.html" %}

    <div class="table-header">
        <h3>{% trans "Refund Items" %}</h3>
    </div>

    <form id="refund_lines_form" action="." method="post" class="form-inline">
        {% csrf_token %}
        {% block refund_lines %}
        <table class="table table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th>{% trans "Refund Line ID" %}</th>
                    <th>{% trans "Associated Order Line" %}</th>
                    <th>{% trans "Quantity" %}</th>
                    <th>{% trans "Product" %}</th>
                    <th>{% trans "UPC" %}</th>
                    <th>{% trans "Status" %}</th>
                    <th>{% trans "Supplier" %}</th>
                    <th>{% trans "Supplier SKU" %}</th>
                    <th>{% trans "Credit (excl. tax)" %}</th>
                </tr>
            </thead>
            <tbody>
                {% for line in refund.lines.all %}
                <tr>
                    <td>{{ line.id }}</td>
                    <td>
                        <a href="{% url 'dashboard:order-line-detail' number=refund.order.number line_id=line.order_line.id %}">{{ line.order_line.id }}</a>
                    </td>
                    <td>{{ line.quantity }}</td>
                    <td>
                        {% if line.order_line.product %}
                        <a href="{% url 'dashboard:catalogue-product' pk=line.order_line.product.id %}">{{ line.order_line.title }}</a>
                        {% else %}
                        {{ line.order_line.title }}
                        {% endif %}
                    </td>
                    <td>{{ line.order_line.upc|default:"-" }}</td>
                    <td>{{ line.status }}</td>
                    <td>
                        {% if line.order_line.partner %}
                        <a href="{% url 'dashboard:partner-manage' pk=line.order_line.partner.id %}">{{ line.order_line.partner_name }}</a>
                        {% else %}
                        {{ line.order_line.partner_name }}
                        {% endif %}
                    </td>
                    <td>{{ line.order_line.partner_sku }}</td>
                    <td style="text-align: right">{{ line.line_credit_excl_tax|currency:refund.currency }}</td>
                </tr>
                {% endfor %}

                <tr>
                    <td colspan="7"></td>
                    <th>{% trans "Total Credit" %}</th>
                    <th style="text-align: right">{{ refund.total_credit_excl_tax|currency:refund.currency }}</th>
                </tr>
            </tbody>
        </table>
        {% endblock refund_lines %}
    </form>
    {% endblock refund_details %}

    {% block payment_events %}
    <div class="table-header">
        <h3>{% trans "Payment Events" %}</h3>
    </div>

    {% with events=refund.order.payment_events.all %}
    <table class="table table-striped table-bordered table-hover">
        {% if events %}
        <thead>
            <tr>
                <th>{% trans "Date" %}</th>
                <th>{% trans "Event" %}</th>
                <th>{% trans "Amount" %}</th>
                <th>{% trans "Lines" %}</th>
                <th>{% trans "Processor" %}</th>
                <th>{% trans "Reference" %}</th>
            </tr>
        </thead>
        <tbody>
            {% for event in events %}
            {% with line_qtys=event.line_quantities.all %}
            <tr>
                <td>{{ event.date_created }}</td>
                <td>{{ event.event_type.name }}</td>
                <td>{{ event.amount|currency:refund.order.currency }}</td>
                <td>
                    {% for line_qty in event.line_quantities.all %}
                    {% trans "Product:" %} {{ line_qty.line.title }}. {% trans "Quantity:" %} {{ line_qty.quantity }}.</br>
                    {% endfor %}
                </td>
                <td>{{ event.processor_name }}</td>
                <td>{{ event.reference|default:"-" }}</td>
            </tr>
            {% endwith %}
            {% endfor %}
        </tbody>
        {% else %}
        <tbody>
            <tr>
                <td>{% trans "No payment events." %}</td>
            </tr>
        </tbody>
        {% endif %}
    </table>
    {% endwith %}
    {% endblock payment_events %}
{% endblock dashboard_content %}
