module.exports = {
	//辅助数组添加元素功能
	find_in_add: function(list, ele) {
	    var idx=list.findIndex(function (e) {
		   return e.goods_id==ele.goods_id;
        })
	    if(idx>-1) return false;
		list.push(ele);
	},
	//辅助数组添加元素功能
	find_in_remove: function(list, ele) {
        var idx=list.findIndex(function (e) {
            return e.goods_id==ele.goods_id;
        })
        if(idx==-1) return false;
		list.splice(idx, 1);
	},

	//主要是把组合购的商品分离出来
	add_ladder_prom: function(car, a, ele) {
		var point = car[a];
		if (point.ladder_map) {
			if (point.ladder_map[ele.prom_id]) {
				this.find_in_add(point.ladder_map[ele.prom_id].goods, ele);
			} else {
				point.ladder_map[ele.prom_id] = {
					goods: [],
					act: ele.act
				};
				point.ladder_map[ele.prom_id].goods.push(ele);
			}
		} else {
			point.ladder_map = {};
			point.ladder_map[ele.prom_id] = {
				goods: [],
				act: ele.act
			};
			point.ladder_map[ele.prom_id].goods.push(ele);
		}
	},
	//未选中的商品，要从中剔除
	remove_ladder_prom: function(car, a, ele) {
		var point = car[a];
		if (point.ladder_map) {
			if (point.ladder_map[ele.prom_id]) {
				this.find_in_remove(point.ladder_map[ele.prom_id].goods, ele);
			}
		}
	},

	//把组合购的商品价格计算出来,同时每个会员都有购买组合商品的上限！ladder_b_num是已经购买了多少件
	calculate_ladder: function(car, a, th) {
		var car_item = car[a];
		var tfeel = 0,offline_price = 0,offline_num = 0;
		var txt = "requestData[" + a + "].l_need_list";
		var need_to=[];

		//活动的列表都存储在这里面，可以快速查询
		if (!car_item.ladder_map) {
			var res = {
				tfeel: tfeel,
				offline_price: offline_price,
				offline_num: offline_num
			};
			th.setData({
				[txt]: null
			});
			return res;
		}
		
		//因为可能有多个组合购的活动
		for (var mi in car_item.ladder_map) {
			var bitem = car_item.ladder_map[mi];
			var act = bitem.act; //获取到组合购的详情
			if(!act) continue;

			var all_num = 0; //商品数量之和
			var all_price = 0;
			var aprice=0;
			var no_in_arr = []; //剩余的未加入组合购

			var title ="";  //提示语；
			var goods = car_item.goods;

			//-- 每件商品要剔除的量 --
            for (let v3 in car_item.goods) {
                car_item.goods[v3].l_need_downlow_num=0;
			}

            //寻找一下
            function get_num2(ite) {
                var vh=car_item.goods.findIndex(function (e) {
                    return e.goods_id == ite.goods_id
                })
                if(vh>-1) return car_item.goods[vh];
                return 0;
            }

			for (var i in goods) {
            	if(goods[i].prom_type!=10) continue;
            	if(!goods[i].selected) continue;
                if(goods[i].prom_id!=act.id) continue;
				var item = goods[i];
                item.goods_price=item.goods_price;
                item.offline_price=item.offline_price;
				item.cart_num =item.goods_num;
				all_num += item.cart_num;
				for (var j = 0; j < item.cart_num; j++) {
						no_in_arr.push({
							price: item.goods_price,offline_price:item.offline_price,goods_id:item.goods_id,shop_price:item.shop_price,goods_market_price:item.goods_market_price
						})
				}
			}

            function sortData(a, b) {
                return a.price - b.price
            }
            no_in_arr.sort(sortData);

            //开始阶梯计算价格
			var lev=0;
			for(let j in act.ladder_list){
				if(no_in_arr.length==0) break;
				let item_j=act.ladder_list[j];
				var end=no_in_arr.pop();
				var new_price = parseInt(item_j.discount_field) == 2?end.goods_market_price:(parseInt(item_j.discount_field) == 1 ? end.shop_price : end.price);
                aprice+=new_price* parseFloat(item_j.discount)/10;
                lev++;
			}

			//获取阶梯促销的下一个促销
			if(lev>0 && lev<act.ladder_list.length){
                title = "再买1件，第"+(lev+1)+"件"+act.ladder_list[lev].discount+"折";
                var it = {
                    id: act.id,
                    pickup_id: car_item.pid,
                    title: title
                };
				need_to.push(it);
			}

            all_price+=aprice;
            //算一下剩余的钱
            if (no_in_arr.length) {
                for (var ii in no_in_arr) {
                    var item = no_in_arr[ii];
                    var gitem=get_num2(item);
                    if(gitem) {
                        if (!gitem.l_need_downlow_num) {
                            gitem.l_need_downlow_num = 1;
                        }
                        else {
                            gitem.l_need_downlow_num++;
                        }
                        all_price += item.price;
                        if (item.offline_price) {
                            offline_price += gitem.goods_price - gitem.offline_price;
                            offline_num += 1;
                        }
                    }
                }
            }
            tfeel += all_price;
		}
		var res = {
			tfeel: tfeel,
            aprice:aprice,
			offline_price: offline_price,
			offline_num: offline_num,
		};

		th.setData({ [txt]: need_to});
		return res;
	},

	find_need_to: function(list, iter) {
		if (!list || list.length <= 0) return -1;
		for (var i in list) {
			var item = list[i];
			if (item.id == iter.id) {
				return i;
			}
		}
		return -1;
	},

	//筛选阶梯促销，纯粹的数组按活动id分组
	find_split: function(arr) {
		//过滤只有团购的商品
		var oarr = arr.goods.filter(function(ele) {
			return ele.prom_type == 10
		})
        if(!oarr || oarr.length==0) return null;
		//看一下有多少个不同的团购
		var map = {},dest = [];
		for (var i = 0; i < oarr.length; i++) {
			var ai = oarr[i];
			var index=map[ai.prom_id]
			if (!index) {
				dest.push({
					prom_id: ai.prom_id,
					data: [ai]
				});
				map[ai.prom_id]=dest.length; //存储下标
			} else {
				var dj = dest[index-1];
				dj.data.push(ai);
			}
		}
		return dest;
	}


}
