
var IqPromotionCart = {
    table: "#datatables-promotion-list",
    btn_add: '.add-product-promotion',
    btn_remove: '.remove-product-promotion',
    input_event: '.input-event-promotion-onchange',
    check_event: '.check-event-promotion-onchange',
    url: null,
    action: null,
    cart_holder: '#holder_html_view_cart',
   
    init: function () {
        var _self = this;
        _self.url = $(_self.table).data('url');
        _self.action = $(_self.table).data('action');
        _self.initElements();
        return _self;
    },
    initElements: 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.input_event).off('change').on('change', function(){
            _self.update_input_table($(this));
        });
        $(_self.table).find(_self.check_event).off('change').on('change', function(){
            _self.update_input_table($(this));
        });
        _self.update_poduct_price();

    },
    add_product: function (_obj){
        var _self = this;
        var input = $(_self.table).find('input#product_qty_'+_obj.data('product-id'));
        var qty = parseInt(input.val()) + 1;
        qty = _self.checkNumber(qty);
        input.val(qty);
        _self.update_cart();
    },
    remove_product: function (_obj){
        var _self = this;
        var input = $(_self.table).find('input#product_qty_'+_obj.data('product-id'));
        var qty = parseInt(input.val()) - 1;
        if(qty < 0){
            qty = 0;
        }
        input.val(qty);
        _self.update_cart();
    },
    update_input_table: function (_obj){
        var _self = this;
        var qty = parseInt(_obj.val());
        qty = _self.checkNumber(qty);
        _obj.val(qty);
        _self.update_cart();
    },
    checkNumber : function(number){
        if(number < 0 || isNaN(number)){
            return  0;
        }
        if(number >= 300){
            return  300;
        }
        return number;
    },
    update_cart: function (){
        var _self = this;
        var data = {};
        var tempData = [];
        $(_self.table).find(_self.input_event).each(function(){
            //push, is checked
            if($(_self.table).find('#product_check_'+$(this).data('product-id')).is(':checked')){
                tempData.push({product_id: $(this).data('product-id'), qty: $(this).val()});
            }            
        });
        data.action = $(_self.table).data('action');
        data.user_promotion_id = $(_self.table).data('user_promotion_id');
        data.products = tempData;
        _self.performRequest(data)
            .done(_self.refreshItemsAndView);
        _self.update_poduct_price();
    
    },
    update_poduct_price: function(){
        //console.log("s");
        var _self = this;
        
        $('.calculate_product_qty_price_total').each(function(){
            var admin_product_id = $(this).data('admin_product_id');
            var price = $(this).data('price');
            var total = 0;
            if($('#product_check_' + admin_product_id).is(':checked')){
                var qty = _self.checkNumber($(_self.table).find('#product_qty_' + admin_product_id).val());;
                var total = price*qty;
            }
            $(this).html(total.toFixed(2).replace('.', ','))
        });
    },
    refreshItemsAndView: function (data){
        var _self = IqPromotionCart;
        //console.log(data.html_cart);
        $(_self.cart_holder).html(data.html_cart);
    },
    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!");
        });
    }
};