let mixinPrice = {
        data() {
            return Object.assign({
                price: null,
                priceWithTax: null,
                buyingPrice: null,
                buyingPriceWithTax: null,
                buyingPriceByRefUnit: null,
                buyingPriceByRefUnitWithTax: null,
                differentSupplierTaxRate: null,
                multiplyingFactor: null,
                priceByRefUnit: null,
                priceByRefUnitWithTax: null,
                taxRate: null,
                supplierTaxRate: null
            }, window.mixinPriceValues);

        },
        computed: {
            marginProfit: function () {
                if (this.behaviorPriceValue == 'by-piece') {
                    return getMargin(this.priceValue,this.buyingPriceValue);
                } else if (this.behaviorPriceValue == 'by-reference-unit') {
                    return getMargin(this.finalPrice,this.finalBuyingPrice);
                }
            },
            marginProfitPercent:function () {
                if (this.behaviorPriceValue == 'by-piece') {
                    return getMarginPercent(this.priceValue,this.buyingPriceValue);
                } else if (this.behaviorPriceValue == 'by-reference-unit') {
                    return getMarginPercent(this.finalPrice,this.finalBuyingPrice);
                }
            },
            marginProfitWithReduction: function () {
                var price = this.$parent.applyReduction(this.priceValue, this.priceWithTaxValue, this.taxRateValue, false);
                return getMargin(price, this.buyingPriceValue);
            },
            marginProfitPercentWithReduction:function () {
                var price = this.$parent.applyReduction(this.priceValue, this.priceWithTaxValue, this.taxRateValue, false);
                return getMarginPercent(price, this.buyingPriceValue);
            },
            finalPrice: function () {
                return parseFloat((this.priceByRefUnitValue * this.quantityValue) / this.unitCoefficient).toFixed(4);
            },
            finalPriceWithTax: function () {
                return getPriceWithTax(parseFloat((this.priceByRefUnitValue * this.quantityValue) / this.unitCoefficient).toFixed(4), this.taxRateValue);
            },
            finalPriceWithTaxAndReduction(){
                return this.$parent.applyReduction(this.priceValue, this.priceWithTaxValue, this.taxRateValue, true);
            },
            finalBuyingPrice:function () {
                return parseFloat((this.buyingPriceByRefUnitValue * this.quantityValue) / this.unitCoefficient).toFixed(4);
            }

        },
        mounted: function () {

        },
        methods: {
            init: function () {
                if (this.behaviorPriceValue == 'by-piece') {
                    this.initByPiece();
                } else if (this.behaviorPriceValue == 'by-reference-unit') {
                    this.initByRefUnit();
                }
            },
            initByPiece: function () {
                this.priceUpdated();
                this.buyingPriceUpdated();
            },

            initByRefUnit: function () {
                this.buyingPriceByRefUnitUpdated();
                this.priceByRefUnitUpdated();
            },


            buyingPriceUpdated: function () {
                if (this.buyingPrice) {
                    this.buyingPrice = formatNumber(this.buyingPrice, 4)
                    this.setBuyingPriceWithTax();
                    //Mise à jour dans le panel price
                    if (this.priceValue == null) {
                        this.setPriceFromMultiplyingFactor();
                        this.priceUpdated();
                    } else {
                        this.setMultiplyingFactor();
                    }
                    this.setBuyingPriceByRefUnit();
                    this.setBuyingPriceByRefUnitWithTax();
                }
            },

            buyingPriceByRefUnitUpdated: function () {
                if (this.buyingPriceByRefUnit) {
                    this.buyingPriceByRefUnit = formatNumber(this.buyingPriceByRefUnit, 4)
                    this.setBuyingPriceByRefUnitWithTax();
                    if (this.priceByRefUnitValue == null) {
                        this.setPriceByRefUnitFromMultiplyingFactor();
                        this.priceByRefUnitUpdated();
                    } else {
                        this.setMultiplyingFactor();
                    }
                    this.setBuyingPriceFromBuyingPriceByRefUnit();
                    this.setBuyingPriceWithTax();
                }
            },


            priceUpdated: function () {
                if (this.price) {

                    this.price = formatNumber(this.price, 4)
                    this.setPriceWithTax();
                    this.setMultiplyingFactor();
                    this.setPriceByRefUnit();
                    this.setPriceByRefUnitWithTax();
                }
            },
            priceByRefUnitUpdated: function () {
                if (this.priceByRefUnit) {
                    this.priceByRefUnit = formatNumber(this.priceByRefUnit, 4)
                    this.setPriceByRefUnitWithTax();
                    this.setMultiplyingFactor();
                    this.setPriceFromPriceByRefUnit();
                    this.setPriceWithTax();
                }
            },

//Toutes les fonctions ci-dessous ne font que mettre à jour les price et buyingPrice
            buyingPriceWithTaxUpdated: function () {
                this.buyingPriceWithTax = formatNumber(this.buyingPriceWithTax, 2)
                this.setBuyingPriceFromBuyingPriceWithTax();
                this.buyingPriceUpdated();
            },
            buyingPriceByRefUnitWithTaxUpdated: function () {
                this.buyingPriceByRefUnitWithTax = formatNumber(this.buyingPriceByRefUnitWithTax, 2)

                this.setBuyingPriceByRefUnitFromBuyingPriceByRefUnitWithTax();
                this.buyingPriceByRefUnitUpdated();
            },

            priceWithTaxUpdated: function () {
                this.priceWithTax = formatNumber(this.priceWithTax, 2);
                this.setPriceFromPriceWithTax();
                this.priceUpdated();
            },

            priceByRefUnitWithTaxUpdated: function () {
                this.priceByRefUnitWithTax = formatNumber(this.priceByRefUnitWithTax, 2)
                this.setPriceByRefUnitFromTax();
                this.priceByRefUnitUpdated();
            },


            unitUpdated: function () {

                this.quantityUpdated();
            },
            quantityUpdated: function () {
                this.quantity = formatNumberWithoutFixed(this.quantity)

                if (this.quantity) {
                    if (this.behaviorPriceValue == 'by-piece') {
                        this.setBuyingPriceByRefUnit();
                        this.setBuyingPriceByRefUnitWithTax();
                        log('ncihe');
                        this.setPriceByRefUnit();
                        this.setPriceByRefUnitWithTax();
                    } else if (this.behaviorPriceValue == 'by-reference-unit') {
                        this.setPriceFromPriceByRefUnit();
                        this.setPriceWithTax();

                        this.setBuyingPriceFromBuyingPriceByRefUnit();
                        this.setBuyingPriceWithTax();
                    }
                }
            },
            multiplyingFactorUpdated: function () {
                this.multiplyingFactor = formatNumber(this.multiplyingFactor, 3)

                if (this.behaviorPriceValue == 'by-piece') {
                    this.setPriceFromMultiplyingFactor();
                    this.setPriceWithTax();
                } else if (this.behaviorPriceValue == 'by-reference-unit') {
                    this.setPriceByRefUnitFromMultiplyingFactor();
                    this.setPriceByRefUnitWithTax();
                }
            },
//NEW VERSION

            setBuyingPriceFromBuyingPriceWithTax: function () {
                if (this.buyingPriceWithTax) {
                    this.buyingPrice = getPrice(this.buyingPriceWithTax, this.supplierTaxRateValue);
                }
            },
            setBuyingPriceWithTax: function () {
                if (this.buyingPrice) {
                    this.buyingPriceWithTax = getPriceWithTax(this.buyingPrice, this.supplierTaxRateValue);
                }
            },

            setPriceFromMultiplyingFactor: function () {
                this.price = getPrice(this.buyingPriceValue * this.multiplyingFactor, this.taxRateValue);
            },
            setPriceWithTax() {
                if (this.price) {
                    this.priceWithTax = getPriceWithTax(this.price, this.taxRateValue);
                }
            },

            setPriceByRefUnitFromMultiplyingFactor: function () {
                this.priceByRefUnit = getPrice(this.buyingPriceByRefUnitValue * this.multiplyingFactorValue, this.taxRateValue);
            },

            //SETTER
            setBuyingPriceFromBuyingPriceByRefUnit: function () {
                this.buyingPrice = parseFloat((this.buyingPriceByRefUnit * this.quantityValue) / this.unitCoefficient).toFixed(4);
            },

            setBuyingPriceByRefUnitFromBuyingPriceByRefUnitWithTax: function () {
                if (this.buyingPriceByRefUnitWithTax) {
                    this.buyingPriceByRefUnit = getPrice(this.buyingPriceByRefUnitWithTax, this.supplierTaxRateValue);
                }
            },
            setBuyingPriceByRefUnit: function () {
                if (this.unitCoefficient && this.quantityValue) {
                    this.buyingPriceByRefUnit = parseFloat((this.buyingPriceValue / this.quantityValue) * this.unitCoefficient).toFixed(4);
                }
            },
            setBuyingPriceByRefUnitWithTax: function () {
                if (this.buyingPriceByRefUnit) {
                    this.buyingPriceByRefUnitWithTax = getPriceWithTax(this.buyingPriceByRefUnit, this.supplierTaxRateValue);
                }
            },
            setMultiplyingFactor: function () {
                if (this.behaviorPriceValue == 'by-piece') {
                    if (this.priceWithTax || this.buyingPrice) {
                        this.multiplyingFactor = parseFloat(this.priceWithTaxValue / this.buyingPriceValue).toFixed(3);
                    }
                } else if (this.behaviorPriceValue == 'by-reference-unit') {
                    if (this.priceByRefUnitWithTax || this.buyingPriceByRefUnit) {
                        this.multiplyingFactor = parseFloat(this.priceByRefUnitWithTaxValue / this.buyingPriceByRefUnitValue).toFixed(3);
                    }
                }
            },
            setPriceByRefUnitFromTax: function () {
                if (this.priceByRefUnitWithTax) {
                    this.priceByRefUnit = getPrice(this.priceByRefUnitWithTax, this.taxRateValue);
                }
            },
            setPriceByRefUnit: function () {
                if (this.unitCoefficient && this.quantityValue) {
                    this.priceByRefUnit = parseFloat((this.priceValue / this.quantityValue) * this.unitCoefficient).toFixed(4);
                }
            },
            setPriceByRefUnitWithTax() {
                if (this.priceByRefUnit) {
                    this.priceByRefUnitWithTax = getPriceWithTax(this.priceByRefUnit, this.taxRateValue);
                }
            },
            setPriceFromPriceByRefUnit: function () {
                this.price = parseFloat((this.priceByRefUnitValue * this.quantityValue) / this.unitCoefficient).toFixed(4);
            },
            setPriceFromPriceWithTax: function () {
                this.price = getPrice(this.priceWithTax, this.taxRateValue);
            },
            emptyPrice: function () {
                this.price = null;
                this.priceInherited = true;
            }
        },
        watch: {
        }

    }
