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


Vue.component('order-product', {
    mixins: [mixinTemplate],
    props: ['template', 'keyItem', 'orderProduct', 'editionMode'],
    computed: {},
    mounted: function () {
        this.setFields()

    },
    methods: {
        setFields: function () {
            var app = this;
            fields = ['fieldQuantity', 'fieldProduct'];
            fields.forEach(function (field) {
                if($(app.$refs[field]).length) {
                    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);
                }
            });
        },
        updateOrderProducts: function () {
            log(this.editionMode);

            this.$parent.updateOrderProducts();
        },
        deleteOrderProduct: function () {
            $(this.$refs.fieldQuantity).val(0);
            this.$parent.updateOrderProducts();
        },
        modalExist:function(modalId){
            return this.$parent.modalExist(modalId);
        },
        modalAddRedeliveryOrderProduct:function () {


            $(this.$parent.$refs['addRedeliveryOrderProductFormOrderProduct']).val(this.orderProduct.id);
            $(this.$parent.$refs['addRedeliveryOrderProductFormTitle']).val(this.orderProduct.title);
            $(this.$parent.$refs['addRedeliveryOrderProductFormUnit']).val(this.orderProduct.unit).trigger('change');
            $(this.$parent.$refs['addRedeliveryOrderProductFormQuantityProduct']).val(this.orderProduct.quantityProduct);
            $(this.$parent.$refs['addRedeliveryOrderProductFormQuantityOrder']).val(this.orderProduct.quantityOrder);
            $('#modal-add-redelivery-order-product').modal('show');
        },
        modalAddReductionCredit:function () {
            $(this.$parent.$refs['addReductionCreditFormTitle']).val(this.orderProduct.title);
            $(this.$parent.$refs['addReductionCreditFormValue']).val(this.orderProduct.price);
            $(this.$parent.$refs['addReductionCreditFormBehaviorTaxRate']).val('tax-included').trigger('change');
            $('#modal-add-reduction-credit').modal('show');
        }

    }


});

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: [],
                orderPaymentFormMeanPayment: null,
                orderPaymentFormAmount: null,
                orderPaymentFormPaidAt:null,
                orderPaymentFormReference:null,
                orderPaymentFormComment:null,
                orderPaymentFormId: null,
                deliveryType: null,
                isLoading: true,
                addProductId: null,
                editionMode: true,
                addProductQuantity: null,
                sectionsArray: [
                    {
                        name: 'cart',
                        nameDisplay: 'Panier'
                    },
                    {
                        name: 'addresses',
                        nameDisplay: 'Adresses'
                    },
                    {
                        name: 'delivery',
                        nameDisplay: 'Livraisons'
                    }
                ]
            })/*, window.appOrderProductFamilyValues)*/;
    },
    mounted: function () {
        this.updateOrder(window.orderObject);
        this.preventFormSubmit();
        //log($(this.$el).find('#orderProductsForm').replace(/__name__/g, 0));
    },
    methods: {
        modalExist:function(modalId){
            if($(modalId).length>0){
                return true;
            }else{
                return false;
            }
        },
        preventFormSubmit: function (){
            $(document).keypress(function(e) {
                if (e.which == 13) {
                    e.preventDefault();
                }
            });
        },
        switchEditionMode: function(){
            if(this.editionMode) {
                this.editionMode = false;
            }else {
                this.editionMode = true;
            }
        },
/*
        //TODO Sélection groupé à utiliser pr les remboursements
        modalAddGroupRedeliveryOrderProduct: function (){
            log(this.order.orderProducts);
            $('#orderProductsAction').find('.order-product-checkbox:checked').each(function (i,checkbox) {
                log($(checkbox).val());
            });
            $(this.$parent.$refs['addRedeliveryOrderProductFormOrderProduct']).val(this.orderProduct.id);
            $(this.$parent.$refs['addRedeliveryOrderProductFormTitle']).val(this.orderProduct.title);
            $(this.$parent.$refs['addRedeliveryOrderProductFormUnit']).val(this.orderProduct.unit).trigger('change');
            $(this.$parent.$refs['addRedeliveryOrderProductFormQuantityProduct']).val(this.orderProduct.quantityProduct);
            $(this.$parent.$refs['addRedeliveryOrderProductFormQuantityOrder']).val(this.orderProduct.quantityOrder);
            $('#modal-add-redelivery-order-product').modal('show');
        },*/
        addProductToOrder: function () {
            this.postForm('#addProductToOrderForm', false);
        },
        addOrderPayment: function () {

            if($('#order_payment_meanPayment').val() == 'credit'){
                if(window.confirm("Vous allez débitez le compte prépayév, êtes vous sur ?")){
                    this.postForm('#orderPaymentForm', '#modal-order-payment');
                }
            }else{
                this.postForm('#orderPaymentForm', '#modal-order-payment');
            }
            $('#modal-order-payment').find('#order_payment_id').val('');

        },
        editOrderPayment:function(id){

            this.setOrderPaymentFormValues(this.order.orderPayments[id])
            $('#modal-order-payment').modal("show")

        },

        setOrderPaymentFormValues(orderPayment = false) {
            if (orderPayment)this.orderPaymentFormId = orderPayment['id'];
            else this.orderPaymentFormId = null;
            if (orderPayment) $(this.$refs["meanPaymentSelect"]).val(orderPayment['meanPayment']).trigger('change');
            else $(this.$refs["meanPaymentSelect"]).val(false).trigger('change');
            if (orderPayment) this.orderPaymentFormAmount = orderPayment['amount'];
            else this.orderPaymentFormAmount = null;
            if (orderPayment) this.orderPaymentFormPaidAt = orderPayment['paidAt'];
            else this.orderPaymentFormPaidAt = false;
            if (orderPayment) this.orderPaymentFormReference = orderPayment['reference'];
            else this.orderPaymentFormReference = null;
            if (orderPayment) this.orderPaymentFormComment = orderPayment['comment'];
            this.orderPaymentFormComment = null;
        },
        modalDeleteOrderPayment:function(id){
            $(this.$refs['deleteOrderPaymentId']).val(id);
            $('#modal-delete-order-payment').modal("show")
        },
        deleteOrderPayment: function () {
            this.postForm('#deleteOrderPaymentForm', '#modal-delete-order-payment');
        },
        updateOrderProducts: function () {
            this.postForm('#orderProductsForm', false);
        },
        updateOrderInvoiceAddress: function () {
            this.postForm('#orderInvoiceAddressForm', '#modal-order-invoice-address');
        },
        updateOrderDeliveryAddress: function () {
            this.postForm('#orderDeliveryAddressForm', '#modal-order-delivery-address', true);
        },
        updateOrderDeliveryAvailability: function () {
            this.postForm('#orderDeliveryAvailabilityForm', '#modal-order-delivery-availability');
        },
        addOrderReductionCart: function () {
            this.postForm('#addOrderReductionCartForm', '#modal-add-order-reduction-cart');
        },
        modalDeleteOrderReductionCart: function (id) {
            log($(this.$refs['deleteOrderReductionCartId']));
            $(this.$refs['deleteOrderReductionCartId']).val(id);
            $('#modal-delete-reduction-cart').modal("show")
        },
        deleteOrderReductionCart: function () {
            this.postForm('#deleteOrderReductionCartForm', '#modal-delete-reduction-cart');
        },
        modalDeleteOrderReductionCredit: function (id) {
            $(this.$refs['deleteOrderReductionCreditId']).val(id);
            $('#modal-delete-reduction-credit').modal("show")
        },
        deleteOrderReductionCredit: function () {
            this.postForm('#deleteOrderReductionCreditForm', '#modal-delete-reduction-credit');
        },
        addOrderReductionCredit: function () {
            this.postForm('#addOrderReductionCreditForm', '#modal-add-order-reduction-credit');
        },
        addRedeliveryOrderProduct: function () {
            this.postForm('#addRedeliveryOrderProductForm', '#modal-add-redelivery-order-product');
        },
        addReductionCredit: function () {
            this.postForm('#addReductionCreditForm', '#modal-add-reduction-credit');
        },
        sendPaymentLink: function(){
            this.postForm('#orderSendPaymentLinkForm', '#modal-order-send-payment-link');
        },
        gotoPaymentLink: function(){
            this.postForm('#orderSendPaymentLinkForm', '#modal-order-send-payment-link');
        },
        postForm: function (formId, modalId, refresh =false) {
            var app = this;
            if(checkFormValidity(formId)) {
                this.isLoading = true;
                if (modalId) $(modalId).modal('hide');
                $.ajax({
                    url: $(formId).prop('action'),
                    method: "POST",
                    data: $(formId).serialize(),
                    dataType: "json",
                    success: function (response) {
                        setFlashMessages(response.flashMessages);
                        app.updateOrder(response.data);
                        if(refresh)window.location.reload();
                    }
                });
            }
        },
        updateOrder: function (orderObject) {
            this.orderProducts = orderObject.orderProducts;
            this.order = orderObject;
            this.isLoading = false;
        },
    },
    watch: {}
});


