// Reference array sent to dynamic staticRenderFns
var staticRenderFns = [];


Vue.component('product-unit-price', {
    mixins: [mixinUnit, mixinPrice, mixinTemplate],
    props: ['template', 'keyForm'],
    data() {
        return Object.assign({
            behaviorPrice: 'by-piece',
            activeProducts: false,
        }, window.productUnitPriceValues);

    },
    computed: {
        buyingPriceValue: function () {
            return this.buyingPrice;
        },
        buyingPriceWithTaxValue: function () {
            return this.buyingPriceWithTax;
        },
        buyingPriceByRefUnitValue:function(){
            return this.buyingPriceByRefUnit;
        },
        buyingPriceByRefUnitWithTaxValue:function(){
            return this.buyingPriceByRefUnitWithTax;
        },
        multiplyingFactorValue:function(){
            return this.multiplyingFactor;
        },
        priceWithTaxValue: function () {
            return this.priceWithTax;
        },
        priceValue: function () {
            return this.price;
        },
        priceByRefUnitValue:function(){
            return this.priceByRefUnit;
        },
        priceByRefUnitWithTaxValue:function(){
            return this.priceByRefUnitWithTax;
        },
        quantityValue: function () {
            return this.quantity;
        },
        unitValue: function () {
            return this.unit;
        },
        behaviorPriceValue:function(){
            return this.behaviorPrice;
        },
        taxRateValue: function () {
            if (this.taxRate) {
                return this.taxRatesList[this.taxRate]['value'];
            } else if (this.taxRate == null || this.taxRate == "undefined") {
                return this.taxRatesList['default']['value'];
            } else {
                log('ERREUR : pas de taxRate')
            }
        },
        supplierTaxRateValue: function () {
            if (this.supplierTaxRate) {
                return this.taxRatesList[this.supplierTaxRate]['value'];
            } else if (this.supplierTaxRate == null || this.supplierTaxRate == "undefined") {
                return this.taxRateValue;
            } else {
                log('ERREUR : pas de supplier taxRate')
            }
        },

    },
    mounted: function () {
        this.init();
    },
    watch: {
        taxRate: function () {
            this.setBuyingPriceWithTax();
            this.setBuyingPriceByRefUnitWithTax();
            this.setPriceWithTax();
            this.setPriceByRefUnitWithTax();
            this.setMultiplyingFactor();
            this.$parent.updateChild();
        },
        supplierTaxRate: function () {
            this.setBuyingPriceWithTax();
            this.setBuyingPriceByRefUnitWithTax();
        },
        differentSupplierTaxRate:function(){
            this.supplierTaxRate = null;
        },
        activeProducts:function () {
            this.$parent.updateActiveProducts();
        },
        behaviorPrice:function () {
            if(this.behaviorPrice == 'by-reference-unit'){
                this.quantity = 1;
            }
        }
    }
});