;

let mixinUnit = {
    data() {
        return Object.assign(
            {
                quantity: null,
                unit: null
            }, window.mixinUnitValues);
    },
    computed: {
        unitWording: function () {
            if (this.unit) {
                return this.unitsList[this.unit]['wording'];
            } else if (this.unitValue) {
                return this.unitsList[this.unitValue]['wording'];
            } else {
                return '';
            }
        },
        unitCoefficient: function () {
            if (this.unit) {
                return this.unitsList[this.unit]['coefficient'];
            } else if (this.unitValue) {
                return this.unitsList[this.unitValue]['coefficient'];
            } else {
                return 0;
            }
        },
        unitReference: function () {
            if (this.unit) {
                return this.unitsList[this.unitsList[this.unit]['unitReference']]['wording'];
            } else if (this.unitValue) {
                return this.unitsList[this.unitsList[this.unitValue]['unitReference']]['wording'];
            } else {
                return '';
            }
        }

    }
};
let mixinTemplate = {
    data() {
        return {
            templateRender: null,
        }
    },
    render(h) {
        if (!this.templateRender) {
            return h('div', 'loading...');
        } else { // If there is a template, I'll show it
            return this.templateRender();
        }
    },
    watch: {
        // Every time the template prop changes, I recompile it to update the DOM
        template: {
            immediate: true, // makes the watcher fire on first render, too.
            handler() {
                if (this.template) {

                    var res = Vue.compile(this.template);

                    this.templateRender = res.render;

                    // staticRenderFns belong into $options,
                    // appearantly
                    this.$options.staticRenderFns = []

                    // clean the cache of static elements
                    // this is a cache with the results from the staticRenderFns
                    this._staticTrees = []

                    // Fill it with the new staticRenderFns
                    for (var i in res.staticRenderFns) {
                        //staticRenderFns.push(res.staticRenderFns[i]);
                        this.$options.staticRenderFns.push(res.staticRenderFns[i])
                    }

                }
            }
        }
    }
};


