
var IqShoppingCart = {
    table: "#datatables-order-list",
    btn_add: '.add-product-basket',
    btn_remove: '.remove-product-basket',
    card_holder: '#holder_html_view_card',
    comp_holder: '#holder_html_view_comp_product',
    is_for: null,
    url: null,
    btn_clear: '#clear-products-basket',
    modal: null,
    oTable: null,
    table_input: '.table-input-event-onchange',
    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',

    init: function () {
        var _self = this;
        _self.url = $(_self.table).data('url');
        _self.is_for = $('input[name="'+_self.shipping_is_for+'"]').val();
        _self.showInit();
        $(_self.shipping_state).on('change', function(){
            _self.update_shipping_state($(this));
        });
        return _self;
    },
    setDatabase: function (oTable){
        var _self = this;
        _self.oTable = oTable;
    },
    reInit: function (){
        var _self = this;
        $(_self.table).find(_self.btn_add).on('click', function(){
            _self.add_product($(this))
        });

        $(_self.table).find(_self.btn_remove).on('click', function(){
            _self.remove_product($(this))
        });
        $(_self.table).find(_self.table_input).off('change').on('change', function(){
            _self.update_input_table($(this));
        });
    },
    showInit: function (){
        var _self = this;
        $(_self.btn_clear).on('click', function (){
            _self.performRequest({action: 'clearCart'})
                .done(_self.refreshDatabaseRefreshAndView)
        });
        $(_self.cart_input).on('change', function(){
            _self.update_input_cart($(this));
        });
        $(_self.remove_item).on('click', function(event){
            event.preventDefault();
            _self.update_cart_database($(this).data('product-id'), 0);
        });
        if(_self.is_for === 'me'){
            $('input[name^="'+_self.comp_products+'"]').on('change', function(){
                _self.update_comp_product($(this));
            });
        }
    },
    update_shipping_state : function (_obj){
        var _self = this;
        var id = parseInt(_obj.val());
        var is_for = _obj.data('is-for');
        _self.performRequest({shipping_country_id: id, shipping_is_for: is_for,  action: 'updateShippingCountry'})
            .done(_self.refreshItemsAndView);
    },
    update_input_table: function (_obj){
        var _self = this;
        var qty = parseInt(_obj.val());
        qty = _self.checkNumber(qty);
        _obj.val(qty);
        _self.update_cart(_obj.data('product-id'), qty);
    },
    update_input_cart: function (_obj){
        var _self = this;
        var qty = parseInt(_obj.val());
        qty = _self.checkNumber(qty);
        _obj.val(qty);
        _self.update_cart_database(_obj.data('product-id'), qty);
    },
    update_comp_product: function (_obj){
        var _self = this;
        _self.performRequest({comp_product_id: _obj.val(), comp_num: _obj.data('comp_num'), count_comp_products: $('input[name="'+_self.count_comp_products+'"]').val(), action: 'updateCompProduct'})
            .done(_self.refreshItemsAndView);
    },
    add_product: function (_obj){
        var _self = this;
        var input = $(_self.table).find('input[name="product_qty_'+_obj.data('product-id')+'"]');
        var qty = parseInt(input.val()) + 1;
        qty = _self.checkNumber(qty);
        input.val(qty);
        _self.update_cart(_obj.data('product-id'), qty);
    },
    remove_product: function (_obj){
        var _self = this;
        var input = $(_self.table).find('input[name="product_qty_'+_obj.data('product-id')+'"]');
        var qty = parseInt(input.val()) - 1;
        if(qty < 0){
            qty = 0;
        }
        input.val(qty);
        _self.update_cart(_obj.data('product-id'), qty);
    },
    update_cart_database: function (product_id, qty){
        var _self = this;
        _self.performRequest({product_id: product_id, qty: qty, action: 'updateCart'})
            .done(_self.refreshDatabaseAndView);
    },
    update_cart: function (product_id, qty){
        var _self = this;
        _self.performRequest({product_id: product_id, qty: qty, action: 'updateCart'})
            .done(_self.refreshItemsAndView);
    },
    refreshItemsAndView: function (data){
        var _self = IqShoppingCart;
        $(_self.card_holder).html(data.html_card);
        $(_self.comp_holder).html(data.html_comp);
        _self.showInit();

    },
    refreshDatabaseAndView: function (data) {
        var _self = IqShoppingCart;
        $(_self.card_holder).html(data.html_card);
        $(_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();
    },
    refreshDatabaseRefreshAndView : function (data){
        var _self = IqShoppingCart;
        $(_self.card_holder).html(data.html_card);
        $(_self.comp_holder).html(data.html_comp);

        _self.showInit();
        _self.oTable.draw();
    },
    checkNumber : function(number){
        if(number < 0 || isNaN(number)){
            return  0;
        }
        if(number >= 100){
            return  100;
        }
        return number;
    },
    performRequest : function(data) {
        var _self = this;
        var url = _self.url,
            contentType = 'application/x-www-form-urlencoded; charset=UTF-8';

            data.shipping_is_for = _self.is_for;
            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!");
            });
    }
};