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


Vue.component('order-product', {
    mixins: [mixinTemplate],
    props: ['template', 'keyItem', 'orderProduct'],
    computed: {},
    /* data:function() {
         return{

             product:null
         };
        /!* return Object.assign(
             {
                 /!*title: null,
                 price: null,
                 priceWithTax: null,
                 priceWithTaxAndReduction: null,
                 totalWithoutTax: null,
                 totalWithTax: null,
                 buyingPrice: null,
                 quantity: 1,
                 unit: null,
                 product: null*!/
             }, window.orderProducts[this.key])*!/
     },*/
    mounted: function () {
        log(this.keyItem);
        this.setFields()
//this.$el.replace(/__name__/g, this.key);
    },
    methods: {
        init: function () {
            //log(this.$els);
            //log(this.$element);
        },
        setFields: function () {
            var app = this;
            fields = ['fieldQuantity', 'fieldProduct'];
            fields.forEach(function (field) {
                var name = $(app.$refs[field]).prop('name');
                var id = $(app.$refs[field]).prop('id');
                name = name.replace(/__name__/g, app.keyItem);
                id = id.replace(/__name__/g, app.keyItem);
                $(app.$refs[field]).prop('name', name);
                $(app.$refs[field]).prop('id', id);

                //log(app.$refs[field]);
            });
        },
        updateOrderProducts: function () {
            this.$parent.updateOrderProducts();
        },
        deleteOrderProduct: function () {
            $(this.$refs.fieldQuantity).val(0);
            this.$parent.updateOrderProducts();
        }
        /*log($('#order-products-list').data('prototype'));
        var prototype = $('#order-products-list').data('prototype');

        var newForm = prototype;
        newForm = newForm.replace(/__name__/g, key);

        */
    }


});

appOrder = new Vue({
    el: '#lc-order-edit',
    delimiters: ['${', '}'],
    computed: {
        deliveryAddressHtml: function () {
            log(this.order.deliveryAddress);
            return this.order.deliveryAddress;
        }
    },
    data() {
        return Object.assign(
            {
                templateTest: window.templateTest,
                orderProducts: [],
                order: [],
                deliveryType: null,
                isLoading: true,
                addProductId: null,
                addProductQuantity: null,

                sectionsArray: [
                    {
                        name: 'cart',
                        nameDisplay: 'Panier'
                    },
                    {
                        name: 'addresses',
                        nameDisplay: 'Adresses'
                    },
                    {
                        name: 'delivery',
                        nameDisplay: 'Livraisons'
                    }
                ]
            })/*, window.appOrderProductFamilyValues)*/;
    },
    mounted: function () {
        log('ncnnc');
        log(window.templateTest);
        this.updateOrder(window.orderObject);
        //log($(this.$el).find('#orderProductsForm').replace(/__name__/g, 0));
    },
    methods: {
        addProductToOrder: function () {
            this.postForm('#addProductToOrderForm', false);
        },
        updateOrderProducts: function () {
            this.postForm('#orderProductsForm', false);
        },
        updateOrderInvoiceAddress: function () {
            this.postForm('#orderInvoiceAddressForm', '#modal-order-invoice-address');
        },
        updateOrderDeliveryAddress: function () {
            this.postForm('#orderDeliveryAddressForm', '#modal-order-invoice-address');
        },
        updateOrderDeliveryAvailability:function(){
            this.postForm('#orderDeliveryAvailabilityForm', '#modal-order-delivery-availability');
        },
        addOrderReductionCart:function(){
            this.postForm('#orderReductionCartForm', '#modal-reduction-cart');
        },
        addOrderReductionCredit:function(){
            this.postForm('#orderReductionCreditForm', '#modal-reduction-credit');
        },
        postForm:function(formId, modalId){
            var app = this;
            this.isLoading = true;
            if(modalId)$(modalId).modal('hide');
            $.ajax({
                url: $(formId).prop('action'),
                method: "POST",
                data: $(formId).serialize(),
                dataType: "json",
                success: function (response) {
                    generateNotice(response.status, response.message);
                    app.updateOrder(response.data);
                }
            });
        },
        updateOrder: function (orderObject) {
            this.orderProducts = orderObject.orderProducts;
            this.order = orderObject;
            this.isLoading = false;
        },
    },
    watch: {
    }
});


