{% import "macros/widgets.html.twig" as widgets %}
{% extends 'invoice/layout.html.twig' %}

{% block invoice %}
    {% set isDecimal = model.template.decimalDuration|default(false) %}
    <div class="row">
        <div class="col-xs-12">
            <h2 class="page-header">
               <span contenteditable="true">{{ model.template.title }}</span>
            </h2>
        </div>
    </div>

    <div class="row">
        <div class="col-xs-12">
            <table class="table no-border table-condensed">
                <tr>
                    <th>{{ 'invoice.from'|trans }}</th>
                    <td contenteditable="true">
                        {% if model.query.user is not empty %}
                            {{ widgets.username(model.query.user) }}
                        {% else %}
                            {{ model.template.company }}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <th>{{ 'label.date'|trans }}</th>
                    <td contenteditable="true">
                        {% if model.query.begin|date('m') != model.query.end|date('m') or model.query.begin|date('Y') != model.query.end|date('Y') %}
                            {{ model.query.begin|date_short }} - {{ model.query.end|date_short }}
                        {% else %}
                            {{ model.query.end|month_name }} {{ model.query.end|date('Y') }}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <th>{{ 'label.customer'|trans }}</th>
                    <td contenteditable="true">
                        {% if model.customer.number is not empty %}[{{ model.customer.number }}]{% endif %}
                        {{ model.customer.name }}{% if model.customer.contact is not empty %} / {{ model.customer.contact }}{% endif %}
                    </td>
                </tr>
                {% if model.query.project is not empty %}
                    <tr>
                        <th>{{ 'label.project'|trans }}</th>
                        <td contenteditable="true">
                            {{ model.query.project.name }}
                            {% if model.query.project.orderNumber is not empty %}
                                ({{ 'label.orderNumber'|trans }}: {{ model.query.project.orderNumber }})
                            {% endif %}
                        </td>
                    </tr>
                {% endif %}
                {% if model.query.activity is not empty %}
                    <tr>
                        <th>{{ 'label.activity'|trans }}</th>
                        <td contenteditable="true">
                            {{ model.query.activity.name }}
                        </td>
                    </tr>
                {% endif %}
            </table>
        </div>
    </div>

    <div class="row invoice-items">
        <div class="col-xs-12 table-responsive">
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>{{ 'label.date'|trans }}</th>
                    {% if model.query.user is empty %}
                    <th>{{ 'label.user'|trans }}</th>
                    {% endif %}
                    <th>{{ 'label.activity'|trans }}</th>
                    <th class="text-right">{{ 'label.hours'|trans }}</th>
                </tr>
                </thead>
                <tbody>
                {% for entry in model.calculator.entries %}
                <tr>
                    <td>{{ entry.begin|date_short }}</td>
                    {% if model.query.user is empty %}
                        <td>{{ widgets.username(entry.user) }}</td>
                    {% endif %}
                    <td contenteditable="true">
                        {% if entry.description is not empty %}
                            {{ entry.description|nl2br }}
                        {% else %}
                            {% if entry.activity is not null %}{{ entry.activity.name }} / {% endif %}{{ entry.project.name }}
                        {% endif %}
                    </td>
                    <td class="text-right text-nowrap">{{ entry.duration|duration(isDecimal) }}</td>
                </tr>
                {% endfor %}
                </tbody>
                <tfoot>
                <tr>
                    <th></th>
                    {% if model.query.user is empty %}
                    <th></th>
                    {% endif %}
                    <th>{{ 'invoice.total_working_time'|trans }}</th>
                    <th class="text-right text-nowrap">{{ model.calculator.timeWorked|duration(isDecimal) }}</th>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="row">
        <div class="col-xs-12">
            {% if model.template.paymentTerms is not empty %}
                <p class="lead">{{ 'label.payment_terms'|trans }}</p>

                <p class="text-muted well well-sm no-shadow" contenteditable="true" style="margin-bottom: 100px">
                    {{ model.template.paymentTerms|trim|nl2br }}
                </p>
            {% endif %}

            <div class="table-responsive">
                <table class="table">
                    <tbody>
                    <tr>
                        <th style="padding-bottom: 60px">{{ 'invoice.signature_user'|trans }}</th>
                    </tr>
                    <tr>
                        <th>{{ 'invoice.signature_customer'|trans }}</th>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
{% endblock %}