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

{% block datatable_row %}
    <tr class="modal-ajax-form open-edit{% if entry.canceled %} text-body-secondary{% elseif entry.overdue %} bg-danger-lt{% endif %}" data-href="{{ path('admin_invoice_edit', {'id': entry.id}) }}">
        <td class="{{ tables.class(dataTable, 'avatar') }}">{{ widgets.user_avatar(entry.user) }}</td>
        <td class="{{ tables.class(dataTable, 'date') }}">{{ entry.createdAt|date_short }}</td>
        <td class="{{ tables.class(dataTable, 'user') }}">{{ widgets.label_user(entry.user) }}</td>
        <td class="{{ tables.class(dataTable, 'customer') }}">{{ widgets.label_customer(entry.customer) }}</td>
        <td class="{{ tables.class(dataTable, 'comment') }}">{{ entry.comment }}</td>
        {% for field in metaColumns %}
            <td class="{{ tables.class(dataTable, 'mf_' ~ field.name) }}">
                {{ tables.datatable_meta_column(entry, field) }}
            </td>
        {% endfor %}
        <td class="{{ tables.class(dataTable, 'invoice_number') }}">{{ widgets.label(entry.invoiceNumber, 'default') }}</td>
        <td class="{{ tables.class(dataTable, 'due_date') }}">{{ _self.invoice_due_date(entry) }}</td>
        <td class="{{ tables.class(dataTable, 'payment_date') }}">
            {% if entry.paymentDate and entry.paid %}
                {{ widgets.label(entry.paymentDate|date_short, 'primary') }}
            {% endif %}
        </td>
        <td class="{{ tables.class(dataTable, 'status') }}">{{ _self.invoice_status(entry) }}</td>
        <td class="{{ tables.class(dataTable, 'subtotal') }}">{{ entry.subtotal|money(entry.currency) }}</td>
        <td class="{{ tables.class(dataTable, 'tax') }}">{{ entry.tax|money(entry.currency) }}</td>
        <td class="{{ tables.class(dataTable, 'total_rate') }}">{{ entry.total|money(entry.currency) }}</td>
        <td class="{{ tables.class(dataTable, 'actions') }}">{{ actions.invoice(entry, 'index') }}</td>
    </tr>
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <div style="display: none" id="status-token" data-value="{{ csrf_token('invoice.status') }}"></div>
    <script>
        function kimaiInvoiceStatus(link)
        {
            const form = document.createElement('form');
            form.method = 'post';
            form.action = link.dataset['href'];

            const token = document.createElement('input');
            token.type = 'hidden';
            token.name = '_token';
            token.value = document.getElementById('status-token').dataset['value'];
            form.appendChild(token);

            document.body.appendChild(form);
            form.submit();

            return false;
        }

        {% if download is not null %}
        document.addEventListener('kimai.initialized', function() {
            location.href = '{{ path('admin_invoice_download', {'id': download.id}) }}';
        });
        {% endif %}
    </script>
{% endblock %}

{% macro invoice_status(invoice) %}
    {% from "macros/widgets.html.twig" import label %}
    {% set overdue = invoice.overdue %}
    {% if invoice.new and overdue %}
        {{ label('status.new'|trans, 'danger') }}
    {% elseif invoice.new %}
        {{ label('status.new'|trans, 'primary') }}
    {% elseif invoice.pending and overdue %}
        {{ label('status.pending'|trans, 'danger') }}
    {% elseif invoice.pending %}
        {{ label('status.pending'|trans, 'warning') }}
    {% elseif invoice.paid %}
        {{ label('status.paid'|trans, 'success') }}
    {% elseif invoice.canceled %}
        {{ label('status.canceled'|trans, 'gray') }}
    {% endif %}
{% endmacro %}

{% macro invoice_due_date(invoice) %}
    {% from "macros/widgets.html.twig" import label %}
    {% if invoice.canceled %}
        {{ label('status.canceled'|trans, 'gray') }}
    {% elseif invoice.overdue and not invoice.paid %}
        {{ label(invoice.dueDate|date_short, 'danger') }}
    {% else %}
        {{ label(invoice.dueDate|date_short, 'primary') }}
    {% endif %}
{% endmacro %}
