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

Vue.component('order-product-form', {
    mixins: [mixinTemplate],
    props: ['template', 'keyForm'],
    computed: {},
    data() {
        return Object.assign(
            {
                title: null,
                price: null,
                priceWithTax: null,
                priceWithTaxAndReduction: null,
                totalWithoutTax: null,
                totalWithTax: null,
                buyingPrice: null,
                quantity: 1,
                unit: null,
                product: null
            }, window.orderProductForm[this.keyForm])
    },
    mounted: function () {
        this.setSelect2();
        this.updateQuantity();

    },
    methods: {
        updateLine: function (data) {

            this.price = data.price;
            this.priceWithTax = data.priceWithTax;
            this.priceWithTaxAndReduction = data.priceWithTaxAndReduction;
            this.priceWithTax = data.priceWithTax;
            this.updateQuantity();
            this.$parent.updateTotalPrice();
        },
        updateQuantity: function () {
            this.totalWithTax = parseFloat(this.priceWithTaxAndReduction * this.quantity).toFixed(2);
            this.totalWithoutTax = parseFloat(this.price * this.quantity).toFixed(2);
            this.$parent.updateTotalPrice();
        },
        setSelect2: function () {
            var selectProduct = $(this.$el).find('select.form-control');
            var select2Product =setSelect2(selectProduct);
            var vue = this;
            select2Product.on('select2:select', function (e) {
                //dispatch event pour que le select soit "submitable"
                var event = new Event('change');
                e.target.dispatchEvent(event);
                this.product = select2Product.val();

                $.ajax({
                    url: DOMAIN + "/api/product/" + this.product,
                    method: "GET",
                    dataType: "json",
                    success: function (response) {
                        vue.updateLine(response);
                    }
                });
            })
        },
        deleteProductForm: function () {
            if (confirm('Êtes-vous sur de cette action ?')) {
                this.$parent.formOrderProductArray.splice(this.keyForm, 1);
            }
        }
    },
    watch: {}
});


Vue.component('delivery-availability-choice', {
    mixins: [mixinTemplate],
    props: ['template', 'key', 'name', 'id'],
    data() {
        return {
            id: -1,
            deliveryAvailability: null, // Have to assign to this variable**
            deliveryAvailabilities: null, // Have to assign to this variable**
            selected_category: "",
            value: null,
            label: null
        };
    },
    mounted() {
    },
    methods: {
        updateDeliveryAvailability() {
            log(this.deliveryAvailability);
            /*log('bnfbf');
            var self = this;
            $.ajax({
                url: DOMAIN+"/admin/delivery-availaibility/14",
                method: "POST",
                dataType: "",
                success: function (response) {
                    self.categories =  response.categories;
                }
            });*/

        }
    }
});

appOrder = new Vue({
    el: '#lc-order-edit',
    delimiters: ['${', '}'],
    computed: {},
    data() {
        return Object.assign(
            {
                currentSection: 'cart',
                formOrderProductArray: [],
                indexOrderProductForm: 0,
                formDeliveryAvailabilityLargeArray: [],
                formDeliveryAvailabilityShortArray: [],
                indexDeliveryAvailability: null,
                invoiceAddress: null,
                deliveryAddress: null,
                deliveryType: null,
                deliveryPointSale: null,
                deliveryAvailability: 'niche',
                totalWithTax: null,
                totalWithoutTax: null,
                sectionsArray: [
                    {
                        name: 'cart',
                        nameDisplay: 'Panier'
                    },
                    {
                        name: 'addresses',
                        nameDisplay: 'Adresses'
                    },
                    {
                        name: 'delivery',
                        nameDisplay: 'Livraisons'
                    }
                ]
            })/*, window.appOrderProductFamilyValues)*/;
    },
    mounted: function () {

    },
    methods: {
        addProductToOrder:function(){
            if(this.addProductId && this.addProductQuantity){

            }
        },

        updateTotalPrice: function () {
            this.totalWithoutTax = 0;
            this.totalWithTax = 0;
            for (var i = 0; i < this.$refs.orderProductForm.length; i++) {
                var line = this.$refs.orderProductForm[i];
                this.totalWithoutTax = parseFloat(parseFloat(this.totalWithoutTax) + parseFloat(line.totalWithoutTax)).toFixed(2);
                this.totalWithTax = parseFloat(parseFloat(this.totalWithTax) + parseFloat(line.totalWithTax)).toFixed(2);
            }
        },
        changeSection: function (section) {
            this.updateChild();
            this.currentSection = section.name;

            this.addDeliveryAvailabilityChoice();
        },
        addDeliveryAvailabilityChoice: function () {
            $.ajax({
                url: DOMAIN + "/admin/delivery-availaibility/" + this.deliveryAddress,
                method: "POST",
                dataType: "",
                success: function (response) {
                    for (i = 0; i < response.large.length; i++) {
                        slot = response.large[i];
                        var deliveryAvailabilityForm = $('#delivery-availability-short').data('prototype');
                        deliveryAvailabilityForm = deliveryAvailabilityForm.replace(/__id__/g, "{{ form.deliveryAvailabilityZone.vars.id }}_"+slot.id );
                        deliveryAvailabilityForm = deliveryAvailabilityForm.replace(/__name__/g, "{{ form.deliveryAvailabilityZone.vars.full_name }}" );
                        deliveryAvailabilityForm = deliveryAvailabilityForm.replace(/__value__/g, slot.id );
                        deliveryAvailabilityForm = deliveryAvailabilityForm.replace(/__label__/g, slot.label );

                        appOrder.formDeliveryAvailabilityLargeArray.push(deliveryAvailabilityForm);
                        appOrder.indexDeliveryAvailability++;
                    }

                    for (i = 0; i < response.short.length; i++) {
                        slot = response.short[i];
                        var deliveryAvailabilityForm = $('#delivery-availability-short').data('prototype');
                        deliveryAvailabilityForm = deliveryAvailabilityForm.replace(/__id__/g, "{{ form.deliveryAvailabilityZone.vars.id }}_"+slot.id );
                        deliveryAvailabilityForm = deliveryAvailabilityForm.replace(/__name__/g, "{{ form.deliveryAvailabilityZone.vars.full_name }}" );
                        deliveryAvailabilityForm = deliveryAvailabilityForm.replace(/__value__/g, slot.id );
                        deliveryAvailabilityForm = deliveryAvailabilityForm.replace(/__label__/g, slot.label );

                        appOrder.formDeliveryAvailabilityShortArray.push(deliveryAvailabilityForm);
                        appOrder.indexDeliveryAvailability++;
                    }
                }
            });

        },
        addOrderProductForm: function () {
            var $collectionHolder = $('#order-products-list');
            var prototype = $collectionHolder.data('prototype');
            var newForm = prototype;
            newForm = newForm.replace(/__name__/g, this.indexOrderProductForm);
            this.formOrderProductArray.push(newForm);
            this.indexOrderProductForm++;
            //updateSortableProducts();
        },

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


