{#
/**
 * Copyright © 2003-2024 The Galette Team
 *
 * This file is part of Galette (https://galette.eu).
 *
 * Galette is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Galette is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
 */
#}
{% extends 'elements/list.html.twig' %}

{% set basic_table = true %}

{% set form = {
    'route': {
        'name': 'payments_filter',
        'args': {
            'type': 'transactions'
        }
    },
    'order': {
        'name': "contributions",
        'args': {"type": "transactions"}
    }
} %}

{% block search %}
    <form action="{{ url_for("payments_filter", {"type": "transactions"}) }}" method="post" class="ui form filters">
        <div class="ui secondary yellow segment">
            <div class="two fields">
                <div class="two fields">
                    <div class="field">
                        <label for="start_date_filter">{{ _T("Show transactions since") }}</label>
                        <div class="ui calendar" id="contrib-rangestart">
                            <div class="ui input left icon">
                                <i class="calendar icon" aria-hidden="true"></i>
                                <input placeholder="{{ _T("yyyy-mm-dd format") }}" type="text" name="start_date_filter" id="start_date_filter" maxlength="10" size="10" value="{{ filters.start_date_filter }}"/>
                            </div>
                        </div>
                    </div>
                    <div class="field">
                        <label for="end_date_filter">{{ _T("until") }}</label>
                        <div class="ui calendar" id="contrib-rangeend">
                            <div class="ui input left icon">
                                <i class="calendar icon" aria-hidden="true"></i>
                                <input placeholder="{{ _T("yyyy-mm-dd format") }}" type="text" name="end_date_filter" id="end_date_filter" maxlength="10" size="10" value="{{ filters.end_date_filter }}"/>

                            </div>
                        </div>
                    </div>
                </div>
                <div class="ui right aligned basic fitted segment field flexend">
                    <button type="submit"  class="tooltip action ui labeled icon primary button" title="{{ _T("Apply filters") }}" name="filter">
                        <i class="search icon" aria-hidden="true"></i>
                        {{ _T("Filter") }}
                    </button>
                    <button type="submit" name="clear_filter" class="tooltip ui labeled icon button" title="{{ _T("Reset all filters to defaults") }}">
                        <i class="trash alt red icon" aria-hidden="true"></i>
                        {{ _T("Clear filter") }}
                    </button>
                </div>
            </div>
        </div>
        {% if member is defined %}
            {% if login.isAdmin() or login.isStaff() or member.canShow(login) %}
                <div class="ui compact vertically fitted segment">
                <div class="ui horizontal list">
                <span class="ui primary ribbon label">
                {% set member_logged_in_as = member.name ~ " " ~ member.surname ~ " (" ~ member.login ~ ")" %}
                {% if login.isAdmin() or login.isStaff() or member.hasChildren() or (member.hasParent() and (member_logged_in_as != login.loggedInAs(true))) %}
                    <a
                        href="{{ url_for("contributions", {"type": "transactions", "option": "member", "value": "all"}) }}"
                    >
                        <i class="icon times tooltip" aria-hidden="true"></i>
                        <span class="ui special popup">
                        {% if member.hasChildren() or (member.hasParent() and (member_logged_in_as != login.logged_in_as(true))) %}
                            {{ _T("Show all your transactions") }}
                        {% else %}
                            {{ _T("Show all members transactions") }}
                        {% endif %}
                        </span>
                    </a>
                {% endif %}
            {% endif %}
            {{ member.sname }}{% if not member.isActive() %} ({{ _T("Inactive") }}){% endif %}
            </span>
            {% if login.isAdmin() or login.isStaff() %}
                <div class="item">
                    <a href="{{ url_for("member", {"id": member.id}) }}" class="ui tiny button">{{ _T("See member profile") }}</a>
                </div>
                <div class="item">
                    <a href="{{ url_for("addTransaction") }}?id_adh={{ member.id }}" class="ui tiny button">{{ _T("Add a transaction") }}</a>
                </div>
            {% endif %}
            </div>
            </div>
        {% endif %}
        {% include "components/forms/csrf.html.twig" %}
    </form>
{% endblock %}

{% block infoline %}
    {% set infoline = {
        'label': _Tn("%count transaction", "%count transactions", nb)|replace({"%count": nb}),
        'route': {
            'name': 'payments_filter',
            'args': {
                'type': 'transactions'
            }
        }
    } %}
    {{ parent() }}
{% endblock %}

{% block infoline_actions %}
    {% if login.isAdmin() or login.isStaff() %}
        <a
                class="ui tiny labeled icon button"
                href="{{ url_for("addTransaction") }}"
        >
            <i class="ui plus circle green icon" aria-hidden="true"></i>
            {{ _T("Add a transaction") }}
        </a>
    {% endif %}
{% endblock %}

{% block header %}
    {% set columns = [
        {'label': '#', 'order': constant("Galette\\Filters\\TransactionsList::ORDERBY_ID")},
        {'label': _T("Date"), 'order': constant("Galette\\Filters\\TransactionsList::ORDERBY_DATE")}
    ] %}

    {% if ((login.isAdmin() or login.isStaff()) and member is not defined) or pmember is defined %}
        {% set columns = columns|merge([{'label': _T("Originator"), 'order': constant("Galette\\Filters\\TransactionsList::ORDERBY_MEMBER")}]) %}
    {% endif %}

    {% set columns = columns|merge([
        {'label': _T("Description")}
    ]) %}

    {% set columns = columns|merge([
        {'label': _T('Amount'), 'order': constant("Galette\\Filters\\TransactionsList::ORDERBY_AMOUNT")}
    ]) %}

    {% set columns = columns|merge([
        {'label': _T('Payment type'), 'order': constant("Galette\\Filters\\TransactionsList::ORDERBY_PAYMENT_TYPE")}
    ]) %}

    {{ parent() }}
{% endblock %}

{% block body %}
    {% for ordre, transaction in list %}
        {% set mid = transaction.member %}
        {% if member is defined %}
            {% set mname = member.sname %}
        {% else %}
            {% set mname = memberName({id: mid}) %}
        {% endif %}

        <tr class="{{ transaction.getRowClass() }}">
            <td data-scope="row">
                {% if preferences.pref_show_id %}
                    {{ transaction.id }}
                {% else %}
                    {{ ordre + 1 + (filters.current_page - 1) * numrows }}
                {% endif %}
                <span class="displaynone">
                            <a href="{{ url_for("editTransaction", {"id": transaction.id}) }}">
                                {{ _T("Transaction %id")|replace({"%id": transaction.id}) }}
                            </a>
                        </span>
            </td>
            <td data-col-label="{{ _T("Date") }}">{{ transaction.date }}</td>
        {% if ((login.isAdmin() or login.isStaff()) and member is not defined) or pmember is defined %}
            <td data-col-label="{{ _T("Member") }}">
            {% if filters.filtre_cotis_adh == "" or filters.filtre_cotis_adh == null %}
                <a
                    href="{{ url_for("contributions", {"type": "transactions", "option": "member", "value": mid}) }}"
                    title="{{ _T("Show only '%name' transactions")|replace({"%name": mname}) }}"
                >
                    <i class="filter grey small icon" aria-hidden="true"></i>
                </a>
            {% endif %}
                <a
                    href="{{ url_for("member", {"id": mid}) }}"
                    title="{{ _T("Show '%name' card")|replace({"%name": mname}) }}"
                >
                    {{ mname }}
                </a>
            </td>
        {% endif %}
            <td data-col-label="{{ _T("Description") }}">{{ transaction.description|escape }}</td>
            <td data-col-label="{{ _T("Amount") }}">{{ transaction.amount }}</td>
            <td data-col-label="{{ _T("Payment type") }}">{{ transaction.getPaymentType() }}</td>
            {% if login.isAdmin() or login.isStaff() %}
            <td class="center actions_row">
                <a
                    href="{{ url_for("editTransaction", {"id": transaction.id}) }}"
                    class="action"
                >
                    <i class="ui edit icon tooltip" aria-hidden="true"></i>
                    <span class="ui special popup">{{ _T("Edit transaction #%id")|replace({"%id": transaction.id}) }}</span>
                </a>
                <a
                    href="{{ url_for("removeContribution", {"type": "transactions", "id": transaction.id}) }}"
                    class="delete"
                >
                    <i class="ui trash red icon tooltip" aria-hidden="true"></i>
                    <span class="ui special popup">{{ _T("Remove transaction #%id")|replace({"%id": transaction.id}) }}</span>
                </a>
            </td>
        {% endif %}
        </tr>
    {% else %}
        <tr><td colspan="{% if login.isAdmin() or login.isStaff() %}7{% else %}5{% endif %}" class="emptylist">{{ _T("no transaction") }}</td></tr>
    {% endfor %}
{% endblock %}

{% block legend %}
        <div id="legende" title="{{ _T("Legend") }}" class="ui modal">
            <div class="header">{{ _T("Legend") }}</div>
            <div class="content">
                <table class="ui stripped table">
                    <tr>
                        <th class="transaction-normal color-sample"><i class="ui icon">&nbsp;</i></th>
                        <td class="back">{{ _T("Completely dispatched transaction") }}</td>
                    </tr>
                    <tr>
                        <th class="transaction-uncomplete color-sample"><i class="ui icon">&nbsp;</i></th>
                        <td class="back">{{ _T("Uncomplete dispatched transaction") }}</td>
                    </tr>
                </table>
            </div>
            <div class="actions"><div class="ui labeled icon deny button"><i class="times icon" aria-hidden="true"></i> {{ _T("Close") }}</div></div>
        </div>
{% endblock %}

{% block javascripts %}
        <script type="text/javascript">
            $(function(){
                var _checklinks = '<div class="checkboxes ui basic horizontal segments"><div class="ui basic right aligned fitted segment"><a href="#" class="show_legend  ui blue tertiary button">{{ _T("Show legend")|e('js') }}</a></div></div>';
                $('.listing').before(_checklinks);
                $('.listing').after(_checklinks);

                //$('#table_footer').parent().before('<td class="right" colspan="{% if (login.isAdmin() or login.isStaff()) and member is not defined %}9{% elseif login.isAdmin() or login.isStaff() %}8{% else %}7{% endif %}"><a href="#" class="show_legend">{{ _T("Show legend") }}</a></td>');

                _bind_legend();

                {% include "elements/js/removal.js.twig" with {
                    single_action: "true"
                } %}
            });
        </script>
{% endblock %}
