{% 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 = 'internalRate' %}
{% else %}
    {% set dataTypeTitle = 'stats.durationTotal' %}
{% endif %}
{% set columns = 0 %}
{% set totalCurrency = false %}
<table class="table 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>
    {% for customerId, data in rows %}
        {% set currency = data.customer.currency %}
        {% if totalCurrency is same as (false) %}
            {% set totalCurrency = currency %}
        {% elseif data.customer != totalCurrency %}
            {% set totalCurrency = null %}
        {% endif %}
        <tr class="summary">
            <td>{{ widgets.label_customer(data.customer) }}</td>
            <td class="text-center total">
                {% if dataType == 'rate' %}
                    {{ data.rate|money(currency) }}
                {% elseif dataType == 'internalRate' %}
                    {{ data.internalRate|money(currency) }}
                {% else %}
                    {{ data.duration|duration(decimal) }}
                {% endif %}
            </td>
            <td colspan="{{ columns }}"></td>
        </tr>
        {% for project in data.projects %}
            {% 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 %}
    {% 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>
