{% trans_default_domain 'lcshop' %}


{% macro tableHead() %}
<table id="order-products-list" class="table table-striped">
    <thead>
    <tr>
        <th :class="editionMode ? '' : 'hidden'">

        </th>
        <th colspan="2">
            <span>Produits / Producteurs</span>
        </th>
        <th v-if="order.countComplementaryOrderShops > 0">Compléments</th>

        <th>
            <span>Prix HT à l'unité</span>
        </th>
        <th>
            <span>Prix TTC à l'unité </span>
        </th>
        <th>
            <span>Marge </span>
        </th>
        <th>
            <span>Disponibilité</span>
        </th>
        <th>
            <span>Quantité</span>
        </th>
        <th>
            <span>Total</span>
        </th>
        <th :class="editionMode ? '' : 'hidden'">
            Actions
        </th>
    </tr>
    </thead>
    {% endmacro %}


    {% macro products() %}
        <tbody>
        <template v-for="(orderProduct, key) in orderProducts">
            <order-product ref="orderProductBLOP" :order-product="orderProduct" :edition-mode="editionMode"
                           :template="templateTest"
                           :key-item="key"></order-product>
        </template>
        </tbody>
    {% endmacro %}


    {% macro productsTemplate(form_order_products=null) %}
        <tr :class=" orderProduct.isRedelivery ? 'redelivery order-product-item' : 'order-product-item'">
            <td :class="editionMode ? '' : 'hidden'">
                <input  type="checkbox" :value="orderProduct.id" class="order-product-checkbox" />
            </td>
            <td colspan="2">
                <a :href="orderProduct.editLink" target="_blank">
                <img :src="orderProduct.image" :alt="orderProduct.title" />
                {% verbatim %}{{orderProduct.title}}{% endverbatim %}
                <span v-show="orderProduct.hasRedelivery">
                 - <i class="fa fa-undo" data-toggle="tooltip" :title="orderProduct.redeliveryOrderShop" ></i>
                </span>

                <span v-show="orderProduct.isRedelivery">
                 - <i class="fa fa-undo" data-toggle="tooltip" title="Ce produit est une relivraison" ></i>
                </span>
                </a>
            </td>
            <td>
               <span v-if="orderProduct.complementaryReference"> {% verbatim %}{{orderProduct.complementaryReference}}{% endverbatim %}</span>
            </td>
            <td>
                {% verbatim %}{{orderProduct.price}}{% endverbatim %}€
            </td>
            <td>
                {% verbatim %}{{orderProduct.priceWithTax}}{% endverbatim %}€
            </td>
            <td>
                {% verbatim %}{{orderProduct.totalMargin}}{% endverbatim %}€ /
                {% verbatim %}{{orderProduct.marginPercent}}{% endverbatim %}%
            </td>
            <td>
                {% verbatim %}{{orderProduct.availableQuantity}}{% endverbatim %}
            </td>
            <td>
                {% if form_order_products is not null %}
                    <div :class="editionMode && orderProduct.isRedelivery ==false ? '' : 'hidden'">
                        {{ form_widget(form_order_products.orderProducts.vars.prototype.quantityOrder, {'attr' : {'ref': 'fieldQuantity', 'v-model' : 'orderProduct.quantityOrder', '@change' : 'updateOrderProducts'}}) }}
                        {{ form_widget(form_order_products.orderProducts.vars.prototype.product, {'attr' : {'ref' : 'fieldProduct', 'v-model' : 'orderProduct.product'}}) }}
                        {#{{ form_widget(form_order_products.orderProducts.vars.prototype.id, {'attr' : {'ref' : 'fieldId', 'v-model' : 'orderProduct.id'}}) }}#}
                        <button type="button" class="btn-remove-product btn btn-default" @click="deleteOrderProduct()">
                            <i class="fa fa-trash"></i>
                        </button>
                    </div>
                    <div :class="editionMode && orderProduct.isRedelivery ==false ? 'hidden' : ''">
                        {% verbatim %}{{ orderProduct.quantityOrder }}{% endverbatim %}
                    </div>
                {% else %}
                    {% verbatim %}{{ orderProduct.quantityOrder }}{% endverbatim %}
                {% endif %}
            </td>
            <td>
                {% verbatim %}{{orderProduct.totalWithTaxAndReduction}}{% endverbatim %}€
            </td>
            <td :class="editionMode ? '' : 'hidden'">
                <div class="dropdown"  :class="editionMode ? '' : 'hidden'">
                    <button class="btn-sm btn-info dropdown-toggle" type="button" id="dropdownMenu" data-toggle="dropdown" aria-expanded="false">
                        Actions
                    </button>
                    <div class="dropdown-menu" aria-labelledby="dropdownMenu">
                        <button v-show="modalExist('#modal-add-redelivery-order-product') && orderProduct.hasRedelivery == false" type="button" class="dropdown-item" @click="modalAddRedeliveryOrderProduct">
                            Générer une relivraison
                        </button>

                        <button v-show="modalExist('#modal-add-reduction-credit')" type="button" class="dropdown-item"  @click="modalAddReductionCredit" >
                            Générer un avoir
                        </button>
                    </div>
                </div>
            </td>
        </tr>

    {% endmacro %}

    {% macro tableTotal() %}

</table>

    <div class="clearfix"></div>

    <div class="row">
        <div class="col-6">
            <div class="row">
                <div class="col-12">
                    {{ _self.order_modal_button('#modal-add-product-to-order', 'btn-success',  "action.order.addOrderProduct", 'plus') }}
                    {{ _self.order_modal_button('#modal-add-order-reduction-cart', 'btn-warning',  "action.order.addReductionCart", 'shopping-basket') }}
                    {{ _self.order_modal_button('#modal-add-order-reduction-credit', 'btn-warning',  "action.order.addReductionCredit", 'backspace') }}

                </div>

            </div>
        </div>

        <div class="col-5">
            <div class="table-responsive">
                <table class="table">
                    <tbody>
                    <tr>
                        <th>Total produits TTC</th>
                        <td>${order.totalOrderProductsWithTax}€</td>
                    </tr>
                    <template v-for="(orderReductionCart, key) in order.orderReductionCarts">
                        <tr>
                            <th>
                                ${orderReductionCart.title}
                                <button v-show="editionMode && modalExist('#modal-delete-reduction-cart')" type="button"
                                        class="btn-sm btn-danger"
                                        @click="modalDeleteOrderReductionCart(orderReductionCart.id)">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </th>
                            <td>${orderReductionCart.amount}€</td>
                        </tr>
                    </template>
                    <template v-for="(orderReductionCredit, key) in order.orderReductionCredits">
                        <tr>
                            <th>${orderReductionCredit.title}

                                <button v-show="editionMode && modalExist('#modal-delete-reduction-credit')"
                                        type="button" class="btn-sm btn-danger"
                                        @click="modalDeleteOrderReductionCredit(orderReductionCredit.id)">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </th>
                            <td>${orderReductionCredit.amount}€</td>
                        </tr>
                    </template>
                    <tr>
                        <th>Total produits après réductions TTC</th>
                        <td>${order.totalOrderProductsWithTaxAndReductions}€</td>
                    </tr>
                    <tr>
                        <th>Total marge produits</th>
                        <td>${order.totalMargin}€ <br /> ${order.totalMarginPercent}%</td>
                    </tr>

                    <tr>
                        <th>Frais de livraisons TTC</th>
                        <td>${order.deliveryPriceWithTaxAndReduction}€</td>
                    </tr>
                    <tr>
                        <th>Total TTC</th>
                        <td>${order.totalWithTax}€</td>
                    </tr>

                    </tbody>
                </table>
            </div>
        </div>
        <div class="col"></div>
        <div class="col-12">
            {#<button v-show="order.user" type="button" class="btn btn-danger" @click="switchEditionMode">
                <template v-if="editionMode">
                    <i class="fa fa-eye"></i> {{ "action.order.viewMode"|trans }}
                </template>
                <template v-else>
                    <i class="fa fa-pen"></i> {{ "action.order.editionMode"|trans }}
                </template>
            </button>#}
            {{ _self.order_modal_button('#modal-order-send-payment-link', 'btn-info float-right',  "action.order.sendPaymentLink", 'credit-card') }}

        </div>
    </div>
{% endmacro %}


{% macro box_user_info() %}
    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}
    {% set value = ' <a v-if="order.user" :href="order.userLink" target="_blank" v-html="order.user"></a>
                <span v-else v-html="order.visitor"></span>' %}
    {{ macros.box_info('bg-info', 'user',"field.default.user"|trans({}, 'lcshop'), value) }}

{% endmacro box_user_info %}




{% macro box_reference() %}
    {% embed '@LcShop/backend/default/block/embed_box.twig' %}
        {% import '@LcShop/backend/order/macros.html.twig' as order_macros %}
        {% trans_default_domain 'lcshop' %}
        {% block class %}bg-info{% endblock %}
        {% block icon %}credit-card{% endblock %}
        {% block label %}{{ "field.OrderShop.reference"|trans({}, 'lcshop')}}{% endblock %}
        {% block value %}
            ${order.reference}<br />
            <span v-if="order.countComplementaryOrderShops > 0">
                ${order.countComplementaryOrderShops} commande(s) complémentaire(s)
            </span>
        {% endblock %}
        {% block button %}
            {{ order_macros.order_modal_button('#modal-order-delivery-address') }}
        {% endblock %}
    {% endembed %}
{% endmacro %}


{% macro box_validation_date() %}
    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}
    {% set value = '<span class="info-box-number" v-if="order.createdAt"> ${order.createdAt}</span>
            <span class="info-box-number" v-else> ${order.createdAt}</span>' %}
    {{ macros.box_info('bg-warning', 'calendar',"field.default.date"|trans({}, 'lcshop'), value) }}

{% endmacro %}


{% macro box_total_order() %}
    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}

    {{ macros.box_info('bg-danger', 'euro-sign',"field.OrderShop.total"|trans({}, 'lcshop'), ' ${order.totalWithTax} €') }}

{% endmacro %}


{% macro box_address_invoice() %}
    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}

    {% set value = '<span v-if="order.invoiceAddress!=false">
        <address v-html="order.invoiceAddress"></address>
        </span>
        <span v-else>
            Aucune adresse de facturation défini.
        </span>' %}
    {{ macros.box_info('bg-info', 'address-card',"field.default.invoiceAddress"|trans({}, 'lcshop'), value , _self.order_modal_button('#modal-order-invoice-address')) }}

{% endmacro %}

{% macro box_main_order() %}
    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}
    {% set value = '${order.mainOrder.reference} <a :href="order.mainOrder.link">[Afficher]</a>' %}
    {{ macros.box_info('bg-danger', 'link',"field.OrderShop.mainOrder"|trans({}, 'lcshop'), value) }}

