{% import "macros/widgets.html.twig" as widgets %}
{%- set absoluteDuration = 0 -%}
{%- set absoluteInternalRate = 0 -%}
{%- set absoluteRate = 0 -%}
{%- set totalsDuration = {} -%}
{%- set totalsInternalRate = {} -%}
{%- set totalsRate = {} -%}
{% if dataType == 'rate' %}
    {% set dataTypeTitle = 'stats.amountTotal' %}
{% elseif dataType == 'internalRate' %}
    {% set dataTypeTitle = 'label.rate_internal' %}
{% else %}
    {% set dataTypeTitle = 'stats.durationTotal' %}
{% endif %}
{% set columns = 2 %}
{% set totalCurrency = false %}
<table class="table table-bordered table-hover dataTable">
    <thead>
        <tr>
            <th>&nbsp;</th>
            <th class="text-center reportDataTypeTitle">{{ dataTypeTitle|trans }}</th>
            {% for column in period.dateTimes %}
                {% block period_name %}{% endblock %}
                {% set columns = columns + 1 %}
                {% set dateKey = column|report_date %}
                {% set totalsDuration = totalsDuration|merge({(dateKey): 0}) %}
                {% set totalsInternalRate = totalsInternalRate|merge({(dateKey): 0}) %}
                {% set totalsRate = totalsRate|merge({(dateKey): 0}) %}
            {% endfor %}
        </tr>
    </thead>
    <tbody>
    {% set oldCustomer = null %}
    {% for project in rows|sort((a,b) => a.project.customer.id <=> b.project.customer.id) %}
        {% set currency = project.project.customer.currency %}
        {% if oldCustomer is null or oldCustomer != project.project.customer.id %}
            {% set oldCustomer = project.project.customer.id %}
            {% if totalCurrency is same as (false) %}
                {% set totalCurrency = currency %}
            {% elseif project.project.customer.currency != totalCurrency %}
                {% set totalCurrency = null %}
            {% endif %}
            <tr class="summary">
                <td colspan="{{ columns }}">{{ widgets.label_customer(project.project.customer) }}</td>
            </tr>
        {% endif %}
        {% set absoluteDuration = absoluteDuration + project.duration %}
        {% set absoluteInternalRate = absoluteInternalRate + project.internalRate %}
        {% set absoluteRate = absoluteRate + project.rate %}
        <tr class="project">
            <td class="text-nowrap">
                <strong>{{ widgets.label_project(project.project) }}</strong>
            </td>
            <td class="text-nowrap text-center total">
                {% if dataType == 'rate' %}
                    {{ project.rate|money(currency) }}
                {% elseif dataType == 'internalRate' %}
                    {{ project.internalRate|money(currency) }}
                {% else %}
                    {{ project.duration|duration(decimal) }}
                {% endif %}
            </td>
            {% for column in project.data.data %}
                {% set dateKey = column.date|report_date %}
                <td class="text-nowrap text-center day-total {% block column_classes_project %}{% endblock %}">
                    {% if column.duration > 0 or column.rate > 0 or column.internalRate > 0 %}
                        {% if dataType == 'rate' %}
                            {% set totalsRate = totalsRate|merge({(dateKey): (totalsRate[dateKey] + column.rate)}) %}
                            <strong>{{ column.rate|money(currency) }}</strong>
                        {% elseif dataType == 'internalRate' %}
                            {% set totalsInternalRate = totalsInternalRate|merge({(dateKey): (totalsInternalRate[dateKey] + column.internalRate)}) %}
                            <strong>{{ column.internalRate|money(currency) }}</strong>
                        {% else %}
                            {% set totalsDuration = totalsDuration|merge({(dateKey): (totalsDuration[dateKey] + column.duration)}) %}
                            <strong>{{ column.duration|duration(decimal) }}</strong>
                        {% endif %}
                    {% endif %}
                </td>
            {% endfor %}
        </tr>
        {% for activity in project.activities %}
            <tr class="activity">
                <td class="text-nowrap">
                    {{ widgets.label_activity(activity.activity) }}
                </td>
                <td class="text-nowrap text-center">
                    {% if dataType == 'rate' %}
                        {{ activity.rate|money(currency) }}
                    {% elseif dataType == 'internalRate' %}
                        {{ activity.internalRate|money(currency) }}
                    {% else %}
                        {{ activity.duration|duration(decimal) }}
                    {% endif %}
                </td>
                {% for column in activity.data.data %}
                    <td class="text-nowrap text-center day-total {% block column_classes_activity %}{% endblock %}">
                        {% if column.duration > 0 or column.rate > 0 or column.internalRate > 0 %}
                            {% if dataType == 'rate' %}
                                {{ column.rate|money(currency) }}
                            {% elseif dataType == 'internalRate' %}
                                {{ column.internalRate|money(currency) }}
                            {% else %}
                                {{ column.duration|duration(decimal) }}
                            {% endif %}
                        {% endif %}
                    </td>
                {% endfor %}
            </tr>
        {% endfor %}
    {% endfor %}
    </tbody>
    {% if totalCurrency is same as (false) %}
        {% set totalCurrency = null %}
    {% endif %}
    <tfoot>
        <tr class="summary">
            <td>{{ dataTypeTitle|trans }}</td>
            <td class="text-nowrap text-center total">
                {% if dataType == 'rate' %}
                    {{ absoluteRate|money(totalCurrency) }}
                {% elseif dataType == 'internalRate' %}
                    {{ absoluteInternalRate|money(totalCurrency) }}
                {% else %}
                    {{ absoluteDuration|duration(decimal) }}
                {% endif %}
            </td>
            {% for column in period.dateTimes %}
                {% set dateKey = column|report_date %}
                <td class="text-nowrap text-center day-total {% block column_classes_total %}{% endblock %}">
                    {% if dataType == 'rate' %}
                        {{ totalsRate[dateKey]|money(totalCurrency) }}
                    {% elseif dataType == 'internalRate' %}
                        {{ totalsInternalRate[dateKey]|money(totalCurrency) }}
                    {% else %}
                        {{ totalsDuration[dateKey]|duration(decimal) }}
                    {% endif %}
                </td>
            {% endfor %}
        </tr>
    </tfoot>
</table>