Vue.component('product-form', {
    mixins: [mixinUnit, mixinPrice, mixinTemplate],
    props: ['template', 'keyForm', 'productFamily'],
    computed: {
        taxRateValue: function () {
            return this.productFamily.taxRateValue;
        },
        supplierTaxRateValue: function () {
            return this.productFamily.supplierTaxRateValue;
        },
        buyingPriceValue: function () {
            if (this.buyingPrice) return this.buyingPrice;
            else return this.productFamily.buyingPrice;
        },
        buyingPriceWithTaxValue: function () {
            if (this.buyingPriceWithTax) return this.buyingPriceWithTax;
            else return this.productFamily.buyingPriceWithTax;
        },
        buyingPriceByRefUnitValue:function(){
            if (this.buyingPriceByRefUnit) return this.buyingPriceByRefUnit;
            else return this.productFamily.buyingPriceByRefUnit;
        },
        buyingPriceByRefUnitWithTaxValue:function(){
            if (this.buyingPriceByRefUnitWithTax) return this.buyingPriceByRefUnitWithTax;
            else return this.productFamily.buyingPriceByRefUnitWithTax;
        },
        multiplyingFactorValue:function(){
            if (this.multiplyingFactor) return this.multiplyingFactor;
            else return this.productFamily.multiplyingFactor;
        },
        priceWithTaxValue: function () {
            if (this.priceWithTax) return this.priceWithTax;
            else return this.productFamily.priceWithTax;
        },
        priceValue: function () {
            if (this.price) return this.price;
            else return this.productFamily.price;
        },
        priceByRefUnitValue:function(){
            if (this.priceByRefUnit) return this.priceByRefUnit;
            else return this.productFamily.priceByRefUnit;
        },
        priceByRefUnitWithTaxValue:function(){
            if (this.priceByRefUnitWithTax) return this.priceByRefUnitWithTax;
            else return this.productFamily.priceByRefUnitWithTax;
        },
        quantityValue: function () {
            if (this.quantity) return this.quantity;
            else return this.productFamily.quantity;
        },
        unitValue: function () {
            if (this.unit) return this.unit;
            else return this.productFamily.unit;
        },
        propertyExpirationDateValue: function () {
            if (this.propertyExpirationDate) return this.propertyExpirationDate;
            else return this.productFamily.propertyExpirationDate;
        },
        behaviorPriceValue:function(){
            return this.productFamily.behaviorPrice;
        },
        finalPriceWithTax:function () {
            return  getPriceWithTax(parseFloat((this.priceByRefUnitValue * this.quantityValue) / this.unitCoefficient).toFixed(4), this.taxRateValue);
        }
    },
    data() {
        return Object.assign(
            {
                title: null,
                fieldToUpdate: ['title', 'unit', 'quantity', 'price'],
                price: null,
                priceWithTax: null,
                buyingPrice: null,
                multiplyingFactor: null,
                availableQuantityDefault: null,
                availableQuantity: null,
                propertyExpirationDate: null,
                titleInherited: false,
                priceInherited: false,
                priceWithTaxInherited: false,
                unitInherited: false,
                quantityInherited: false,
                multiplyingFactorInherited: false,
                buyingPriceInherited: false,
                buyingPriceWithTaxInherited: false,
                buyingPriceByRefUnitInherited: false,
                buyingPriceByRefUnitWithTaxInherited: false,
                priceByRefUnitInherited:false,
                priceByRefUnitWithTaxInherited:false,
                expirationDateInherited: false,
                availableQuantityInherited: false,
                availableQuantityDefaultInherited: false,
                propertyExpirationDateInherited: false

            }, window.productForm[this.keyForm])
    },
    mounted: function () {
         //INIT VAR
        updateSortableProducts();
        //this.setUnitSelect2();


        //METHOD
        //this.updateProductForm();
        //this.updateProductView();
    },
    methods: {
        setUnitSelect2:function(){
            $(this.$el).find('select').each(function (i, select) {
                setSelect2($(select));
            });
        },
        titleUpdated:function(){},
        availableQuantityUpdated:function(){},
        availableQuantityDefaultUpdated:function(){},
        propertyExpirationDateUpdated:function(){},

        updateProductForm: function () {
            this.init();
        },
        saveProductForm: function () {
            $('#form-product-modal-' + this.keyForm).modal('hide');
        },
        deleteProductForm: function () {
            if (confirm('Êtes-vous sur de cette action ?')) {
                this.$parent.formProductArray.splice(this.keyForm, 1);
            }
        },
    },
    watch: {}
});


