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

{% block document_title %}{{ 'export'|trans }}{% endblock %}

{% set storageItemName = 'kimai_html_export' %}

{% set decimal = decimal|default(false) %}
{% set columnTitles = {} %}
{% set columns = {
    'date': true,
    'begin': false,
    'end': false,
    'username': false,
    'customer': true,
    'project': true,
    'activity': true,
    'description': false,
    'exported': false,
    'tags': false,
} %}

{% for id, field in timesheetMetaFields %}
    {% set columns = columns|merge({ ('t_' ~ field.name): true}) %}
    {% set columnTitles = columnTitles|merge({ ('t_' ~ field.name): (field.label)}) %}
{% endfor %}
{% for id, field in customerMetaFields %}
    {% set columns = columns|merge({ ('c_' ~ field.name): true}) %}
    {% set columnTitles = columnTitles|merge({ ('c_' ~ field.name): (field.label)}) %}
{% endfor %}
{% for id, field in projectMetaFields %}
    {% set columns = columns|merge({ ('p_' ~ field.name): true}) %}
    {% set columnTitles = columnTitles|merge({ ('p_' ~ field.name): (field.label)}) %}
{% endfor %}
{% for id, field in activityMetaFields %}
    {% set columns = columns|merge({ ('a_' ~ field.name): true}) %}
    {% set columnTitles = columnTitles|merge({ ('a_' ~ field.name): (field.label)}) %}
{% endfor %}
{% for id, field in userPreferences %}
    {% set columns = columns|merge({ ('u_' ~ field.name): true}) %}
    {% set columnTitles = columnTitles|merge({ ('u_' ~ field.name): (field.label)}) %}
{% endfor %}

{% set columns = columns|merge({
    'hourlyRate': false,
    'fixedRate': false,
    'duration': 'duration',
    'internalRate': 'internalRate',
    'rate': 'rate',
}) %}

