{% extends '@CpCreationVitiCore/admin/base.html.twig' %}

{% block content %}
    <div class="panel panel-default">
        <div class="panel-heading">
            Filtres
        </div>
        <div class="panel-body">
            <form method="get" action="{{ path('admin_order_invoices') }}">
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label for="search">Recherche</label>
                            <input
                                type="text"
                                name="search"
                                id="search"
                                class="form-control"
                                value="{{ filters.search_input }}"
                                placeholder="Numero, avoir, client, email, reference commande..."
                            >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="date_from">Du</label>
                            <input
                                type="date"
                                name="date_from"
                                id="date_from"
                                class="form-control"
                                value="{{ filters.date_from_input }}"
                            >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="date_to">Au</label>
                            <input
                                type="date"
                                name="date_to"
                                id="date_to"
                                class="form-control"
                                value="{{ filters.date_to_input }}"
                            >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="type">Type</label>
                            <select name="type" id="type" class="form-control">
                                <option value="all" {% if filters.type == 'all' %}selected{% endif %}>Tous</option>
                                <option value="invoice" {% if filters.type == 'invoice' %}selected{% endif %}>Factures</option>
                                <option value="credit" {% if filters.type == 'credit' %}selected{% endif %}>Avoirs</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <label>&nbsp;</label>
                        <div>
                            <button type="submit" class="btn btn-primary btn-block">
                                Filtrer
                            </button>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12">
                        <a href="{{ path('admin_order_invoices') }}" class="btn btn-default">
                            Reinitialiser
                        </a>
                        <a href="{{ path('admin_order_invoices_download_pdf', filters.query_parameters) }}" class="btn btn-success">
                            Télécharger les factures
                        </a>
                        <a href="{{ path('admin_order') }}" class="btn btn-info">
                            Retour aux commandes
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            Factures et avoirs ({{ items|length }})
        </div>
        <div class="table-responsive">
            <table class="table table-bordered dataTable" width="100%" cellspacing="0" data-order='[[1, "desc"]]'>
                <thead>
                <tr>
                    <th>Numero</th>
                    <th>Date</th>
                    <th>Type</th>
                    <th>Client</th>
                    <th>Email</th>
                    <th>Reference commande</th>
                    <th>Total</th>
                    <th>Actions</th>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th>Numero</th>
                    <th>Date</th>
                    <th>Type</th>
                    <th>Client</th>
                    <th>Email</th>
                    <th>Reference commande</th>
                    <th>Total</th>
                    <th>Actions</th>
                </tr>
                </tfoot>
                <tbody>
                {% for item in items %}
                    {% set cart = item.cart %}
                    {% set billing_address = cart ? (cart.invoiceAddress ?: cart.deliveryAddress) : null %}
                    {% set client_name = billing_address ? (billing_address.company ?: ((billing_address.firstname ~ ' ' ~ billing_address.lastname)|trim)) : '-' %}
                    {% set debit_reference = '-' %}
                    {% if item.numberDebit is not null and item.year is not null %}
                        {% set invoice_parts = [] %}
                        {% if invoice_prefix %}
                            {% set invoice_parts = invoice_parts|merge([invoice_prefix]) %}
                        {% endif %}
                        {% set invoice_parts = invoice_parts|merge([item.year, '%05d'|format(item.numberDebit)]) %}
                        {% set debit_reference = invoice_parts|join('-') %}
                    {% endif %}
                    {% set credit_reference = null %}
                    {% if item.credit and item.numberCredit is not null and item.year is not null %}
                        {% set credit_parts = [] %}
                        {% if invoice_prefix %}
                            {% set credit_parts = credit_parts|merge([invoice_prefix]) %}
                        {% endif %}
                        {% set credit_parts = credit_parts|merge(['Avoir', item.year, '%05d'|format(item.numberCredit)]) %}
                        {% set credit_reference = credit_parts|join('-') %}
                    {% endif %}
                    {% set invoice_date = item.credit and item.creditAt ? item.creditAt : (cart and cart.cartCheckout ? cart.cartCheckout.payedAt : item.createdAt) %}
                    {% set credit_total_ttc = item.credit and item.creditDetails and item.creditDetails.totals.total_ttc is defined ? item.creditDetails.totals.total_ttc : null %}
                    <tr>
                        <td>
                            {{ debit_reference }}
                            {% if credit_reference %}
                                <br><small class="text-muted">{{ credit_reference }}</small>
                            {% endif %}
                        </td>
                        <td data-order="{{ invoice_date ? invoice_date|date('U') : 0 }}">
                            {% if invoice_date %}
                                {{ invoice_date|date('d-m-Y') }}<br/>{{ invoice_date|date('H:i:s') }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>
                            <span class="label label-success">Facture</span>
                            {% if item.credit %}
                                <br><span class="label label-warning">Avoir</span>
                            {% endif %}
                        </td>
                        <td>{{ client_name ?: '-' }}</td>
                        <td>{{ cart ? (cart.email ?: '-') : '-' }}</td>
                        <td>{{ cart ? (cart.token ?: '-') : '-' }}</td>
                        <td>
                            {% if cart and cart.total is not null %}
                                {{ cart.total / 100 }} €
                                {% if credit_total_ttc is not null %}
                                    <br><small class="text-muted">{{ credit_total_ttc|price_format }} €</small>
                                {% endif %}
                            {% elseif credit_total_ttc is not null %}
                                {{ credit_total_ttc|price_format }} €
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>
                            {% if cart %}
                                <a href="{{ path('admin_order_invoice_documents_pdf', { id: cart.id }) }}" class="btn btn-default btn-sm">
                                    <i class="fas fa-file-pdf"></i> Facture
                                </a>
                                <a href="{{ path('admin_order_order', { id: cart.id }) }}" class="btn btn-info btn-sm">
                                    <i class="fas fa-search"></i> Détails
                                </a>
                            {% else %}
                                -
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}