appProductFamily = new Vue({
    el: '#lc-product-family-edit',
    mixins: [mixinReduction],
    delimiters: ['${', '}'],
    computed: {
        productFamily: function () {
            if(this.isMounted) {
                return {
                    'title': this.title,
                    'behaviorCountStock': this.behaviorCountStock,
                    'behaviorPrice': this.$refs.productUnitPrice.behaviorPrice,
                    'unit': this.$refs.productUnitPrice.unit, //this.getProductUnitPrice('unit'),
                    'unitWording': this.$refs.productUnitPrice.unitWording,
                    'quantity': this.$refs.productUnitPrice.quantity,
                    'price': this.$refs.productUnitPrice.price,
                    'priceWithTax': this.$refs.productUnitPrice.priceWithTax,
                    'buyingPrice': this.$refs.productUnitPrice.buyingPrice,
                    'buyingPriceWithTax': this.$refs.productUnitPrice.buyingPriceWithTax,
                    'buyingPriceByRefUnit': this.$refs.productUnitPrice.buyingPriceByRefUnit,
                    'buyingPriceByRefUnitWithTax': this.$refs.productUnitPrice.buyingPriceByRefUnitWithTax,
                    'priceByRefUnit': this.$refs.productUnitPrice.priceByRefUnit,
                    'priceByRefUnitWithTax': this.$refs.productUnitPrice.priceByRefUnitWithTax,
                    'taxRateValue': this.$refs.productUnitPrice.taxRateValue,
                    'supplierTaxRateValue': this.$refs.productUnitPrice.supplierTaxRateValue,

                    /*'taxRate': this.$refs.productUnitPrice.taxRate,
                    'supplierTaxRate': this.$refs.productUnitPrice.supplierTaxRate,
                    */
                    'multiplyingFactor': this.$refs.productUnitPrice.multiplyingFactor,
                    'propertyExpirationDate': this.propertyExpirationDate,
                    'behaviorExpirationDate': this.behaviorExpirationDate,
                    'reductionActive': this.reductionActive,
                    'reductionUnit': this.reductionUnit,
                    'reductionValue': this.reductionValue,
                    'reductionBehaviorTaxRate': this.reductionBehaviorTaxRate
                };
            }else{
                return {
                    'title': null,
                    'behaviorCountStock': null,
                    'behaviorPrice': null,
                    'unit': null,
                    'unitWording': null,
                    'quantity': null,
                    'price': null,
                    'priceWithTax':null,
                    'buyingPrice': null,
                    'buyingPriceWithTax': null,
                    'buyingPriceByRefUnit': null,
                    'buyingPriceByRefUnitWithTax': null,
                    'priceByRefUnit': null,
                    'priceByRefUnitWithTax': null,
                    'taxRate': null,
                    'supplierTaxRate': null,
                    'multiplyingFactor': null,
                    'taxRateValue': null,
                    'propertyExpirationDate': this.propertyExpirationDate,
                    'behaviorExpirationDate': this.behaviorExpirationDate
                };
            }
        },
        unitReference: function () {
            this.getUnitReference();
        }
    },
    data() {

        return Object.assign(
            {
                isMounted:false,
                indexFormProduct: 0,
                title: null,
                isNovelty: null,
                isOrganic: null,
                propertyExpirationDate: null,
                behaviorCountStock: null,
                typeExpirationDate: null,
                behaviorExpirationDate: null,
                propertyAllergens: null,
                propertyOrganicLabelActive: false,
                propertyNoveltyExpirationDateActive: false,
                activeProducts:false,

                formProductArray: [],
                currentSection: 'general',
                sectionsArray: [
                    {
                        name: 'general',
                        nameDisplay: 'Général'
                    },
                    {
                        name: 'price',
                        nameDisplay: 'Prix '
                    },
                    {
                        name: 'products',
                        nameDisplay: 'Déclinaisons'
                    },
                    {
                        name: 'stock',
                        nameDisplay: 'Stock'
                    },
                    {
                        name: 'property',
                        nameDisplay: 'Caractéristiques'
                    },
                    {
                        name: 'reduction',
                        nameDisplay: 'Réduction'
                    },
                    {
                        name: 'note',
                        nameDisplay: 'Note interne'
                    }
                ]
            }, window.appProductFamilyValues);
    },
    mounted: function () {
        this.isMounted = true;
        $(".vuejs-checkbox-switch input").each(function () {
            setBootstrapSwitch($(this));
        });
    },
    methods: {
        changeSection: function (section) {
            this.updateChild();
            this.currentSection = section.name;
            $(".vuejs-checkbox-switch input").each(function () {
                setBootstrapSwitch($(this));
            });
        },
        addProductForm: function () {
            var $collectionHolder = $('table.products-collection-table');
            var prototype = $collectionHolder.data('prototype');
            var newForm = prototype;
            newForm = newForm.replace(/__name__/g, this.indexFormProduct);
            this.formProductArray.push(newForm);
            this.indexFormProduct++;

            //updateSortableProducts();
        },

        updateChild: function () {
            if (typeof this.$refs.productForm !== 'undefined') {
                for (i = 0; i < this.$refs.productForm.length; i++) {
                    this.$refs.productForm[i].updateProductForm();
                }
            }
        },
        getUnitReference: function () {
            if (typeof this.$refs.productUnitPrice !== 'undefined') {
                return this.$refs.productUnitPrice.unitReference;
            }
        },
        updateActiveProducts: function () {
            this.activeProducts = this.$refs.productUnitPrice.activeProducts;
        },
        getBehaviorPrice: function () {
            if (typeof this.$refs.productUnitPrice !== 'undefined') {
                return this.$refs.productUnitPrice.behaviorPrice;
            }
        }
    },
    watch: {
        title: function () {
            this.updateChild()
        },
    }
});


$(window).on('load', function () {
    for (i = 0; i < window.formProductTemplate.length; i++) {
        appProductFamily.formProductArray.push(formProductTemplate[i]);
        appProductFamily.indexFormProduct++;
    }
});
