
var IqHomepartyCart = {
    form: "#homeparty-order-form",
    btn_modal_add: '.add-product-basket',
    btn_add: '.add-from-basket',
    btn_remove: '.remove-from-basket',
    table_input: '.table-input-event-onchange',
    oTable: null,
    cart_holder: '#insert_show_products_order_',
    cart_holders: '.insert_show_products_order',
    modal: '#modals-load-content',
    remove_item_cart: '.remove_item_form_cart',
    insert_show_total_order: '#insert_show_total_order',
    insert_show_bonus: '#insert_show_bonus',
    insert_show_bonus_host: '#insert_show_bonus_host',
    select_delivery_option: '.select_delivery_option',
    /*
     comp_holder: '#holder_html_view_comp_product',
     is_for: null,
     url: null,
     btn_clear: '#clear-products-basket',

     cart_input: '.cart-input-event-onchange',
     remove_item: '.remove_item_form_cart',
     shipping_state: '#change_shipping_state',
     comp_products: 'switchers-comp-product',
     count_comp_products: 'count_comp_products',
     shipping_is_for: 'shipping_is_for',*/

    _log: function ($msg){
        console.log($msg);
    },
    init: function () {
        var _self = this;
        _self.url = $(_self.form).attr('action');


        $(_self.cart_holders).each(function (key, obj){
            _self.reInit($(obj));

        });
        //_self.showInit();

        /*$(_self.shipping_state).on('change', function(){
            _self.update_shipping_state($(this));
        });*/
        return _self;
    },
    setDatabase: function (oTable){
        var _self = this;
        _self.oTable = oTable;
    },
    reInitModal: function (){
        var _self = this;
        $(_self.oTable).find(_self.btn_modal_add).on('click', function(){
           _self.add_product_modal($(this))
        });
    },
    add_product_modal: function (_obj){
        var _self = this;
        _self.performRequest({
            product_id: _obj.data('product-id'),
            homeparty_id: $(_self.oTable).data('homeparty-id'),
            homeparty_user_id: $(_self.oTable).data('homeparty-user-id'),
            qty: 1,
            action: 'addProduct'
        }).done(_self.refreshView);

    },
    reInit: function (obj){
        var _self = this;
        var _obj = obj;
        obj.find(_self.btn_add).off('click').on('click', function(){
            _self.add_product($(this), _obj)
        });
        obj.find(_self.btn_remove).off('click').on('click', function(){
            _self.remove_product($(this), _obj)
        });
        obj.find(_self.table_input).off('change').on('change', function(){
            _self.update_input_table($(this), _obj);
        });

        obj.find(_self.remove_item_cart).off('click').on('click', function(){
            _self.remove_from_cart($(this), _obj);
        });

        obj.find(_self.select_delivery_option).off('change').on('change', function(){
            _self.update_delivery_option($(this), _obj);
        });
    },

    add_product: function (_obj, _holder){
        var _self = this;
        var input = _holder.find('input[name="product_qty_'+_obj.data('order-item-id')+'"]');
        var qty = parseInt(input.val()) + 1;
        qty = _self.checkNumber(qty);
        input.val(qty);
        _self.update_cart(_holder, _obj, qty);
    },
    remove_product: function (_obj, _holder){
        var _self = this;
        var input = _holder.find('input[name="product_qty_'+_obj.data('order-item-id')+'"]');
        var qty = parseInt(input.val()) - 1;
        qty = _self.checkNumber(qty);
        input.val(qty);
        _self.update_cart(_holder, _obj, qty);
    },
    update_input_table: function (_obj, _holder){
        var _self = this;
        var qty = parseInt(_obj.val());
        qty = _self.checkNumber(qty);
        _obj.val(qty);
        _self.update_cart(_holder, _obj, qty);
    },
    update_cart: function (_holder, _obj, qty){
        var _self = this;
        _self.performRequest({
            product_id: _obj.data('product-id'),
            order_item_id: _obj.data('order-item-id'),
            homeparty_id: _holder.data('homeparty-id'),
            homeparty_user_id: _holder.data('homeparty-user-id'),
            qty: qty,
            action: 'updateCart'
        }).done(_self.refreshView);
    },
    remove_from_cart: function (_obj, _holder){
        var _self = this;
        _self.performRequest({
            product_id: _obj.data('product-id'),
            order_item_id: _obj.data('order-item-id'),
            homeparty_id: _holder.data('homeparty-id'),
            homeparty_user_id: _holder.data('homeparty-user-id'),
            action: 'removeFromCart'
        }).done(_self.refreshView);
    },
    update_delivery_option: function (_obj, _holder){
        var _self = this;
        _self.performRequest({
            delivery: _obj.val(),
            homeparty_id: _holder.data('homeparty-id'),
            homeparty_user_id: _holder.data('homeparty-user-id'),
            action: 'updateDeliveryOption'
        }).done(_self.refreshView);
    },

    refreshView: function (data) {
        var _self = IqHomepartyCart;
        var obj = $(_self.cart_holder+data.data.homeparty_user_id)
        obj.html(data.html_user_cart);
        $(_self.insert_show_total_order).html(data.html_total);
        $(_self.insert_show_bonus).html(data.html_bonus);
        $(_self.insert_show_bonus_host).html(data.html_host_bonus);
        $(_self.modal).modal('hide');
        _self.reInit(obj);
        //$(_self.comp_holder).html(data.html_comp);
       // var input = $(_self.table).find('input[name="product_qty_'+data.data.product_id+'"]');
       // input.val(data.data.qty);
       // _self.showInit();
    },
    performRequest : function(data) {
        var _self = this;
        var url = _self.url,
            contentType = 'application/x-www-form-urlencoded; charset=UTF-8';

        console.log(data);
        console.log(url);

        return $.ajax({
            url: url,
            data: data,
            type: "POST",
            dataType: "json",
            cache: false,
            contentType: contentType,
            encode: true,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        })
            .done(function (data) {
                console.log('performRequest');
                console.log(data);

            }).fail(function (jqXHR, textStatus, errorThrown) {
                console.log(jqXHR);
                console.log(jqXHR.responseText);
                console.log(textStatus);
                console.log(errorThrown);
                console.log("Sorry, there was a problem!");
            });
    },
    checkNumber : function(number){
        if(number < 1 || isNaN(number)){
            return  1;
        }
        if(number >= 100){
            return  100;
        }
        return number;
    }
};