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

{% block page_name %}Reconcile Bank Statements{% endblock %}
{% block page_description %}What did you spend money on?{% endblock %}

{% block content %}
    <table class="table table-striped">
        {% block table_header %}
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Amount</th>
                    <th>Description</th>
                    <th>Account</th>
                </tr>
            </thead>
        {% endblock table_header %}
        <tbody>
            {% for line in statement_lines %}
                {% block reconcile_row %}
                    <tr id="L{{ line.uuid }}">
                        <td>{{ line.date }}</td>
                        <td>{{ line.amount|color_currency }}</td>
                        <td>{{ line.description }}</td>
                        <td>
                            {% if line.transaction %}
                                <td>
                                    <dl>
                                    {% for leg in line.transaction.legs.all %}
                                        {% if leg.account != line.statement_import.bank_account %}
                                            <dt>{{ leg.account.name }}</dt>
                                            <dd>{{ leg.amount|abs }}</dd>
                                        {% endif %}
                                    {% endfor %}
                                    </dl>
                                </td>
                                <td>
                                <form action="{% url 'hordak:transactions_unreconcile' line.uuid %}" method="post">
                                    {% csrf_token %}
                                    <input type="submit" value="Unreconcile" class="btn btn-default btn-xs">
                                </form>
                                </td>
                            {% elif line != reconcile_line %}
                                <td></td>
                                <td>
                                <form action="#L{{ line.uuid }}" method="get">
                                    <input type="hidden" name="reconcile" value="{{ line.uuid }}">
                                    <input type="hidden" name="page" value="{{ page_obj.number }}">
                                    <input type="submit" value="Reconcile" class="btn btn-primary btn-xs">
                                </form>
                                </td>
                            {% endif %}
                        </td>
                    </tr>
                {% endblock reconcile_row %}
                {% if line == reconcile_line %}
                    {% block reconcile_action_row %}
                        <tr>
                            <td colspan="4">
                                {% if leg_formset.non_form_errors %}
                                    <ul>
                                        {% for error in leg_formset.non_form_errors %}
                                        <li class="error">{{ error }}</li>
                                        {% endfor %}
                                    </ul>
                                {% endif %}

                                <form action="" method="post">
                                    {% csrf_token %}
                                    <input type="hidden" name="reconcile" value="{{ line.uuid }}">
                                    <input type="hidden" name="page" value="{{ page_obj.number }}">
                                    {% block reconcile_form_content %}
                                        <table>
                                            {% block reconcile_form_content_transaction_form %}
                                                {{ transaction_form }}
                                            {% endblock %}
                                        </table>
                                        {{ leg_formset.management_form }}
                                        <table class="table table-striped">
                                            {% for form in leg_formset %}
                                                {% block leg_formset %}
                                                    <tr>
                                                        <td>{{ form.amount }}</td>
                                                        <td>{{ form.description }}</td>
                                                        <td>{{ form.account }}</td>
                                                    </tr>
                                                {% endblock %}
                                            {% endfor %}
                                        </table>

                                        <input type="submit" value="Reconcile" class="btn btn-success pull-right">
                                    {% endblock reconcile_form_content %}
                                </form>
                            </td>
                        </tr>
                    {% endblock reconcile_action_row %}
                {% endif %}
            {% empty %}
                <tr>
                    <td colspan="4">
                        {% block nothing_to_reconcile %}Nothing to reconcile, you are up to date{% endblock %}
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

    {% block pagination %}
        <div class="pagination">
            <span class="step-links">
                {% if page_obj.has_previous %}
                    <a href="?page={{ page_obj.previous_page_number }}">previous</a>
                {% endif %}

                <span class="current">
                    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                </span>

                {% if page_obj.has_next %}
                    <a href="?page={{ page_obj.next_page_number }}">next</a>
                {% endif %}
            </span>
        </div>
    {% endblock %}

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

{% endblock %}
