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

{% block table %}
    <script>
        window.mixinUnitValues = {
            unitsList: {{ getUnitsList()|json_encode|raw }}
        };
        window.mixinPriceValues = {
            taxRatesList: {{ getTaxRatesList()|json_encode|raw }}
        };
        window.appProductFamilyValues = {};
        window.productUnitPriceValues = {};
        window.productForm ={};
        window.formProductTemplate = {};

    </script>

    <ul id="product-family-advanced-types-list">
        {% for i,advancedType in advancedTypes %}
            {% set formValues = advancedType.vars.value %}
            {% set productFamily = paginator.currentPageResults[i] %}
            <script>
                window.appProductFamilyValues[{{ i }}] = {
                    title: "{{ productFamily.title }}",
                    behaviorCountStock: "{{ productFamily.behaviorCountStock }}",
                    behaviorStockWeek: "{{ productFamily.behaviorStockWeek }}",
                    availableQuantity: "{{ productFamily.availableQuantity }}",
                    availableQuantityDefault: "{{ productFamily.availableQuantityDefault }}",
                    {% if productFamily.propertyNoveltyExpirationDate %}propertyNoveltyExpirationDate: {{ productFamily.propertyNoveltyExpirationDate }},{% endif %}
                    typeExpirationDate: "{{ productFamily.typeExpirationDate }}",
                    behaviorExpirationDate: "{{ productFamily.behaviorExpirationDate }}",
                    propertyExpirationDate: "{{ productFamily.propertyExpirationDate }}",
                    {% if productFamily.activeProducts %}activeProducts: {{ productFamily.activeProducts }},{% endif %}
                };

                window.productUnitPriceValues[{{ i }}] = {
                    {% if productFamily.activeProducts %}activeProducts: {{ productFamily.activeProducts }},{% endif %}
                    behaviorPrice: "{{ productFamily.behaviorPrice }}",
                    unit: parseInt({{ productFamily.unit.id }}),
                    {% if productFamily.taxRate %}taxRate: parseInt({{ productFamily.taxRate.id }}),{% endif %}
                    {% if productFamily.supplierTaxRate %}supplierTaxRate: parseInt({{ productFamily.supplierTaxRate.id }}),{% endif %}
                    quantity: "{{ productFamily.quantity }}",
                    price: parseFloat({{ productFamily.price }}).toFixed(3),
                    priceByRefUnit: parseFloat({{ productFamily.priceByRefUnit }}).toFixed(3),
                    buyingPrice: parseFloat({{ productFamily.buyingPrice }}).toFixed(3),
                    buyingPriceByRefUnit: parseFloat({{ productFamily.buyingPriceByRefUnit }}).toFixed(3),
                }
            </script>

            <li class="product-family-advanced-type-item">
                <div id="product-family-advanced-type-{{ i }}" class="product-family-advanced-type">
                    {{ form_start(advancedType, {"attr": {"ref": 'advanced-type'}}) }}

                    <product-unit-price ref="productUnitPrice" inline-template key-form="productfamily">
                        <div class="row">
                            {{ form_row(advancedType.unit, {"attr":{'v-model': 'unit', '@change': "unitUpdated"}}) }}


                            <div class="input-group">
                                {{ form_widget(advancedType.quantity, {'attr' : {'v-model': 'quantity', '@change': "quantityUpdated"}}) }}
                                <div class="input-group-append">
                                    <span class="input-group-text">${ unitWording }</span>
                                </div>
                            </div>

                            {{ macros.priceField(advancedType.buyingPrice, advancedType.buyingPriceWithTax, 'buyingPrice', 'by-piece') }}

                            {{ macros.priceField(advancedType.buyingPriceByRefUnit, advancedType.buyingPriceByRefUnitWithTax, 'buyingPriceByRefUnit', 'by-reference-unit') }}


                            <div class="input-group">
                                {{ form_widget(advancedType.multiplyingFactor, {'attr': {'v-model':'multiplyingFactor', '@change' : 'multiplyingFactorUpdated'}}) }}
                                <div class="input-group-append">
                                    <span class="input-group-text">X</span>
                                </div>
                            </div>

                            {{ macros.priceField(advancedType.price, advancedType.priceWithTax, 'price', 'by-piece') }}
                            {{ macros.priceField(advancedType.priceByRefUnit, advancedType.priceByRefUnitWithTax, 'priceByRefUnit', 'by-reference-unit') }}

                        </div>
                    </product-unit-price>


                        <table class="table datagrid sortable lc-sortable-products  products-collection-table"
                               :data-index="formProducts.length"
                               data-prototype="{{ product_family_macros.product_row(advancedType.products.vars.prototype)|e('html_attr') }}">
                            <thead>
                            <tr>
                                <th>
                                </th>
                                <th colspan="4" class="string">
                                    Titre
                                </th>
                                <th colspan="2" class="string ">
                                    Quantité
                                </th>
                                <th colspan="2" class="quantity">
                                    Unité
                                </th>
                                <th v-show="getBehaviorPrice() == '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT') }}'"
                                    colspan="3" class="buyingPriceByRefUnit">
                                    PA HT / ${ getUnitReference() }
                                </th>
                                <th v-show="getBehaviorPrice() == '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT') }}'"
                                    colspan="3" class="priceByRefUnit">
                                    PA TTC / ${ getUnitReference() }
                                </th>

                                <th colspan="3" class="price"
                                    v-show="getBehaviorPrice() =='{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_PIECE') }}'">
                                    PA HT
                                </th>
                                <th colspan="3" class="price">
                                    PA TTC
                                </th>
                                <th colspan="3" class="">
                                    Coef
                                </th>


                                <th v-show="getBehaviorPrice() == '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT') }}'"
                                    colspan="3" class="">
                                    PV HT / ${ getUnitReference() }
                                </th>
                                <th v-show="getBehaviorPrice() == '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT') }}'"
                                    colspan="3" class="price">
                                    PV TTC / ${ getUnitReference() }
                                </th>

                                <th colspan="3" class="price"
                                    v-show="getBehaviorPrice() =='{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_PIECE') }}'">
                                    PV HT
                                </th>
                                <th colspan="3" class="price">
                                    PV TTC
                                </th>
                                <th colspan="2">
                                    Marge HT
                                </th>

                                <th colspan="2" class=""
                                    v-show="behaviorExpirationDate== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT') }}'">
                                    <span style="text-transform: uppercase"> ${typeExpirationDate}</span>
                                </th>
                                <th colspan="2"
                                    v-show="behaviorCountStock== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT') }}'">
                                    Stock
                                </th>
                                <th colspan="2"
                                    v-show="behaviorCountStock== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT') }}' && behaviorStockWeek!= '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_STOCK_WEEK_NON_RENEWABLE') }}'">
                                    Stock par défaut
                                </th>
                                <th colspan="2"
                                    v-show="behaviorCountStock== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT') }}' || behaviorCountStock== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE') }}'">
                                    Semaine / Commandés
                                </th>

                                <th colspan="2" class="">
                                    Action
                                </th>
                            </tr>
                            </thead>
                            <tbody class="products-collection">

                            <template>
                                <product-form v-for="(formProduct, blop) in formProducts" v-bind:key="formProductKey[blop]"
                                              :key-form="formProductKey[blop]" ref="productForm" v-bind:product-family="productFamily"
                                              :template="formProduct"></product-form>
                            </template>
                            </tbody>
                            <tfoot>
                            <th>
                                Rappel
                            </th>
                            <th colspan="4" class="string">
                                ${title}
                            </th>
                            <th colspan="2" class="string ">
                                ${productFamily.quantity}
                            </th>
                            <th colspan="2" class="quantity">
                                ${productFamily.unitWording}
                            </th>
                            <th v-show="getBehaviorPrice() == '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT') }}'"
                                colspan="3" class="buyingPriceByRefUnit">
                                ${productFamily.buyingPriceByRefUnit}
                            </th>
                            <th v-show="getBehaviorPrice() == '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT') }}'"
                                colspan="3" class="priceByRefUnit">
                                ${productFamily.buyingPriceByRefUnitWithTax}
                            </th>

                            <th colspan="3" class="price"
                                v-show="getBehaviorPrice() =='{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_PIECE') }}'">
                                ${productFamily.buyingPrice}
                            </th>
                            <th colspan="3" class="price">
                                ${productFamily.buyingPriceWithTax}
                            </th>
                            <th colspan="3" class="">
                                ${productFamily.multiplyingFactor}
                            </th>


                            <th v-show="getBehaviorPrice() == '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT') }}'"
                                colspan="3" class="">
                                ${productFamily.priceByRefUnit}
                            </th>
                            <th v-show="getBehaviorPrice() == '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT') }}'"
                                colspan="3" class="price">
                                ${productFamily.priceByRefUnitWithTax}
                            </th>

                            <th colspan="3" class="price"
                                v-show="getBehaviorPrice() =='{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_PRICE_BY_PIECE') }}'">
                                ${productFamily.price}
                            </th>
                            <th colspan="3" class="price">
                                ${productFamily.priceWithTax}
                            </th>
                            <th colspan="2" class="price">
                                ${productFamily.marginProfit}€<br/>
                                ${productFamily.marginProfitPercent}%
                            </td>

                            <th colspan="2" class=""
                                v-show="behaviorExpirationDate== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT') }}'">
                                ${propertyExpirationDate}
                            </th>

                            <th colspan="2"
                                v-show="behaviorCountStock== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT') }}'">
                            </th>
                            <th colspan="2"
                                v-show="behaviorCountStock== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT') }}'  && behaviorStockWeek!= '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_STOCK_WEEK_NON_RENEWABLE') }}'">
                            </th>
                            <th colspan="2"
                                v-show="behaviorCountStock== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT') }}' || behaviorCountStock== '{{ constant('Lc\\ShopBundle\\Model\\ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE') }}'">
                            </th>

                            <th colspan="2" 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>
                        <p>
                            <strong>Aide à l'utilisation - Raccourci clavier</strong>
                        <ul>
                            <li><strong>TAB</strong> : Champ suivant</li>
                            <li><strong>SHIFT + TAB</strong> : Champ précédent</li>
                            <li><strong>FLÈCHE BAS</strong> : Déclinaison suivante</li>
                            <li><strong>FLÈCHE HAUT</strong> : Déclinaison précédente</li>
                            <li><strong>SHIFT + [+]</strong> : Ajout d'une nouvelle déclinaison</li>
                        </ul>
                        </p>

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

                    {% do advancedType.products.setRendered %}
                    <script>
                        window.productForm[{{ i }}] ={};
                        window.formProductTemplate[{{ i }}] = {};

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


                        window.productForm[{{ i }}][{{ keyForm }}] = {
                            {% if product.vars.value.position %}position: "{{ product.vars.value.position }}",{% endif %}
                            {% 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[{{ i }}][{{ keyForm }}] = '{{ product_family_macros.product_row(product, totalProductOrdered[i][product.vars.value.id])|replace({"\n":' ', "\r":' ', "'" : "\\'"})|raw }}';
                        {% endfor %}

                    </script>

                    {{ form_end(advancedType) }}
                </div>
            </li>
        {% endfor %}
    </ul>

{% endblock table %}


{% block head_stylesheets %}
    {{ parent() }}
{% endblock %}

{% block plugin_javascript %}
    {{ parent() }}
{% endblock %}

{% block script_javascript %}
    {{ parent() }}
    {% include '@LcShop/backend/default/block/script-vuejs.html.twig' %}
    <script src="{{ asset('assets/js/backend/script/productfamily/vuejs-advanced-edition-product-family.js') }}"></script>

{% endblock %}