{% endmacro %}

{% macro box_address_delivery() %}
    {% embed '@LcShop/backend/default/block/embed_box.twig' %}
        {% import '@LcShop/backend/order/macros.html.twig' as order_macros %}
        {% trans_default_domain 'lcshop' %}
        {% block class %}bg-success{% endblock %}
        {% block icon %}map-marked-alt{% endblock %}
        {% block label %}{{ "field.default.deliveryAddress"|trans({}, 'lcshop')}}{% endblock %}
        {% block value %}
            <div v-if="order.deliveryAddress">
                <address v-html="order.deliveryAddress">
                </address>
            </div>
            <div v-else>
                Aucun lieu de livraison défini.
            </div>
        {% endblock %}
        {% block button %}
            {{ order_macros.order_modal_button('#modal-order-delivery-address') }}
        {% endblock %}
    {% endembed %}
{% endmacro %}

{% macro box_delivery_slot() %}
    {% embed '@LcShop/backend/default/block/embed_box.twig' %}
        {% import '@LcShop/backend/order/macros.html.twig' as order_macros %}
        {% trans_default_domain 'lcshop' %}
        {% block class %}bg-success{% endblock %}
        {% block icon %}clock{% endblock %}
        {% block label %}{{ "field.default.deliveryAvailabilty"|trans({}, 'lcshop') }}{% endblock %}
        {% block value %}
            <div v-if="order.deliveryType == 'home'">
                <div v-html="order.deliveryAvailabilityZone != null ? order.deliveryAvailabilityZone : order.deliveryDate">
                </div>
            </div>
            <div v-else-if="order.deliveryType == 'point-sale'">
                <div v-html="order.deliveryAvailabilityPointSale != null ? order.deliveryAvailabilityPointSale : order.deliveryDate">
                </div>
            </div>
            <div v-else>
                Aucun créneau défini.
            </div>
        {% endblock %}
        {% block button %}
            {{ order_macros.order_modal_button('#modal-order-delivery-availability') }}
        {% endblock %}
    {% endembed %}
{% endmacro %}