{% block javascripts %}
    {{ webloader.init_frontend_loader() }}
    <script type="text/javascript">
        let initialized = false;

        document.addEventListener('kimai.initialized', function(event) {
            document.getElementById('duration-decimal').addEventListener('click', function(event) {
                var spans = document.getElementsByClassName('duration-format');
                for (var span of spans) {
                    if (!event.target.checked) {
                        if (span.dataset['duration'] !== undefined) {
                            span.innerHTML = span.dataset['duration'];
                        }
                    } else {
                        if (span.dataset['durationDecimal'] !== undefined) {
                            span.innerHTML = span.dataset['durationDecimal'];
                        }
                    }
                }
                saveVisibility();
            });

            document.getElementById('summary-by-activities').addEventListener('click', function(event) {
                document.getElementById('summary-project').style.display = event.target.checked ? 'none' : 'table';
                document.getElementById('summary-activity').style.display = event.target.checked ? 'table' : 'none';
                saveVisibility();
            });
            document.getElementById('date-format').addEventListener('change', function(event) {
                changedDateFormat(event.target.value, 'dateformat');
            });
            document.getElementById('begin-format').addEventListener('change', function(event) {
                changedDateFormat(event.target.value, 'beginformat');
            });
            document.getElementById('end-format').addEventListener('change', function(event) {
                changedDateFormat(event.target.value, 'endformat');
            });
            document.getElementById('summary-show').addEventListener('click', function(event) {
                document.getElementById('export-summary').style.display = event.target.checked ? 'block' : 'none';
                saveVisibility();
            });
            document.getElementById('summary-timeBudget').addEventListener('click', function(event) {
                let cells = document.getElementsByClassName('export-timeBudget');
                for (let columnCell of cells) {
                    columnCell.style.display = event.target.checked ? 'table-cell' : 'none';
                }
                saveVisibility();
            });
            document.getElementById('summary-budget').addEventListener('click', function(event) {
                let cells = document.getElementsByClassName('export-budget');
                for (let columnCell of cells) {
                    columnCell.style.display = event.target.checked ? 'table-cell' : 'none';
                }
                saveVisibility();
            });
            document.getElementById('summary-duration').addEventListener('click', function(event) {
                let cells = document.getElementsByClassName('summary-duration');
                for (let columnCell of cells) {
                    columnCell.style.display = event.target.checked ? 'table-cell' : 'none';
                }
                saveVisibility();
            });
            document.getElementById('summary-rate').addEventListener('click', function(event) {
                let cells = document.getElementsByClassName('summary-rate');
                for (let columnCell of cells) {
                    columnCell.style.display = event.target.checked ? 'table-cell' : 'none';
                }
                saveVisibility();
            });
            document.getElementById('summary-internalRate').addEventListener('click', function(event) {
                let cells = document.getElementsByClassName('summary-internalRate');
                for (let columnCell of cells) {
                    columnCell.style.display = event.target.checked ? 'table-cell' : 'none';
                }
                saveVisibility();
            });
            document.getElementById('summary-show').addEventListener('click', function(event) {
                document.getElementById('export-summary').style.display = event.target.checked ? 'block' : 'none';
                saveVisibility();
            });
            document.getElementById('records-show').addEventListener('click', function(event) {
                document.getElementById('export-records').style.display = event.target.checked ? 'block' : 'none';
                saveVisibility();
            });

            let columnCheckboxes = document.getElementsByClassName('column-visibility-changer');

            for (let checkbox of columnCheckboxes) {
                checkbox.addEventListener('click', function(event) {
                    changeVisibility(event.target.name, event.target.checked);
                });
            }

            let editableTitles = document.querySelectorAll('[contenteditable=true]');

            for (let editable of editableTitles) {
                editable.addEventListener('input', function(event) {
                    if (event.target.innerText === '') {
                        return;
                    }
                    saveVisibility();
                });
            }

            // needs to be executed as last action in the flow, after the listener were registered
            let config = localStorage.getItem('{{ storageItemName }}');
            if (config !== null) {
                try {
                    config = JSON.parse(config);
                    for (const elName in config) {
                        if (!config.hasOwnProperty(elName)) {
                            continue;
                        }
                        let elValue = config[elName];
                        let el = document.getElementById(elName);
                        if (el === undefined || el === null) {
                            el = document.getElementById('records-column-' + elName);
                            if (el === undefined || el === null) {
                                continue;
                            }
                        }
                        if (el.type === 'checkbox') {
                            if (elValue !== el.checked) {
                                el.click();
                            }
                        } else if (el.type === 'select-one') {
                            if (el.value !== elValue) {
                                el.value = elValue;
                                el.dispatchEvent(new Event('change'));
                            }
                        } else if (el.isContentEditable) {
                            el.innerText = elValue;
                        }
                    }
                } catch (e) {
                    // ignore error in restoring
                    console.log('Failed to restore settings, removing invalid settings', e);
                    localStorage.removeItem('{{ storageItemName }}');
                }
            }

            initialized = true;
        });

        function changeVisibility(column, visible)
        {
            let cells = document.getElementsByClassName('column-' + column);
            for (let columnCell of cells) {
                columnCell.style.display = visible ? 'table-cell' : 'none';
            }
            saveVisibility();
        }

        function changedDateFormat(formatType, className)
        {
            let elements = document.getElementsByClassName(className);
            for (let elem of elements) {
                let formattedDate = elem.dataset[formatType];
                if (formattedDate === undefined) {
                    continue;
                }
                elem.innerHTML = formattedDate;
            }
            saveVisibility();
        }

        function saveVisibility()
        {
            if (!initialized) {
                return;
            }

            const form = document.getElementsByTagName('form')[0];
            let settings = {};
            for (let checkbox of form.querySelectorAll('input[type=checkbox]')) {
                settings[checkbox.getAttribute('id')] = checkbox.checked;
            }
            for (let selectbox of form.querySelectorAll('select')) {
                settings[selectbox.getAttribute('id')] = selectbox.value;
            }
            for (let editable of document.querySelectorAll('[contenteditable=true]')) {
                settings[editable.getAttribute('id')] = editable.innerText;
            }
            localStorage.setItem('{{ storageItemName }}', JSON.stringify(settings));
        }

        function resetExport()
        {
            let editableTitles = document.querySelectorAll('[contenteditable=true]');

            for (let editable of editableTitles) {
                editable.innerText = editable.dataset['original'];
            }

            localStorage.removeItem('{{ storageItemName }}');
            const form = document.getElementsByTagName('form')[0];
            form.reset();
        }
    </script>
{% endblock %}

