{% extends 'reporting/layout.html.twig' %}
{% import "macros/datatables.html.twig" as tables %}

{% set showMoneyBudget = is_granted('budget_money', 'project') %}
{% set showTimeBudget = is_granted('budget_time', 'project') %}

{% set availableColumns = {
    'name':          {'class': 'alwaysVisible'},
} %}
{% if showTimeBudget %}
    {% set availableColumns = availableColumns|merge({
        'timeBudget':    {'class': 'd-none d-md-table-cell', 'title': 'timeBudget'|trans},
    }) %}
{% endif %}
{% if showMoneyBudget %}
    {% set availableColumns = availableColumns|merge({
        'budget':        {'class': 'd-none d-md-table-cell', 'title': 'budget'|trans},
    }) %}
{% endif %}
{% set availableColumns = availableColumns|merge({
    'lastRecord':       {'class': 'd-none d-sm-table-cell text-start hw-min w-min', 'title': 'last_record'|trans, 'columnClass': 'w-min'},
    'today':            {'class': 'd-none text-end hw-min w-min', 'title': 'stats.durationToday'|trans},
    'week':             {'class': 'd-none text-end hw-min w-min', 'title': 'stats.durationWeek'|trans},
    'month':            {'class': 'd-none d-lg-table-cell text-end hw-min w-min', 'title': 'stats.durationMonth'|trans},
    'durationTotal':    {'class': 'text-end hw-min w-min', 'title': 'stats.durationTotal'|trans, 'columnClass': 'w-min'},
}) %}
{% if showTimeBudget and is_granted('create_export') %}
    {% set availableColumns = availableColumns|merge({
        'exported':         {'class': 'd-none d-xl-table-cell text-end hw-min w-min', 'title': 'not_exported'|trans, 'columnClass': 'w-min'},
    }) %}
{% endif %}
{% if showMoneyBudget and is_granted('view_invoice') %}
    {% set availableColumns = availableColumns|merge({
        'invoiced':         {'class': 'd-none d-xl-table-cell text-end hw-min w-min', 'title': 'not_invoiced'|trans, 'columnClass': 'w-min'},
    }) %}
{% endif %}
{% set availableColumns = availableColumns|merge({
    'projectStart':     {'class': 'd-none text-start hw-min w-min', 'title': 'project_start'|trans},
    'projectEnd':       {'class': 'd-none text-start hw-min w-min', 'title': 'project_end'|trans},
    'comment':          {'class': 'd-none', 'title': 'comment'|trans},
    'actions':          {'class': 'actions alwaysVisible'},
}) %}
{% set tableName = tableName|default('project_view_reporting') %}
{% set skipColumns = skipColumns is defined ? skipColumns : {} %}
{% set columns = {} %}
{% for name, config in availableColumns %}
    {% if name not in skipColumns %}
        {% set columns = columns|merge({(name): config}) %}
    {% endif %}
{% endfor %}

{% block main_before %}
    {{ tables.data_table_column_modal(tableName, columns) }}
{% endblock %}

{% block report %}

    {% set hasData = entries|length > 0 %}

    {% embed '@theme/embeds/card.html.twig' %}
        {% import "macros/progressbar.html.twig" as progress %}
        {% import "macros/widgets.html.twig" as widgets %}
        {% import "macros/datatables.html.twig" as tables %}
        {% import "project/actions.html.twig" as projectActions %}
        {% block box_body_class %}{{ tableName }}-box {% if hasData %}p-0{% endif %}{% endblock %}
        {% block box_body %}
            {% if not hasData %}
                {{ widgets.nothing_found() }}
            {% else %}
                {{ tables.datatable_header(tableName, columns, null, {boxClass: ''}) }}

                {% for id, mapping in entries|sort((a, b) => a.customer.name <=> b.customer.name) %}
                    <tr class="summary">
                        <td colspan="{{ columns|length }}">{{ widgets.label_customer(mapping.customer) }}</td>
                    </tr>
                    {% for entry in mapping.projects|sort((a, b) => a.project.name <=> b.project.name) %}
                        {% set project = entry.project %}
                        {% set budgetStats = entry.getBudgetStatisticModel() %}
                        {% set currency = project.customer.currency %}
                        <tr {{ widgets.project_row_attr(project, now) }}>
                        {% for name, column_config in columns %}
                            <td class="{{ tables.data_table_column_class(tableName, columns, name) }}">
                            {% if name == 'name' %}
                                {{ widgets.label_project(project) }}
                            {% elseif name == 'lastRecord' %}
                                {% if entry.lastRecord is not null %}
                                    {{ entry.lastRecord|date_short }}
                                {% else %}
                                    &ndash;
                                {% endif %}
                            {% elseif name == 'today' %}
                                {{ entry.durationDay|duration }}
                            {% elseif name == 'week' %}
                                {{ entry.durationWeek|duration }}
                            {% elseif name == 'month' %}
                                {{ entry.durationMonth|duration }}
                            {% elseif name == 'durationTotal' %}
                                {{ entry.durationTotal|duration }}
                            {% elseif name == 'timeBudget' %}
                                {% if budgetStats.hasTimeBudget() and is_granted('time', project) %}
                                    {{ progress.progressbar_timebudget(budgetStats) }}
                                {% endif %}
                            {% elseif name == 'budget' %}
                                {% if project.hasBudget() and is_granted('budget', project) %}
                                    {{ progress.progressbar_budget(budgetStats, project.customer.currency) }}
                                {% endif %}
                            {% elseif name == 'exported' %}
                                <a href="{{ path('export', {'customers[]': project.customer.id, 'projects[]': project.id, 'daterange': '', 'preview': true}) }}">
                                    {{ entry.notExportedDuration|duration }}
                                </a>
                            {% elseif name == 'invoiced' %}
                                <a href="{{ path('invoice', {'customers[]': project.customer.id, 'projects[]': project.id, 'daterange': ''}) }}">
                                    {{ entry.notBilledRate|money(currency) }}
                                </a>
                            {% elseif name == 'projectStart' %}
                                {% if project.start is not null %}{{ project.start|date_short }}{% endif %}
                            {% elseif name == 'projectEnd' %}
                                {% if project.end is not null %}{{ project.end|date_short }}{% endif %}
                            {% elseif name == 'comment' %}
                                {{ project.comment }}
                            {% elseif name == 'actions' %}
                                {{ projectActions.project(project, 'custom') }}
                            {% endif %}
                            </td>
                        {% endfor %}
                        </tr>
                    {% endfor %}
                {% endfor %}
                {{ tables.data_table_footer(entries) }}
            {% endif %}
        {% endblock %}
    {% endembed %}

{% endblock %}
