{% macro table_elements(elements, type) %}

<table class="table table-bordered border-dark budget-table">
    <thead>
        <tr>
            <th class="{{ type }} el-type">{{ 'Budget element type'|trans }}</th>
            <th class="{{ type }}">{{ 'Amount'|trans }}</th>
            <th class="{{ type }}">{{ 'Validity period'|trans }}</th>
            <th class="{{ type }}">&nbsp;</th>
        </tr>
    </thead>
    <tbody>
        {% set total = 0 %}
        {% for f in elements %}
        {% set total = total + f.amount %}
        <tr>
            <td class="el-type">
                {% if f.isResource %}
                    {{ f.resource.name|localize_translatable_string }}
                    {% if f.resource.getKind is same as 'other' %}
                        : {{ f.getComment }}
                    {% endif %}
                {% else %}
                    {{ f.charge.name|localize_translatable_string }}
                        {% if f.charge.getKind is same as 'other' %}
                            : {{ f.getComment }}
                        {% endif %}
                {% endif %}
            </td>
            <td>{{ f.amount|format_currency('EUR') }}</td>
            <td>
                {% if f.endDate is not null %}
                    {{ f.startDate|format_date('short') ~ ' - ' ~ f.endDate|format_date('short') }}
                {% else %}
                    {{ 'depuis le  ' ~ f.startDate|format_date('short') }}
                {% endif %}
            </td>
            <td class="column-fixed">
                <ul class="record_actions">
                    {% if is_granted('CHILL_BUDGET_ELEMENT_SEE', f) %}
                        <li>
                            <a href="{{ path('chill_budget_' ~ type ~ '_view', { 'id': f.id } ) }}" class="btn btn-sm btn-show"></a>
                        </li>
                    {% endif %}
                    {% if is_granted('CHILL_BUDGET_ELEMENT_UPDATE', f) %}
                        <li>
                            <a href="{{ path('chill_budget_' ~ type ~'_edit', { 'id': f.id } ) }}" class="btn btn-sm btn-edit"></a>
                        </li>
                    {% endif %}
                    {% if is_granted('CHILL_BUDGET_ELEMENT_DELETE', f) %}
                        <li>
                            <a href="{{ path('chill_budget_' ~ type ~ '_delete', { 'id': f.id } ) }}" class="btn btn-sm btn-delete"></a>
                        </li>
                    {% endif %}
                </ul>
            </td>
        </tr>
        {% endfor %}
        <tr class="total">
            <td class="el-type">
                {{ 'Total'|trans }}
            </td>
            <td>
                {{ total|format_currency('EUR') }}
            </td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </tbody>
</table>
{% endmacro %}

{% macro table_results(actualCharges, actualResources, results) %}

{% set totalCharges = 0 %}
{% for c in actualCharges %}
    {% set totalCharges = totalCharges + c.amount %}
{% endfor %}

{% set totalResources = 0 %}
{% for r in actualResources %}
    {% set totalResources = totalResources + r.amount %}
{% endfor %}

{% set result = (totalResources - totalCharges) %}

<table class="table table-bordered border-dark">
    <thead>
        <tr>
            <th>&nbsp;</th>
            <th>{{ 'Budget calculator result'|trans }}</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>{{ 'The balance'|trans }}</td>
            <td>
                {{ result|format_currency('EUR') }}
            </td>
        </tr>
        {% for result in results %}
            <tr>
                <td>{{ result.label }}</td>
                <td>
                    {% if result.type == 'currency' %}
                        {{ result.result|format_currency('EUR') }}
                    {% elseif result.type == 'percentage' %}
                        {{ result.result|round(2, 'ceil') ~ '%' }}
                    {% else %}
                        {{ result.result|round(2, 'common') }}
                    {% endif %}
                </td>
            </tr>
        {% endfor %}
    </tbody>
</table>
{% endmacro %}