{% macro box_payments() %}
    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}
    {% set tools = '<div v-show="order.isOrderPaid">
                <h5>
                    <span class="badge badge-success">
                        <i class="icon fas fa-check"></i> Commande soldé
                    </span>
                </h5>
            </div>
            <div v-show="order.isOrderPaid==false" >
                <h5>
                    <span class="badge badge-error">
                        <i class="icon fas fa-check"></i> Commande non soldé
                    </span>
                </h5>
            </div>' %}

    {{ macros.card_start("OrderShop.payment", 'danger card-outline', false, tools) }}
        <table class="table table-striped" style="margin-bottom: 15px">
            <tbody>
            <thead>
            <tr>
                <th v-if="order.countComplementaryOrderShops > 0">Référence</th>
                <th>Mode de règlement</th>
                <th>Date</th>
                <th>Montant</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            <template v-for="(orderPayment, i) in order.orderPayments">
                <tr>
                    <td v-if="order.countComplementaryOrderShops > 0">
                        ${orderPayment.orderReference}
                    </td>
                    <td>${orderPayment.meanPaymentText}</td>
                    <td>${orderPayment.paidAtText}</td>
                    <td>${orderPayment.amount}</td>

                    <td>
                        <button v-show="orderPayment.editable && editionMode && modalExist('#modal-delete-order-payment')" class="btn-sm btn-info" type="button" @click="editOrderPayment(orderPayment.id)">
                            <i class="fa fa-pen"></i>
                        </button>
                        <button v-show="orderPayment.editable && editionMode && modalExist('#modal-delete-order-payment')"
                                type="button" class="btn-sm btn-danger"
                                @click="modalDeleteOrderPayment(orderPayment.id)">
                            <i class="fa fa-trash"></i>
                        </button>
                    </td>
                </tr>
            </template>
            </tbody>
        </table>
        <div class="col-11">
            {{ _self.order_modal_button('#modal-order-payment', 'btn-info',  "action.order.addOrderPayment") }}

            <strong class="float-right">Total règlement : ${order.totalOrderPaid} €</strong>

            {#TODO: afficher si la commande est règlé et afficher une alerte si le montant des paiments est supérieur au montant total de la commande#}
        </div>
    {{ macros.card_end() }}
{% endmacro %}



{% macro box_documents() %}
    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}

    {{ macros.card_start("OrderShop.documents", 'primary card-outline') }}
    <table class="table table-striped">
        <tbody>
        <thead>
        <tr>
            <th v-if="order.countComplementaryOrderShops > 0">Référence</th>
            <th>Date</th>
            <th>Type</th>
            <th>Numéro</th>
            <th>Actions</th>
        </tr>
        </thead>
        <tbody>
        <template v-for="(orderDocument, i) in order.orderDocuments">
            <tr>
                <td v-if="order.countComplementaryOrderShops > 0">
                    ${orderDocument.orderReference}
                </td>
                <td>${orderDocument.date}</td>
                <td>Facture</td>
                <td>${orderDocument.reference}</td>
                <td>
                    <a :href="'./?entity=Document&action=downloadInvoice&id='+orderDocument.id" class="btn-sm btn-default">Télécharger</a>
                </td>
            </tr>
        </template>
        </tbody>
    </table>

    {{ macros.card_end() }}
{% endmacro %}




{% macro box_complementary() %}
    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}

    {{ macros.card_start("OrderShop.complementary", 'primary card-outline') }}
    <table class="table table-striped">
        <tbody>
        <thead>
        <tr>
            <th>Réference</th>
            <th>Date</th>
            <th>total</th>
            <th>Actions</th>
        </tr>
        </thead>
        <tbody>
        <template v-for="(complementaryOrderShop, i) in order.complementaryOrderShops">
            <tr>
                <td>${complementaryOrderShop.reference}</td>
                <td>${complementaryOrderShop.date}</td>
                <td>${complementaryOrderShop.total}</td>
                <td>
                    <a :href="complementaryOrderShop.link" class="btn-sm btn-default">Afficher</a>
                </td>
            </tr>
        </template>
        </tbody>
    </table>

    {{ macros.card_end() }}
{% endmacro %}



