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

{% macro printProductRow(product) %}

    <tr class="lc-draggable">
        <td><i class="fa fa-fw fa-sort"></i></td>
        {{ macros.productField(2, product.title, 'title') }}
        {{ macros.productField(1, product.quantity, 'quantity') }}
        {{ macros.productField(2, product.unit, 'unit', 'unitWording') }}
        {{ macros.productField(2, product.buyingPrice, 'buyingPrice',false, '€', 'v-show="productFamily.behaviorPrice == \'by-piece\'"') }}
        {{ macros.productField(2, product.buyingPriceWithTax, 'buyingPriceWithTax',false, '€', 'v-show="productFamily.behaviorPrice == \'by-piece\'"') }}
        {{ macros.productField(2, product.buyingPriceByRefUnit, 'buyingPriceByRefUnit',false, '€', 'v-show="productFamily.behaviorPrice == \'by-reference-unit\'"') }}
        {{ macros.productField(2, product.buyingPriceByRefUnitWithTax, 'buyingPriceByRefUnitWithTax',false, '€', 'v-show="productFamily.behaviorPrice == \'by-reference-unit\'"') }}
        {{ macros.productField(2, product.multiplyingFactor, 'multiplyingFactor') }}
        {{ macros.productField(2, product.priceByRefUnit, 'priceByRefUnit',false, '€', 'v-show="productFamily.behaviorPrice == \'by-reference-unit\'"') }}
        {{ macros.productField(2, product.priceByRefUnitWithTax, 'priceByRefUnitWithTax',false, '€', 'v-show="productFamily.behaviorPrice == \'by-reference-unit\'"') }}

        <td colspan="2" v-show="productFamily.behaviorPrice == 'by-reference-unit'">
            {% verbatim %}{{ finalPriceWithTax }}{% endverbatim %}€
        </td>

        {{ macros.productField(2, product.price, 'price', false, '€', 'v-show="productFamily.behaviorPrice == \'by-piece\'"') }}
        {{ macros.productField(2, product.priceWithTax, 'priceWithTax', false, '€', 'v-show="productFamily.behaviorPrice == \'by-piece\'"') }}

        {{ macros.productField(1, product.availableQuantity, 'availableQuantity',false, '', 'v-show="productFamily.behaviorCountStock== \'by-product\'"') }}
        {{ macros.productField(1, product.availableQuantityDefault, 'availableQuantityDefault',false, '', 'v-show="productFamily.behaviorCountStock== \'by-product\'"') }}

        {{ macros.productField(2, product.propertyExpirationDate, 'propertyExpirationDate','propertyExpirationDateValue', '', 'v-show="productFamily.behaviorExpirationDate== \'by-product\'"') }}

        <td>
            <button type="button" class="btn-remove-product btn btn-default" @click="deleteProductForm()">
                <i class="fa fa-trash"></i>
            </button>
            {{ form_widget(product.position, {'attr' : {'class': "field-position"}}) }}
        </td>
    </tr>
{% endmacro %}