{% block styles %}
    <style>
        @media print{
            h2 { font-size: 22px; }
            h3 { font-size: 18px; }
            p, td { font-size: 11px; }
            th, td.totals { font-size: 12px; }
        }
        .items td.totals {
            font-weight: bold;
            text-align: right;
        }
        .items td.duration,
        .items th.duration,
        .items th.cost,
        .items td.cost,
        .items th.column-rate,
        .items th.column-internalRate,
        .items th.column-hourlyRate,
        .items th.column-fixedRate,
        .items th.column-duration
        {
            text-align: right;
        }
        .table>thead:first-child>tr:first-child>th {
            padding-right: 5px;
        }
    </style>
{% endblock %}

{% block export %}
    {% set formItemClass = "d-inline-block w-auto text-nowrap me-2" %}
    <form class="w-100 d-print-none row row-cards p-3">
        <div class="card mb-1 p-0">
            <div class="card-header">
                {{ 'settings'|trans }}
            </div>
            <div class="card-body">
                <label for="duration-decimal">
                    <input type="checkbox" id="duration-decimal" name="duration-decimal"{% if decimal%} checked="checked"{% endif %}>
                    {{ 'export_decimal'|trans }}
                </label>
                |
                <label for="date-format">
                    {{ 'date'|trans }}:
                    {% set demo_date = create_date('2024-01-01 11:00:00') %}
                    <select id="date-format" name="date-format">
                        <option value="short">{{ demo_date|date_short }}</option>
                        <option value="time">{{ demo_date|date_time }}</option>
                    </select>
                </label>
                |
                <label for="begin-format">
                    {{ 'begin'|trans }}:
                    {% set demo_date = create_date('2024-01-01 11:00:00') %}
                    <select id="begin-format" name="begin-format">
                        <option value="plain">{{ demo_date|date_format('H:i') }}</option>
                        <option value="short">{{ demo_date|date_short }}</option>
                        <option value="time">{{ demo_date|date_time }}</option>
                    </select>
                </label>
                |
                <label for="end-format">
                    {{ 'end'|trans }}:
                    {% set demo_date = create_date('2024-01-01 18:00:00') %}
                    <select id="end-format" name="end-format">
                        <option value="plain">{{ demo_date|date_format('H:i') }}</option>
                        <option value="short">{{ demo_date|date_short }}</option>
                        <option value="time">{{ demo_date|date_time }}</option>
                    </select>
                </label>
                <button type="reset" class="btn btn-primary btn-sm pull-right" onclick="resetExport()">{{ 'action.reset'|trans }}</button>
            </div>
        </div>

        <div class="card mb-1 p-0">
            <div class="card-header">
                <label for="summary-show">
                    <input type="checkbox" id="summary-show" name="summary-show" checked="checked">
                    {{ 'export.summary'|trans }}
                </label>
            </div>
            <div class="card-body w-100">
                <div class="{{ formItemClass }}">
                    <label for="summary-by-activities">
                        <input type="checkbox" id="summary-by-activities" name="summary-by-activities">
                        {{ 'activity'|trans }}
                    </label>
                </div>
                <div class="{{ formItemClass }}">
                    <label for="summary-timeBudget">
                        <input type="checkbox" id="summary-timeBudget" name="summary-timeBudget" checked>
                        {{ 'timeBudget'|trans }}
                    </label>
                </div>
                <div class="{{ formItemClass }}">
                    <label for="summary-budget">
                        <input type="checkbox" id="summary-budget" name="summary-budget" checked>
                        {{ 'budget'|trans }}
                    </label>
                </div>

                <div class="{{ formItemClass }}">
                    <label for="summary-duration">
                        <input type="checkbox" id="summary-duration" name="summary-duration" checked>
                        {{ 'duration'|trans }}
                    </label>
                </div>
                <div class="{{ formItemClass }}">
                    <label for="summary-internalRate">
                        <input type="checkbox" id="summary-internalRate" name="summary-internalRate" checked>
                        {{ 'internalRate'|trans }}
                    </label>
                </div>
                <div class="{{ formItemClass }}">
                    <label for="summary-rate">
                        <input type="checkbox" id="summary-rate" name="summary-rate" checked>
                        {{ 'rate'|trans }}
                    </label>
                </div>
            </div>
        </div>

        <div class="card mb-1 p-0">
            <div class="card-header">
                <div class="{{ formItemClass }}">
                    <label for="records-show">
                        <input type="checkbox" id="records-show" name="records-show" checked="checked">
                        {{ 'export.full_list'|trans }}
                    </label>
                </div>
            </div>
            <div class="card-body">
                {% for columnId, visibility in columns %}
                    <div class="{{ formItemClass }}">
                        <label for="records-column-{{ columnId }}">
                            <input type="checkbox" class="column-visibility-changer" id="records-column-{{ columnId }}" name="{{ columnId }}" {% if visibility %}checked="checked"{% endif %}>
                            {{ columnTitles[columnId]|default(columnId)|trans }}
                        </label>
                    </div>
                {% endfor %}
            </div>
        </div>
    </form>

    <div class="row m-3">
        <div class="col-xs-12">
            <h2 id="doc-title" contenteditable="true" data-original="{{ 'export.document_title'|trans }}">{{ 'export.document_title'|trans }}</h2>
            <p>
                {{ 'export.period'|trans }}:
                {{ query.begin|date_short }} - {{ query.end|date_short }}
            </p>
        </div>
    </div>

    <div class="row m-3" id="export-summary">
        <div class="card p-0">
            <div class="card-header">
                <h3 class="card-title" id="doc-summary" contenteditable="true" data-original="{{ 'export.summary'|trans }}">{{ 'export.summary'|trans }}</h3>
            </div>
            <table class="items table table-sm table-bordered dataTable" id="summary-project">
                <thead>
                <tr>
                    <th>{{ 'customer'|trans }}</th>
                    <th>{{ 'project'|trans }}</th>
                    <th class="center export-timeBudget">{{ 'timeBudget'|trans }}</th>
                    <th class="center export-budget">{{ 'budget'|trans }}</th>
                    <th class="duration summary-duration">{{ 'duration'|trans }}</th>
                    <th class="cost summary-internalRate">{{ 'internalRate'|trans }}</th>
                    <th class="cost summary-rate">{{ 'rate'|trans }}</th>
                </tr>
                </thead>
                <tbody>
                {% set customer = null %}
                {% set customerDuration = 0 %}
                {% set customerRate = 0 %}
                {% set customerInternalRate = 0 %}
                {% set customerCurrency = null %}
                {% 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) %}
                        {% set durationNormal = customerDuration|duration(false) %}
                        {% set durationDecimal = customerDuration|duration(true) %}
                        <tr class="summary">
                            <td colspan="2">&nbsp;</td>
                            <td class="export-timeBudget"></td>
                            <td class="export-budget"></td>
                            <td class="totals duration summary-duration">
                                <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                            </td>
                            <td class="totals cost summary-internalRate">{{ customerInternalRate|money(customerCurrency) }}</td>
                            <td class="totals cost summary-rate">{{ customerRate|money(customerCurrency) }}</td>
                        </tr>
                        {% set customerCurrency = summary.currency %}
                        {% set customer = summary.customer %}
                        {% set customerDuration = 0 %}
                        {% set customerRate = 0 %}
                        {% set customerInternalRate = 0 %}
                    {% endif %}
                    <tr>
                        <td>{{ summary.customer }}</td>
                        <td>{{ summary.project }}</td>
                        <td class="center export-timeBudget">
                            {% if budgets[id] is defined and budgets[id].time_left > 0 %}
                                {% set durationNormal = budgets[id].time_left|duration(false) %}
                                {% set durationDecimal = budgets[id].time_left|duration(true) %}
                                <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                            {% endif %}
                        </td>
                        <td class="center export-budget">
                            {% if budgets[id] is defined and budgets[id].money_left > 0 %}
                                {{ budgets[id].money_left|money(summary.currency) }}
                            {% endif %}
                        </td>
                        <td class="duration summary-duration">
                            {% set durationNormal = summary.duration|duration(false) %}
                            {% set durationDecimal = summary.duration|duration(true) %}
                            <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                        </td>
                        <td class="cost summary-internalRate">{{ summary.rate_internal|money(summary.currency) }}</td>
                        <td class="cost summary-rate">{{ summary.rate|money(summary.currency) }}</td>
                    </tr>
                    {% set customerDuration = customerDuration + summary.duration %}
                    {% set customerRate = customerRate + summary.rate %}
                    {% set customerInternalRate = customerInternalRate + summary.rate_internal %}
                {% endfor %}
                {% if customer is not same as(null) %}
                    {% set durationNormal = customerDuration|duration(false) %}
                    {% set durationDecimal = customerDuration|duration(true) %}
                    <tr class="summary">
                        <td colspan="2">&nbsp;</td>
                        <td class="export-timeBudget"></td>
                        <td class="export-budget"></td>
                        <td class="totals duration summary-duration">
                            <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                        </td>
                        <td class="totals cost summary-internalRate">{{ customerInternalRate|money(customerCurrency) }}</td>
                        <td class="totals cost summary-rate">{{ customerRate|money(customerCurrency) }}</td>
                    </tr>
                {% endif %}
                </tbody>
            </table>

            {#
            ============================================
            SUMMARY: BY ACTIVITY
            ============================================
            #}

            <table class="items table table-sm table-bordered dataTable" id="summary-activity" style="display:none">
                <thead>
                <tr>
                    <th>{{ 'customer'|trans }}</th>
                    <th>{{ 'project'|trans }}</th>
                    <th>{{ 'activity'|trans }}</th>
                    <th class="center export-timeBudget">{{ 'timeBudget'|trans }}</th>
                    <th class="center export-budget">{{ 'budget'|trans }}</th>
                    <th class="duration summary-duration">{{ 'duration'|trans }}</th>
                    <th class="cost summary-internalRate">{{ 'internalRate'|trans }}</th>
                    <th class="cost summary-rate">{{ 'rate'|trans }}</th>
                </tr>
                </thead>
                <tbody>
                {% set customer = null %}
                {% set customerDuration = 0 %}
                {% set customerRate = 0 %}
                {% set customerInternalRate = 0 %}
                {% set customerCurrency = null %}
                {% 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="3"></td>
                            <td class="export-timeBudget"></td>
                            <td class="export-budget"></td>
                            <td class="totals duration summary-duration">
                                {% set durationNormal = customerDuration|duration(false) %}
                                {% set durationDecimal = customerDuration|duration(true) %}
                                <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                            </td>
                            <td class="totals cost summary-internalRate">{{ customerInternalRate|money(customerCurrency) }}</td>
                            <td class="totals cost summary-rate">{{ customerRate|money(customerCurrency) }}</td>
                        </tr>
                        {% set customerCurrency = summary.currency %}
                        {% set customer = summary.customer %}
                        {% set customerDuration = 0 %}
                        {% set customerRate = 0 %}
                        {% set customerInternalRate = 0 %}
                    {% endif %}
                    {% for activityId, activitySummary in summary.activities %}
                        <tr>
                            <td>{{ summary.customer }}</td>
                            <td>{{ summary.project }}</td>
                            <td>{{ activitySummary.activity }}</td>
                            {% if activity_budgets[id] is defined and activity_budgets[id][activityId] is defined %}
                                <td class="center export-timeBudget">
                                    {% if activity_budgets[id][activityId].time_left > 0 %}
                                        {% set durationNormal = activity_budgets[id][activityId].time_left|duration(false) %}
                                        {% set durationDecimal = activity_budgets[id][activityId].time_left|duration(true) %}
                                        <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                                    {% endif %}
                                </td>
                                <td class="center export-budget">
                                    {% if activity_budgets[id][activityId].money_left > 0 %}
                                        {{ activity_budgets[id][activityId].money_left|money(summary.currency) }}
                                    {% endif %}
                                </td>
                            {% else %}
                                <td class="center export-timeBudget"></td>
                                <td class="center export-budget"></td>
                            {% endif %}
                            <td class="duration summary-duration">
                                {% set durationNormal = activitySummary.duration|duration(false) %}
                                {% set durationDecimal = activitySummary.duration|duration(true) %}
                                <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                            </td>
                            <td class="cost summary-internalRate">{{ activitySummary.rate_internal|money(activitySummary.currency) }}</td>
                            <td class="cost summary-rate">{{ activitySummary.rate|money(activitySummary.currency) }}</td>
                        </tr>
                    {% endfor %}
                    {% set customerDuration = customerDuration + summary.duration %}
                    {% set customerRate = customerRate + summary.rate %}
                    {% set customerInternalRate = customerInternalRate + summary.rate_internal %}
                {% endfor %}
                {% if customer is not same as(null) %}
                    <tr class="summary">
                        <td colspan="3"></td>
                        <td class="export-timeBudget"></td>
                        <td class="export-budget"></td>
                        <td class="totals duration summary-duration">
                            {% set durationNormal = customerDuration|duration(false) %}
                            {% set durationDecimal = customerDuration|duration(true) %}
                            <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                        </td>
                        <td class="totals cost summary-internalRate">{{ customerInternalRate|money(customerCurrency) }}</td>
                        <td class="totals cost summary-rate">{{ customerRate|money(customerCurrency) }}</td>
                    </tr>
                {% endif %}
                </tbody>
            </table>
        </div>
    </div>

    <div class="row m-3" id="export-records">
        <div class="card p-0">
            <div class="card-header">
                <h3 class="card-title" id="doc-records" contenteditable="true" data-original="{{ 'export.full_list'|trans }}">{{ 'export.full_list'|trans }}</h3>
            </div>
            <table class="items table table-sm table-bordered dataTable">
                <thead>
                    <tr>
                        {% for columnId, visibility in columns %}
                        <th class="column column-{{ columnId }}" {% if not visibility %}style="display: none"{% endif %}>
                            {{ columnTitles[columnId]|default(columnId)|trans }}
                        </th>
                        {% endfor %}
                    </tr>
                </thead>
                <tbody>
                {% set timeWorked = 0 %}
                {% set rateTotal = 0 %}
                {% set rateInternalTotal = 0 %}
                {% set currency = false %}
                {% for entry in entries %}
                    {% set timeWorked = timeWorked + entry.duration %}
                    {% set rateTotal = rateTotal + entry.rate %}
                    {% if entry.internalRate is defined %}
                        {% set rateInternalTotal = rateInternalTotal + entry.internalRate %}
                    {% else %}
                        {% set rateInternalTotal = rateInternalTotal + entry.rate %}
                    {% endif %}
                    {% 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>
                        <td class="column-date text-nowrap" {% if not columns.date %}style="display: none"{% endif %}>
                            <span class="dateformat" data-short="{{ entry.begin|date_short }}" data-full="{{ entry.begin|date_time }}" data-time="{{ entry.begin|date_time }}">
                            {{ entry.begin|date_short }}
                            </span>
                        </td>
                        <td class="column-begin text-nowrap" {% if not columns.begin %}style="display: none"{% endif %}>
                            <span class="beginformat" data-short="{{ entry.begin|date_short }}" data-full="{{ entry.begin|date_time }}" data-time="{{ entry.begin|date_time }}" data-plain="{{ entry.begin|date_format('H:i') }}">
                            {{ entry.begin|date_format('H:i') }}
                            </span>
                        </td>
                        <td class="column-end text-nowrap" {% if not columns.end %}style="display: none"{% endif %}>
                            <span class="endformat" data-short="{{ entry.end|date_short }}" data-full="{{ entry.end|date_time }}" data-time="{{ entry.end|date_time }}" data-plain="{{ entry.end|date_format('H:i') }}">
                            {{ entry.end|date_format('H:i') }}
                            </span>
                        </td>
                        <td class="column-username" {% if not columns.username %}style="display: none"{% endif %}>
                            {{ widgets.username(entry.user) }}
                        </td>
                        <td class="column-customer" {% if not columns.customer %}style="display: none"{% endif %}>
                            {{ entry.project.customer.name }}
                        </td>
                        <td class="column-project" {% if not columns.project %}style="display: none"{% endif %}>{{ entry.project.name }}
                        </td>
                        <td class="column-activity" {% if not columns.activity %}style="display: none"{% endif %}>
                            {% if entry.activity is not null %}
                                {{ entry.activity.name }}
                            {% endif %}
                        </td>
                        <td class="column-description" {% if not columns.description %}style="display: none"{% endif %}>
                            {% if entry.description is not empty %}
                                {{ entry.description|desc2html }}
                            {% endif %}
                        </td>
                        <td class="column-exported" {% if not columns.exported %}style="display: none"{% endif %}>
                            {% if entry.exported %}
                                {{ 'entryState.exported'|trans }}
                            {% else %}
                                {{ 'entryState.not_exported'|trans }}
                            {% endif %}
                        </td>
                        <td class="column-tags" {% if not columns.tags %}style="display: none"{% endif %}>
                            {% if entry.tags is defined and entry.tags is not empty %}
                                {{ entry.tagsAsArray|join(', ') }}
                            {% endif %}
                        </td>
                        {% for id, field in timesheetMetaFields %}
                            <td class="column-{{ 't_' ~ field.name }} text-nowrap" {% if not columns['t_' ~ field.name] %}style="display: none"{% endif %}>
                                {{ tables.datatable_meta_column(entry, field) }}
                            </td>
                        {% endfor %}
                        {% for id, field in customerMetaFields %}
                            <td class="column-{{ 'c_' ~ field.name }} text-nowrap" {% if not columns['c_' ~ field.name] %}style="display: none"{% endif %}>
                                {{ tables.datatable_meta_column(entry.project.customer, field) }}
                            </td>
                        {% endfor %}
                        {% for id, field in projectMetaFields %}
                            <td class="column-{{ 'p_' ~ field.name }} text-nowrap" {% if not columns['p_' ~ field.name] %}style="display: none"{% endif %}>
                                {{ tables.datatable_meta_column(entry.project, field) }}
                            </td>
                        {% endfor %}
                        {% for id, field in activityMetaFields %}
                            <td class="column-{{ 'a_' ~ field.name }} text-nowrap" {% if not columns['a_' ~ field.name] %}style="display: none"{% endif %}>
                                {% if entry.activity is not null %}
                                    {{ tables.datatable_meta_column(entry.activity, field) }}
                                {% endif %}
                            </td>
                        {% endfor %}
                        {% for id, field in userPreferences %}
                            <td class="column-{{ 'u_' ~ field.name }} text-nowrap" {% if not columns['u_' ~ field.name] %}style="display: none"{% endif %}>
                                {% set metaField = entry.user.preference(field.name) %}
                                {% if not metaField is null and metaField.value is not null and metaField.value is not empty %}
                                    {{ widgets.form_type_value(field.type, metaField.value, entry.user) }}
                                {% endif %}
                            </td>
                        {% endfor %}
                        <td class="cost column-hourlyRate text-nowrap" {% if not columns.hourlyRate %}style="display: none"{% endif %}>
                            {{ entry.hourlyRate|money(entry.project.customer.currency) }}
                        </td>
                        <td class="cost column-fixedRate text-nowrap" {% if not columns.fixedRate %}style="display: none"{% endif %}>
                            {{ entry.fixedRate|money(entry.project.customer.currency) }}
                        </td>
                        <td class="duration column-duration text-nowrap" {% if not columns.duration %}style="display: none"{% endif %}>
                            {% set durationNormal = entry.duration|duration(false) %}
                            {% set durationDecimal = entry.duration|duration(true) %}
                            <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                        </td>
                        <td class="cost column-internalRate text-nowrap" {% if not columns.internalRate %}style="display: none"{% endif %}>
                            {% if entry.internalRate is defined %}
                                {{ entry.internalRate|money(entry.project.customer.currency) }}
                            {% else %}
                                {{ entry.rate|money(entry.project.customer.currency) }}
                            {% endif %}
                        </td>
                        <td class="cost column-rate text-nowrap" {% if not columns.rate %}style="display: none"{% endif %}>
                            {{ entry.rate|money(entry.project.customer.currency) }}
                        </td>
                    </tr>
                {% endfor %}
                {# leave in tbody instead of adding it to tfoot, as tfoot will be repeated on each page when printing #}
                <tr>
                    {% for id, visibility in columns %}
                        {% if id not in ['duration', 'rate', 'internalRate'] %}
                            <th class="column-{{ id }}" {% if not visibility %}style="display: none"{% endif %}></th>
                        {% endif %}
                    {% endfor %}
                    <th class="text-nowrap column-duration">
                        {% set durationNormal = timeWorked|duration(false) %}
                        {% set durationDecimal = timeWorked|duration(true) %}
                        <span class="duration-format" data-duration-decimal="{{ durationDecimal }}" data-duration="{{ durationNormal }}">{{ decimal ? durationDecimal : durationNormal }}</span>
                    </th>
                    <th class="text-nowrap column-internalRate">
                        {%- if currency is not null and currency is not same as(false) %}
                            {{ rateInternalTotal|money(currency) }}
                        {% else %}
                            {{ rateInternalTotal|money }}
                        {% endif -%}
                    </th>
                    <th class="text-nowrap column-rate">
                        {%- if currency is not null and currency is not same as(false) %}
                            {{ rateTotal|money(currency) }}
                        {% else %}
                            {{ rateTotal|money }}
                        {% endif -%}
                    </th>
                </tr>
                </tbody>
            </table>
        </div>
    </div>

{% endblock %}
