{% set showUserColumn = showUserColumn ?? true %}
{% set showInternalRate = showInternalRate ?? false %}
{% set showRateColumn = showRateColumn ?? true %}
{% set showRateBudget = showRateBudget ?? false %}
{% set showTimeBudget = showTimeBudget ?? false %}
{% set decimal = decimal ?? false %}
{% if query.user %}
    {# this is only triggered, if a user exports from his personal timesheet screen #}
    {% set showUserColumn = false %}
    {# if exporting via the admin screen, users without view_rate_own_timesheet might still see their own rates - maybe merge view_rate_own_timesheet and view_rate_other_timesheet into a new view_rate permission? #}
    {% set showRateColumn = is_granted('view_rate_own_timesheet') %}
{% endif %}
<html lang="{{ app.request.locale }}">
<head>
    {% block styles %}
    <style>
        body {
            font-family: sans-serif;
            font-size: 10pt;
            margin: 0;
            padding: 0;
        }
        p {
            margin: 0;
        }
        table.items {
            border: 0.1mm solid #000000;
            width: 100%;
            font-size: 9pt;
            border-collapse: collapse;
        }
        td, th {
            padding: 7px;
        }
        td {
            vertical-align: top;
        }
        .items td {
            border-left: 0.1mm solid #000000;
            border-right: 0.1mm solid #000000;
        }
        .items tr.even {
            background-color: #f5f5f5;
        }
        .items tr.summary {
            background-color: #efefef;
        }
        .items tr.summary td {
            font-weight: bold;
            border-top: 0.1mm solid #000000;
            border-bottom: 0.1mm solid #000000;
        }
        table thead th {
            background-color: #ececec;
            border: 0.1mm solid #000000;
            font-weight: bold;
            font-size: 10pt;
            text-align: left;
        }
        .items td.totals {
            font-weight: bold;
            border: 0.1mm solid #000000;
        }
        .items .center,
        .items td.duration,
        .items td.cost {
            text-align: center;
        }
        .text-nowrap {
            white-space: nowrap;
        }
    </style>
    {% endblock %}
</head>
<body>
{% block pdf_footer %}
<!--mpdf
<htmlpagefooter name="myfooter">
<table style="border-top: 1px solid #000000; font-size: 9pt; padding-top: 3mm; width: 100%">
<tr>
    <td align="left">
    {{ 'export.page_of'|trans({'%page%': '{PAGENO}', '%pages%': '{nb}'}) }}
    {% if not showUserColumn and query.user %}
        &ndash;
        {{ 'label.user'|trans }}: {{ query.user.displayName }} 
    {% endif %}
    </td>
    <td align="right">
    {% if kimai_context.branding.company is not empty %}
        {{ kimai_context.branding.company|raw }} &ndash; {{ now|date_full }}
    {% else %}
        {{ 'export.date_copyright'|trans({'%date%': now|date_full, '%kimai%': '<a href="' ~ constant('App\\Constants::HOMEPAGE') ~ '">' ~ constant('App\\Constants::SOFTWARE') ~ '</a>'})|raw }}
    {% endif %}
    </td>
</tr>
</table>
</htmlpagefooter>
<sethtmlpagefooter name="myfooter" value="on" />
mpdf-->
{% endblock %}
{% block summary %}
    <h2 style="margin-bottom: 0; padding-bottom: 0">{{ 'export.document_title'|trans }}</h2>
    <p>
        {{ 'export.period'|trans }}:
        {{ query.begin|date_short }} - {{ query.end|date_short }}
    </p>
    <h3>{{ 'export.summary'|trans }}</h3>
    <table class="items">
        <thead>
        <tr>
            <th>{{ 'label.customer'|trans }}</th>
            <th>{{ 'label.project'|trans }}</th>
            {% if showTimeBudget %}
                <th class="center">{{ 'label.timeBudget'|trans }}</th>
            {% endif %}
            {% if showRateBudget %}
                <th class="center">{{ 'label.budget'|trans }}</th>
            {% endif %}
            <th class="center">{{ 'label.duration'|trans }}</th>
            {% if showRateColumn %}
                {% if showInternalRate %}
                    <th class="center">{{ 'label.rate_internal'|trans }}</th>
                {% endif %}
                <th class="center">{{ 'label.rate'|trans }}</th>
            {% endif %}
        </tr>
        </thead>
        <tbody>
        {% set customer = null %}
        {% set customerDuration = 0 %}
        {% set customerRate = 0 %}
        {% set customerInternalRate = 0 %}
        {% set customerCurrency = null %}
        {% set customerCount = 0 %}
        {% for id, summary in summaries %}
            {% if customer is same as(null) %}
                {% set customer = summary.customer %}
                {% set customerCurrency = summary.currency %}
            {% endif %}
            {% if customer is not same as(summary.customer) %}
                    <tr class="summary">
                        <td colspan="2">
                        </td>
                        {% if showTimeBudget %}
                            <td></td>
                        {% endif %}
                        {% if showRateBudget %}
                            <td></td>
                        {% endif %}
                        <td class="totals duration">{{ customerDuration|duration(decimal) }}</td>
                        {% if showRateColumn %}
                            {% if showInternalRate %}
                                <td class="totals cost">{{ customerInternalRate|money(customerCurrency) }}</td>
                            {% endif %}
                            <td class="totals cost">{{ customerRate|money(customerCurrency) }}</td>
                        {% endif %}
                    </tr>
                {% set customerCurrency = summary.currency %}
                {% set customer = summary.customer %}
                {% set customerDuration = 0 %}
                {% set customerRate = 0 %}
                {% set customerInternalRate = 0 %}
                {% set customerCount = 0 %}
            {% endif %}
            <tr class="{{ cycle(['odd', 'even'], customerCount) }}">
                <td>{{ summary.customer }}</td>
                <td>{{ summary.project }}</td>
                {% if showTimeBudget %}
                    <td class="center">
                        {% if budgets[id] is defined and budgets[id].time_left > 0 %}
                            {{ budgets[id].time_left|duration(decimal) }}
                        {% endif %}
                    </td>
                {% endif %}
                {% if showRateBudget %}
                    <td class="center">
                        {% if budgets[id] is defined and budgets[id].money_left > 0 %}
                            {{ budgets[id].money_left|money(summary.currency) }}
                        {% endif %}
                    </td>
                {% endif %}
                <td class="duration">{{ summary.duration|duration(decimal) }}</td>
                {% if showRateColumn %}
                    {% if showInternalRate %}
                        <td class="cost">{{ summary.rate_internal|money(summary.currency) }}</td>
                    {% endif %}
                    <td class="cost">{{ summary.rate|money(summary.currency) }}</td>
                {% endif %}
            </tr>
            {% set customerDuration = customerDuration + summary.duration %}
            {% set customerRate = customerRate + summary.rate %}
            {% set customerInternalRate = customerInternalRate + summary.rate_internal %}
            {% set customerCount = customerCount + 1 %}
        {% endfor %}
        {% if customer is not same as(null) %}
        <tr class="summary">
            <td colspan="2"></td>
            {% if showTimeBudget %}
                <td></td>
            {% endif %}
            {% if showRateBudget %}
                <td></td>
            {% endif %}
            <td class="totals duration">{{ customerDuration|duration(decimal) }}</td>
            {% if showRateColumn %}
                {% if showInternalRate %}
                    <td class="totals cost">{{ customerInternalRate|money(customerCurrency) }}</td>
                {% endif %}
                <td class="totals cost">{{ customerRate|money(customerCurrency) }}</td>
            {% endif %}
        </tr>
        {% endif %}
        </tbody>
    </table>

    <pagebreak>
{% endblock %}
{% block items %}
    <h3>{{ 'export.full_list'|trans }}</h3>

    {% set duration = 0 %}
    {% set rate = 0 %}
    {% set rateInternal = 0 %}
    {% set currency = false %}
    <table class="items">
        <thead>
        <tr>
            <th>{{ 'label.date'|trans }}</th>
            {% if showUserColumn %}
                <th>{{ 'label.user'|trans }}</th>
            {% endif %}
            <th>{{ 'label.description'|trans }}</th>
            <th class="center">{{ 'label.duration'|trans }}</th>
            {% if showRateColumn %}
                {% if showInternalRate %}
                    <th class="center">{{ 'label.rate_internal'|trans }}</th>
                {% endif %}
                <th class="center">{{ 'label.rate'|trans }}</th>
            {% endif %}
        </tr>
        </thead>
        <tbody>
        {% for entry in entries %}
            {% set duration = duration + entry.duration %}
            {% if currency is same as(false) %}
                {% set currency = entry.project.customer.currency %}
            {% endif %}
            {% if currency is not same as(entry.project.customer.currency) %}
                {% set currency = null %}
            {% endif %}
            <tr class="{{ cycle(['odd', 'even'], loop.index0) }}">
                <td class="text-nowrap">
                    {{ entry.begin|date_time }}
                    {% if entry.end %}
                        <br>
                        {{ entry.end|date_time }}
                    {% endif %}
                </td>
                {% if showUserColumn %}
                    <td>{{ entry.user.displayName }}</td>
                {% endif %}
                <td>
                    {{ entry.project.customer.name }} - {{ entry.project.name }}{% if entry.activity is not null %} - {{ entry.activity.name }}{% endif %}
                    {% if entry.description is not empty %}
                        <br>
                        <i>{{ entry.description|escape|desc2html }}</i>
                    {% endif %}
                </td>
                <td class="duration">{{ entry.duration|duration(decimal) }}</td>
                {% if showRateColumn %}
                    {% if is_granted('view_rate', entry) %}
                        {% set rate = rate + entry.rate %}
                        {% set rateInternal = rateInternal + entry.internalRate %}
                        {% set entryRate = entry.rate|money(entry.project.customer.currency) %}
                        {% set entryRateInternal = entry.internalRate|money(entry.project.customer.currency) %}
                    {% else %}
                        {% set entryRate = '&ndash;' %}
                        {% set entryRateInternal = '&ndash;' %}
                    {% endif %}
                    {% if showInternalRate %}
                        <td class="cost">{{ entryRateInternal }}</td>
                    {% endif %}
                    <td class="cost">{{ entryRate }}</td>
                {% endif %}
            </tr>
        {% endfor %}
        <tr class="summary">
            {% if showUserColumn %}
                <td colspan="3"></td>
            {% else %}
                <td colspan="2"></td>
            {% endif %}
            <td class="totals duration">{{ duration|duration(decimal) }}</td>
            {% if showRateColumn %}
                {% if showInternalRate %}
                    <td class="totals cost">{% if currency is not null %}{{ rateInternal|money(currency) }}{% endif %}</td>
                {% endif %}
                <td class="totals cost">{% if currency is not null %}{{ rate|money(currency) }}{% endif %}</td>
            {% endif %}
        </tr>
        </tbody>
    </table>
{% endblock %}
</body>
</html>
