{% extends 'base.html.twig' %}
{% import "macros/widgets.html.twig" as widgets %}
{% import "macros/toolbar.html.twig" as toolbar %}
{% import "macros/datatables.html.twig" as tables %}
{% import "invoice/actions.html.twig" as actions %}

{% set columns = {
    'date': 'alwaysVisible',
    'user': 'hidden-xs hidden-sm',
    'description': 'hidden-xs hidden-sm',
    'unit_price': 'hidden-xs text-center',
    'amount': 'text-center',
    'duration': 'hidden-xs text-center',
    'total_rate': 'text-right alwaysVisible',
} %}

{% set tableName = 'invoice' %}

{% block page_title %}{{ 'invoice.title'|trans }}{% endblock %}
{% block page_subtitle %}{{ 'invoice.subtitle'|trans }}{% endblock %}
{% block page_actions %}{{ actions.invoices('index') }}{% endblock %}

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

{% block main %}

    {% if is_granted('create_invoice') %}
        {% embed '@AdminLTE/Widgets/box-widget.html.twig' %}
            {% form_theme form '@AdminLTE/layout/form-theme-horizontal.html.twig' %}
            {% block box_title %}{{ 'invoice.filter'|trans }}{% endblock %}
            {% block box_before %}{{ form_start(form) }}{% endblock %}
            {% block box_body %}
                {{ form_row(form.searchTerm) }}
                {{ form_row(form.daterange) }}
                {{ form_row(form.customer) }}
                {{ form_row(form.project) }}
                {{ form_row(form.activity) }}
                {{ form_row(form.users) }}
                {{ form_row(form.tags) }}
                {{ form_row(form.exported) }}
                {{ form_row(form.template) }}
                {{ form_row(form.markAsExported) }}
            {% endblock %}
            {% block box_footer%}
                {{ form_widget(form.create, {'attr': {'class': 'btn btn-success'}}) }}
                {{ form_widget(form.preview) }}
            {% endblock %}
            {% block box_after %}{{ form_end(form) }}{% endblock %}
        {% endembed %}
    {% else %}
        {{ widgets.callout('danger', 'http_error_403.suggestion'|trans({}, 'exceptions')) }}
    {% endif %}

    {% if preview %}
        {% if model.calculator is empty or model.calculator.entries is empty %}
            {{ widgets.callout('warning', 'error.no_entries_found') }}
        {% else %}
            {% set entries = model.calculator.entries %}
            {{ tables.data_table_header(tableName, columns) }}
            {% for entry in entries %}
                {% set amount = entry.amount %}
                {% set duration = entry.duration|duration_decimal ~ ' / ' ~ entry.duration|duration %}
                {% set rate = 0 %}
                {% if entry.fixedRate is not null %}
                    {% set rate = entry.fixedRate %}
                {% elseif entry.hourlyRate is not null %}
                    {% set rate = entry.hourlyRate %}
                {% endif %}
                <tr>
                    <td class="text-nowrap">{{ entry.begin|date_short }}</td>
                    <td class="{{ tables.data_table_column_class(tableName, columns, 'user') }}">{{ widgets.label_user(entry.user) }}</td>
                    <td class="{{ tables.data_table_column_class(tableName, columns, 'description') }} timesheet-description">
                        {% if entry.description is not empty %}
                            {{ entry.description|escape|desc2html }}
                        {% else %}
                            {{ entry.activity.name }} / {{ entry.project.name }}
                        {% endif %}
                    </td>
                    <td class="{{ tables.data_table_column_class(tableName, columns, 'unit_price') }} text-center">{{ rate|money(model.calculator.currency) }}</td>
                    <td class="{{ tables.data_table_column_class(tableName, columns, 'amount') }} text-center text-nowrap">{{ amount }}</td>
                    <td class="{{ tables.data_table_column_class(tableName, columns, 'duration') }} text-center text-nowrap">{{ duration }}</td>
                    <td class="text-right text-nowrap">{{ entry.rate|money(model.calculator.currency) }}</td>
                </tr>
            {% endfor %}
            {{ tables.data_table_footer(entries) }}
        {% endif %}
    {% endif %}

{% endblock %}