let mixinReduction = {
    data() {
        return Object.assign({
            reductionActive: true,
            reductionUnit: null,
            reductionBehaviorTaxRate: null,
            reductionValue: null,
            reductionPermanent: true,
            reductionUsersActive: false,
            reductionUsers: [],
            reductionGroupUsersActive: false,
            reductionGroupUsers: [],
            reductionSuppliersActive: false,
            reductionSuppliers: [],
            reductionProductCategoriesActive: false,
            reductionProductCategories: [],
            reductionProductFamiliesActive: false,
            reductionProductFamilies: []
        }, window.mixinReductionValues);
    },
    mounted: function () {
    },
    methods: {
        emptySelect2: function (refField) {
            this[refField] = [];
            $(this.$refs[refField]).trigger('change');
        },
        filterOn: function (values, refFiltered, dataFilter, filterType, selectedWay = false) {
            if (values.length > 0) {
                this[refFiltered] = [];

                $(this.$refs[refFiltered]).val(false).trigger('change');

                $(this.$refs[refFiltered]).find('option').prop('disabled', !selectedWay);

                app = this;
                values.forEach(function (val) {
                    if (filterType == 'array') {
                        $(app.$refs[refFiltered]).find('option[data-' + dataFilter + '*="_' + val + '_"').prop('disabled', selectedWay);
                    } else {
                        $(app.$refs[refFiltered]).find('option[data-' + dataFilter + '="' + val + '"').prop('disabled', selectedWay);
                    }

                });
            } else {
                $(this.$refs[refFiltered]).find('option').prop('disabled', false);
            }

            select = setSelect2($(this.$refs[refFiltered]))

        },
        applyReduction: function(price, priceWithTax, taxRateValue, returnWithTax){
            if (this.reductionUnit == 'percent') {
                if(returnWithTax) return applyReductionPercent(priceWithTax, this.reductionValue);
                else return applyReductionPercent(price, this.reductionValue);
            } else if (this.reductionUnit == 'amount'){
                if(this.reductionBehaviorTaxRate == 'tax-included') {
                    if(returnWithTax) return applyReductionAmount(priceWithTax, this.reductionValue);
                    else return getPrice(applyReductionAmount(priceWithTax, this.reductionValue), taxRateValue);
                }else if(this.reductionBehaviorTaxRate == 'tax-excluded') {
                    if(returnWithTax) return getPriceWithTax(applyReductionAmount(price, this.reductionValue), taxRateValue);
                    else return applyReductionAmount(price, this.reductionValue);
                }
            }
        }
    },
    watch: {
        reductionGroupUsersActive:function(){
            this.emptySelect2('reductionGroupUsers');
        },
        reductionGroupUsers: function () {
            this.filterOn(this.reductionGroupUsers, 'reductionUsers', 'group-users', 'array');
        },
        reductionUsersActive:function(){
            this.emptySelect2('reductionUsers');
        },
        reductionSuppliersActive:function(){
            this.emptySelect2('reductionSuppliers');
        },
        reductionSuppliers: function () {
            this.filterOn(this.reductionSuppliers, 'reductionProductFamilies', 'supplier', 'string');
        },
        reductionProductCategoriesActive:function(){
            this.emptySelect2('reductionProductCategories');
        },
        reductionProductFamiliesActive:function(){
            this.emptySelect2('reductionProductFamilies');
        },

    }
};