{% macro box_tickets() %}
    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}

    {{ macros.card_start("OrderShop.tickets", 'warning card-outline') }}
    <table class="table table-striped">
        <tbody>
        <thead>
        <tr>
            <th>Date</th>
            <th>Status</th>
            <th>Subject</th>
            <th>Actions</th>
        </tr>
        </thead>
        <tbody>
        <template v-for="(ticket, i) in order.tickets">
            <tr>
                <td>${ticket.date}</td>
                <td>${ticket.status}</td>
                <td>${ticket.subject}</td>
                <td>
                    <a :href="ticket.link" class="btn-sm btn-default">Afficher</a>
                </td>
            </tr>
        </template>
        </tbody>
    </table>

    {{ macros.card_end() }}
{% endmacro %}


{% macro box_status() %}
    {% embed '@LcShop/backend/default/block/embed_box.twig' %}
        {% import '@LcShop/backend/order/macros.html.twig' as order_macros %}
        {% trans_default_domain 'lcshop' %}
        {% block class %}bg-success{% endblock %}
        {% block icon %}info{% endblock %}
        {% block label %}{{ "field.OrderShop.status"|trans({}, 'lcshop')}}{% endblock %}
        {% block value %}
            <strong> ${order.orderStatus}</strong>
        {% endblock %}
        {% block button %}
            {{ order_macros.order_modal_button('#modal-order-status') }}
        {% endblock %}
    {% endembed %}
{% endmacro %}

{% macro order_modal_button(modalId, class="btn-primary",  trad="action.edit", icon=false) %}

    <button v-show="editionMode && modalExist('{{ modalId }}')" type="button" class="btn-sm  {{ class }}"
            data-toggle="modal"
            data-target="{{ modalId }}">
        {% if icon %}
            <i class="fa fa-{{ icon }}"></i>
        {% endif %}
        {{ trad|trans }}
    </button>

{% endmacro order_modal_button %}
