{% extends 'hordak/base.html' %}
{% load hordak %}

{% block page_name %}Account {{ account.name }}{% endblock %}
{% block page_description %}See all transactions for an account{% endblock %}

{% block content %}
    <h5>Balance: {{ account.balance }}</h5>

    <table class="table table-striped">
        <thead>
            <tr>
                <th>Date</th>
                <th>Account</th>
                <th>Description</th>
                <th>Debit</th>
                <th>Credit</th>
                <th>Balance</th>
            </tr>
        </thead>
        <tbody>
            {% for leg in legs %}
                <tr>
                    <td>{{ leg.transaction.date }}</td>
                    <td>
                        {% if leg.is_debit %}
                            {% for debit_leg in leg.transaction.legs.debits %}
                                {{ debit_leg.account.name }}{% if not forloop.last %},{% endif %}
                            {% endfor %}
                        {% else %}
                            {% for credit_leg in leg.transaction.legs.credits %}
                                {{ credit_leg.account.name }}{% if not forloop.last %},{% endif %}
                            {% endfor %}
                        {% endif %}
                    </td>
                    <td>{{ leg.transaction.description }}</td>
                    <td>{% if leg.is_debit %}{{ leg.amount|abs }}{% endif %}</td>
                    <td>{% if leg.is_credit %}{{ leg.amount|abs }}{% endif %}</td>
                    <td>{{ leg.account_balance_after }}</td>
                    <td>
                        <a href="{% url 'hordak:transactions_delete' leg.transaction.uuid %}" class="btn btn-xs btn-danger">Delete</a>
                    </td>
                </tr>

                {# Show the entering balance at the bottom of the list #}
                {% if forloop.last %}
                    <tr>
                        <td colspan="5"></td>
                        <td>{{ leg.account_balance_before }}</td>
                    </tr>
                {% endif %}
            {% empty %}
                <tr>
                <td colspan="6" class="text-center">No transactions exist</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

    <p>
        <a href="{% url 'hordak:accounts_list' %}">Back</a>
    </p>
{% endblock %}