{% import _self as formMacros %}
<div class="row">
    {{ macros.startCard(12, 'ProductFamily.products', 'light', true) }}

        <table class="table datagrid sortable lc-sortable-products  products-collection-table"
               :data-index="formProductArray.length"
               data-prototype="{{ formMacros.printProductRow(form.products.vars.prototype)|e('html_attr') }}">
            <thead>
            <tr>
                <th>
                </th>
                <th colspan="2" class="string">
                    Titre
                </th>
                <th colspan="1" class="string ">
                    Quantité
                </th>
                <th colspan="2" class="quantity">
                    Unité
                </th>
                <th v-show="getBehaviorPrice() == 'by-reference-unit'" colspan="2" class="buyingPriceByRefUnit">
                    PA HT / ${ getUnitReference() }
                </th>
                <th v-show="getBehaviorPrice() == 'by-reference-unit'" colspan="2" class="priceByRefUnit">
                    PA TTC / ${ getUnitReference() }
                </th>

                <th colspan="2" class="price" v-show="getBehaviorPrice() =='by-piece'">
                    PA HT
                </th>
                <th colspan="2" class="price" v-show="getBehaviorPrice() =='by-piece'">
                    PA TTC
                </th>
                <th colspan="2" class="">
                    Coef
                </th>


                <th v-show="getBehaviorPrice() == 'by-reference-unit'" colspan="2" class="">
                    PV HT / ${ getUnitReference() }
                </th>
                <th v-show="getBehaviorPrice() == 'by-reference-unit'" colspan="2" class="price">
                    PV TTC / ${ getUnitReference() }
                </th>

                <th colspan="2" class="price" v-show="getBehaviorPrice() =='by-piece'">
                    PV HT
                </th>
                <th colspan="2" class="price">
                    PV TTC
                </th>

                <th v-show="behaviorCountStock== 'by-product'">
                    Stock
                </th>
                <th v-show="behaviorCountStock== 'by-product'">
                    Stock par défaut
                </th>
                <th colspan="2" class="" v-show="behaviorExpirationDate== 'by-product'">
                    <span style="text-transform: uppercase"> ${typeExpirationDate}</span>
                </th>
                <th class="">
                    Action
                </th>
            </tr>
            </thead>
            <tbody class="products-collection">

            <template v-for="(formProduct, key) in formProductArray">

                <product-form ref="productForm" v-bind:product-family="productFamily" :template="formProduct"
                              :key-form="key"></product-form>
            </template>

            </tbody>
            <tfoot>
            <th>
                Rappel
            </th>
            <th colspan="2" class="string">
                ${title}
            </th>
            <th colspan="1" class="string ">
                ${productFamily.quantity}
            </th>
            <th colspan="2" class="quantity">
                ${productFamily.unitWording}
            </th>
            <th v-show="getBehaviorPrice() == 'by-reference-unit'" colspan="2" class="buyingPriceByRefUnit">
                ${productFamily.buyingPriceByRefUnit}
            </th>
            <th v-show="getBehaviorPrice() == 'by-reference-unit'" colspan="2" class="priceByRefUnit">
                ${productFamily.buyingPriceByRefUnitWithTax}
            </th>

            <th colspan="2" class="price" v-show="getBehaviorPrice() =='by-piece'">
                ${productFamily.buyingPrice}
            </th>
            <th colspan="2" class="price" v-show="getBehaviorPrice() =='by-piece'">
                ${productFamily.buyingPriceWithTax}
            </th>
            <th colspan="2" class="">
                ${productFamily.multiplyingFactor}
            </th>


            <th v-show="getBehaviorPrice() == 'by-reference-unit'" colspan="2" class="">
                ${productFamily.priceByRefUnit}
            </th>
            <th v-show="getBehaviorPrice() == 'by-reference-unit'" colspan="2" class="price">
                ${productFamily.priceByRefUnitWithTax}
            </th>

            <th colspan="2" class="price" v-show="getBehaviorPrice() =='by-piece'">
                ${productFamily.price}
            </th>
            <th colspan="2" class="price">
                ${productFamily.priceWithTax}
            </th>

            <th v-show="behaviorCountStock== 'by-product'">
            </th>
            <th v-show="behaviorCountStock== 'by-product'">
            </th>
            <th colspan="2" class="" v-show="behaviorExpirationDate== 'by-product'">
                ${propertyExpirationDate}
            </th>
            <th class="">

            </th>
            </tfoot>
        </table>
        <button type="button" class="add_tag_link btn-add-product btn btn-default" @click="addProductForm"><span
                    class="fa fa-plus"></span> Ajouter une déclinaison
        </button>
        <div class="clearfix"></div>

        {{ macros.endCard() }}
    </div>
    {% do form.products.setRendered %}

    <script>
        window.productForm = new Array();
        window.formProductTemplate = new Array();

        {% for keyForm,i in sortableProductsField %}
        {% set product = form.products[i] %}

        window.productForm[{{ keyForm }}] = {
            {% if product.vars.value.title %}title: "{{ product.vars.value.title }}",{% endif %}
            {% if product.vars.value.quantity %}quantity: "{{ product.vars.value.quantity }}",{% endif %}
            {% if product.vars.value.unit %}unit: {{ product.vars.value.unit.id }},{% endif %}
            {% if product.vars.value.buyingPrice %}buyingPrice: parseFloat({{ product.vars.value.buyingPrice }}).toFixed(3),{% endif %}
            {% if product.vars.value.buyingPriceByRefUnit %}buyingPriceByRefUnit: parseFloat({{ product.vars.value.buyingPriceByRefUnit }}).toFixed(3),{% endif %}
            {% if product.vars.value.price %}price: parseFloat({{ product.vars.value.price }}).toFixed(3),{% endif %}
            {% if product.vars.value.priceByRefUnit %}priceByRefUnit: parseFloat({{ product.vars.value.priceByRefUnit }}).toFixed(3),{% endif %}
            {% if product.vars.value.availableQuantity %}availableQuantity: parseInt({{ product.vars.value.availableQuantity }}),{% endif %}
            {% if product.vars.value.availableQuantityDefault %}availableQuantityDefault: parseInt({{ product.vars.value.availableQuantityDefault }}),{% endif %}
            {% if product.vars.value.propertyExpirationDate %}propertyExpirationDate: "{{ product.vars.value.propertyExpirationDate }}",{% endif %}
            {#{% if product.vars.value.expirationDate %}expirationDate: "{{ product.vars.value.expirationDate|date('d/m/Y') }}"{% endif %}#}
        };
        window.formProductTemplate[{{ keyForm }}] = '{{ formMacros.printProductRow(product)|replace({"\n":' ', "\r":' ', "'" : "\\'"})|raw }}';
        {% endfor %}

    </script>

