module.exports = {
	//辅助数组添加元素功能
	find_in_add: function (list, ele) {
		for (var i in list) {
			var item = list[i];
			if (item.goods_id == ele.goods_id) {
				return false;
			}
		}
		list.push(ele);
	},
	//辅助数组添加元素功能
	find_in_remove: function (list, ele) {
		for (var i in list) {
			var item = list[i];
			if (item.goods_id == ele.goods_id) {
				list.splice(i, 1);
			}
		}
	},

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

	//把组合购的商品价格计算出来,同时每个会员都有购买组合商品的上限！zh_b_num是已经购买了多少件
	calculate_zh: function (car, a, th) {
		var car_item = car[a];
		var tfeel = 0,
			offline_price = 0,
			offline_num = 0;
		var txt = "requestData[" + a + "].need_list";
		//活动的列表都存储在这里面，可以快速查询
		var zh_prom_goods = car_item.zh_prom_goods;
		if (!car_item.zh_map || !zh_prom_goods) {
			var res = {
				tfeel: tfeel,
				offline_price: offline_price,
				offline_num: offline_num
			};
			th.setData({
				[txt]: null
			});
			return res;
		}

		//因为可能有多个组合购的活动
		for (var i in car_item.zh_map) {
			var bitem = car_item.zh_map[i];
			var act = bitem.act; //获取到组合购的详情
			if(!act) continue;


			var all_num = 0; //商品数量之和
			var all_price = 0;
			var need_to_buy = 0;
			var all_zhqty = 0; //所有商品要求起购数之和
			var no_in_arr = []; //剩余的未加入组合购
			var out_arr = []; //超出活动限购的商品放入
			var zhqty_bz = []; //超量
			var title = ""; //提示语； 	
			//var goods = bitem.goods;
			var goods = zh_prom_goods[act.id];
			for (let v3 in car_item.goods) {
				car_item.goods[v3].need_downlow_num = 0;
			}

			//寻找一下
			function get_num(ite) {
				for (let v1 in bitem.goods) {
					var vh = bitem.goods[v1];
					if (vh.goods_id == ite.goods_id) {
						return vh;
					}
				}
				return 0;
			}

			//寻找一下
			function get_num2(ite) {
				for (let v2 in car_item.goods) {
					var vh = car_item.goods[v2];
					if (vh.goods_id == ite.goods_id) {
						return vh;
					}
				}
				return 0;
			}

			for (var i in goods) {
				var item = goods[i];
				item.goods_price = get_num(item).goods_price;
				item.offline_price = get_num(item).offline_price;
				var cart_num = get_num(item).goods_num;
				var zh_b_num = get_num2(item).zh_b_num;
				if (!cart_num) {
					if (item.zhqty) {
						title = "必买商品未加购，下单立享【" + act.zhprice + "元任选" + act.zhbuyqty + "件】";
						need_to_buy += item.zhqty;
					}
					continue;
				}

				item.cart_num = cart_num;
				//-- 当商品有购买的时候,以及有限购的时候 --
				if (item.buyqty > 0) {
					console.log(111, th.data.in_zhact_gdmap);
					var cbuy = item.buyqty - zh_b_num;
					var key = item.goods_id + "." + act.id;
					if (th.data.in_zhact_gdmap[key]) {
						var cbuy = item.buyqty - zh_b_num - th.data.in_zhact_gdmap[key];
					}
					//当可买的数量为0
					if (cbuy <= 0) {
						item.num = 0;
						out_arr.push({
							price: item.goods_price,
							offline_price: item.offline_price,
							goods_id: item.goods_id,
							num: cart_num
						})
					} else {
						if (cbuy >= cart_num) item.num = cart_num;
						else {
							item.num = cbuy;
							out_arr.push({
								price: item.goods_price,
								offline_price: item.offline_price,
								goods_id: item.goods_id,
								num: cart_num - cbuy
							})
						}
					}

				} else {
					item.num = cart_num;
				}

				item.goods_price = get_num(item).goods_price;
				item.offline_price = get_num(item).offline_price;
				if (item.cart_num) {
					all_price += item.cart_num * item.goods_price;
				}
				if (item.num) {
					all_num += item.num;
					//当有起购数的控制的时候
					if (item.zhqty) {
						zhqty_bz.push(item);
						all_zhqty += item.zhqty;
						if (item.num < item.zhqty) {
							need_to_buy += item.zhqty - item.num;
						}
						if (item.num > item.zhqty) {
							for (var i = 0; i < item.num - item.zhqty; i++) {
								no_in_arr.push({
									price: item.goods_price,
									offline_price: item.offline_price,
									goods_id: item.goods_id
								})
							}
						}
					} else {
						for (var j = 0; j < item.num; j++) {
							no_in_arr.push({
								price: item.goods_price,
								offline_price: item.offline_price,
								goods_id: item.goods_id
							})
						}
					}
				} else {
					if (item.zhqty) {
						title = "必买商品未加购，下单立享【" + act.zhprice + "元任选" + act.zhbuyqty + "件】";
						need_to_buy += item.zhqty;
					}
				}
			}

			//当满足组合的要求：总数要满足，起购数要满足
			if (all_num >= act.zhbuyqty && !need_to_buy) {
				//商品的列表
				for (var u in goods) {
					var ite = goods[u];
					if (ite.buyqty > 0 && ite.num > 0) {

						console.log(222, th.data.in_zhact_gdmap);

						var key = ite.goods_id + "." + act.id;
						if (th.data.in_zhact_gdmap[key]) {
							th.data.in_zhact_gdmap[key] += ite.num;
						} else {
							th.data.in_zhact_gdmap[key] = ite.num;
						}
					}
				}

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

				var aprice = act.zhprice;
				if (act.zhbuyqty > all_zhqty) {
					for (var n = 0; n < act.zhbuyqty - all_zhqty; n++) {
						no_in_arr.pop();
					}
				}
				//看一下剩下的数量有没有满足组合购的要求，以及要不要倍增
				if (act.is_bz && no_in_arr.length >= act.zhbuyqty) {
					var bz_num_ok = 0; //超量倍增是否满
					var delete_num = 0;
					var zhqty_len = 0; //几个超量倍增
					var be = parseInt(no_in_arr.length / act.zhbuyqty); //看一下是几倍

					//如果有总数控制的时候,看还能买几组
					if(act.zh_num){
						var be1= parseInt(act.zh_num-act.zh_buy_num)-1;
						if(be1<be) be=be1;
					}

					if(be){
						if (act.is_bzyh && zhqty_bz.length > 0) {
							if (zhqty_bz.length > 1) {
								let zhqty_bz_arr = [];
								let zhqty_bz_flag=this.zhqty_bz_fun(zhqty_bz,be,zhqty_bz_arr);

								if (zhqty_bz_flag) {
									for (let i = 0; i < zhqty_bz.length; i++) {
										var vv = zhqty_bz[i];
										for (let j = 0; j < be * vv['zhqty']; j++) {
											let index = no_in_arr.findIndex(i => {
												return vv.goods_id === i.goods_id
											})
											if (index > -1) {
												delete_num++
												no_in_arr.splice(index, 1)
											}
										}
									}
								} else {
									zhqty_len=1;
									let min_bz_num = Math.min.apply(Math, zhqty_bz_arr.map(function (o) {
										return o['num'];
									}));
									let new_arr = zhqty_bz_arr.filter(ii => {
										return ii['num'] == min_bz_num;
									})
									var vv = new_arr[0];
									var bz_num = be * new_arr[0].zhqty; //超量倍增
									var num = min_bz_num - new_arr[0].zhqty; //购买数量减去超量
									bz_num_ok = bz_num - num;
									if (bz_num_ok <= 0) {
										//超量倍增满足,超量倍增就等于倍数
										bz_num_ok = bz_num;
									} else {
										//超量倍增不满足,倍数要减去多出得
										// be=be-bz_num_ok;
										if (num % vv.zhqty == 0) {
											be = num / vv.zhqty;
										} else {
											be = Math.floor(num / vv.zhqty)
										}
										bz_num_ok = be * vv.zhqty;
									}

									for(let i = 0; i < zhqty_bz.length; i++){
										let item1=zhqty_bz[i];
										for (let j = 0; j < be * item1['zhqty']; j++) {
											let index = no_in_arr.findIndex(i => {
												return item1.goods_id === i.goods_id
											})
											if (index > -1) {
												// delete_num++
												no_in_arr.splice(index, 1)
											}
										}
									}
									aprice += be * aprice;
								}
							} else {
								var vv = zhqty_bz[0];
								var bz_num = be * vv.zhqty; //超量倍增
								var num = vv['num'] - vv.zhqty; //购买数量减去超量
								if (num > 0) {
									bz_num_ok = bz_num - num;
									if (bz_num_ok <= 0) {
										//超量倍增满足,超量倍增就等于倍数
										bz_num_ok = bz_num;
									} else {
										//超量倍增不满足,倍数要减去多出得
										// be=be-bz_num_ok;
										if (num % vv.zhqty == 0) {
											be = num / vv.zhqty;
										} else {
											be = Math.floor(num / vv.zhqty)
										}
										bz_num_ok = be * vv.zhqty;
									}
									for (let j = 0; j < bz_num_ok; j++) {
										let index = no_in_arr.findIndex(i => {
											return vv.goods_id === i.goods_id
										})
										if (index > -1) {
											delete_num++
											no_in_arr.splice(index, 1)
										}
									}
								} else {
									zhqty_len = 1;
								}
							}
						}

						if (!zhqty_len) {
							aprice += be * act.zhprice;
							let pop_num = be * act.zhbuyqty - delete_num;
							for (var m = 0; m < pop_num; m++) {
								no_in_arr.pop();
							}
						}
					}

				}
				//算一下剩余的钱
				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.need_downlow_num) {
								gitem.need_downlow_num = 1;
							} else {
								gitem.need_downlow_num++;
							}
							aprice += item.price;
							if (item.offline_price) {
								offline_price += gitem.goods_price - gitem.offline_price;
								offline_num += 1;
							}
						}
					}
				}

				//超量的部分也要计算一下
				if (out_arr.length) {
					for (var m in out_arr) {
						var item = out_arr[m];
						var gitem = get_num2(item);
						if (gitem) {
							if (!gitem.need_downlow_num) {
								gitem.need_downlow_num = item.num;
							} else {
								gitem.need_downlow_num += item.num;
							}
							aprice += item.price * item.num;
							if (item.offline_price && th.data.sales_rules == 2) {
								offline_price += (gitem.goods_price - gitem.offline_price) * item.num;
								offline_num += item.num;
							}

						}
					}
				}



				all_price = aprice;

				//-- 设置还需要购买多少件，享受活动，前段显示 --
				var need_to = car_item.need_list;
				var index = this.find_need_to(need_to, act);
				if (index > -1) {
					need_to.splice(index, 1);
					th.setData({
						[txt]: need_to
					});
				}

			} else {
				for (var ii in goods) {
					var item = goods[ii];
					var gitem = get_num2(item);
					if (gitem && gitem.selected) {

						gitem.need_downlow_num = gitem.goods_num;

						if (item.offline_price && th.data.sales_rules == 2) {
							offline_price += (item.goods_price - item.offline_price) * gitem.goods_num;
							offline_num += gitem.goods_num;
						}
					}
				}
				if (act.zhbuyqty - all_num > need_to_buy) {
					need_to_buy = act.zhbuyqty - all_num;
				}
				//-- 设置还需要购买多少件，享受活动，前段显示 --
				var need_to = car_item.need_list;
				if (all_num <= 0) {
					var index = this.find_need_to(need_to, act);
					if (index > -1) {
						need_to.splice(index, 1);
						th.setData({
							[txt]: need_to
						});
					}
				} else {
					var index = this.find_need_to(need_to, act);
					if (title == '')
						title = "再买" + need_to_buy + "件，下单立享【" + act.zhprice + "元任选" + act.zhbuyqty + "件】";
					if (index != -1) {
						need_to[index].title = title;
					} else {
						if (!need_to) need_to = [];
						var it = {
							id: act.id,
							pickup_id: car_item.pickup_id,
							title: title
						};
						need_to.push(it);
					}
					th.setData({
						[txt]: need_to
					});
				}

			}
			tfeel += all_price;
		}

		console.log('nyf111');
		console.log(car_item);

		var res = {
			tfeel: tfeel,
			offline_price: offline_price,
			offline_num: offline_num
		};
		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 == 7
		})
		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;
	},
	
	zhqty_bz_fun(zhqty_bz,be,zhqty_bz_arr){
        let zhqty_bz_flag =true;
        for(let i=0;i<zhqty_bz.length;i++){
            let item=zhqty_bz[i];
            let bz_num = be * item['zhqty']; //超量倍增
            let num = item['num'] - item['zhqty']; //购买数量减去超量
            if (bz_num > num) {
                zhqty_bz_flag=false;
                zhqty_bz_arr.push(item);
                // zhqty_bz.splice(i,1)
            }
        }
        return zhqty_bz_flag;
    },

	//-- 判断组合购有没有满足，只有在检查的时候，要用新的 ---
	ch_zh_ok(act,gdlist,c_goods,th,item_act_map,is_need_ck_num){
		
		//-- 寻找一下 --
		function get_num(ite) {
			for (let v1 in c_goods) {
				var vh = c_goods[v1];
				if (vh.goods_id == ite.goods_id) {
					return vh;
				}
			}
			return 0;
		}
		var goods =gdlist;
		var all_num=0;
		var need_to_buy=0;
		var all_zhqty=0;
		//-- 是综合考虑到组合购的总数量和限购数量 --
		var min_can_buy=-1;
		var aid=act.id;

		let no_in_arr = []; //剩余的未加入组合购
		let out_arr = []; //超出活动限购的商品放入

		//-- 判断组合购的总数量是不是到了 --
		if(act.zh_num>0){
			//如果是在全选的情况下，那么就需要in_zhact_new来控制数量
			var need_ck=0;
			if(is_need_ck_num){
				need_ck=th.data.in_zhact_new[aid]?th.data.in_zhact_new[aid]:0;
			}
			if(need_ck+act.zh_buy_num>=act.zh_num ){
				return false;
			}else{
				min_can_buy= act.zh_num-act.zh_buy_num-need_ck;
			}
		}

		//-- 判断组合购的限购是不是到了 --
		if(act.buy_limit>0){
			var need_ck0=0;
			if(is_need_ck_num){
				need_ck0=th.data.in_zhact_new[aid]?th.data.in_zhact_new[aid]:0;
			}
			var min_can_buy0=0;
			if(need_ck+item_act_map.buyed>=act.buy_limit ){
				return false;
			}else{
				min_can_buy0=act.buy_limit-item_act_map.buyed-need_ck;
			}
			if(min_can_buy>0){
				if(min_can_buy0<min_can_buy) min_can_buy=min_can_buy0;
			}else{
				min_can_buy=min_can_buy0;
			}
		}

		for (var i in goods) {
			var item = goods[i];
			var cart_num = get_num(item).goods_num;
			var zh_b_num = get_num(item).zh_b_num;
			if (!cart_num) {
				if (item.zhqty) {
					need_to_buy += item.zhqty;
				}
				continue;
			}

			item.cart_num = cart_num;
			//-- 当商品有购买的时候,以及有限购的时候 --
			if (item.buyqty > 0) {
				var cbuy = item.buyqty - zh_b_num;
				var key = item.goods_id + "." + act.id;
				if (is_need_ck_num && th.data.in_zhact_gdmap_new[key]) {
					var cbuy = item.buyqty - zh_b_num - th.data.in_zhact_gdmap_new[key];
				}
				//当可买的数量为0
				if (cbuy <= 0) {
					item.num = 0;
					out_arr.push({
						goods_id: item.goods_id,
						num: cart_num
					})
				} else {
					if (cbuy >= cart_num) item.num = cart_num;
					else {
						item.num = cbuy;
						out_arr.push({
							goods_id: item.goods_id,
							num: cart_num - cbuy
						})
					}
				}
			} else {
				item.num = cart_num;
			}

			if (item.num) {
				all_num += item.num;
				//当有起购数的控制的时候
				if (item.zhqty) {
					all_zhqty += item.zhqty;
					if (item.num < item.zhqty) {
						need_to_buy += item.zhqty - item.num;
					}
					if (item.num > item.zhqty) {
						for (let i = 0; i < item.num - item.zhqty; i++) {
							no_in_arr.push({
								goods_id: item.goods_id,
							})
						}
					}
				}
				else {
					for (let j = 0; j < item.num; j++) {
						no_in_arr.push({
							goods_id: item.goods_id,
						})
					}
				}
			} else {
				if (item.zhqty) {
					need_to_buy += item.zhqty;
				}
			}
		}

		//当满足组合的要求：总数要满足，起购数要满足
		if (all_num >= act.zhbuyqty && !need_to_buy) {
			//默认数量是1
			var act_num=1;
			function sortData(a, b) {
				return a.price - b.price
			}
			if (no_in_arr.length > 0) no_in_arr.sort(sortData);
			if (act.zhbuyqty > all_zhqty) {
				for (let n = 0; n < act.zhbuyqty - all_zhqty; n++) {
					no_in_arr.pop();
				}
			}

			//看一下剩下的数量有没有满足组合购的要求，以及要不要倍增
			if (act.is_bz && no_in_arr.length >= act.zhbuyqty) {

				var bz_num_ok = 0; //超量倍增是否满足
				var zhqty_len = 0; //几个超量倍增
				var delete_num = 0;
				//看一下是几倍
				let be = parseInt(no_in_arr.length / act.zhbuyqty);
				//如果有总数控制的时候
				if (min_can_buy>0) {
					var be1 = min_can_buy - 1;
					if (be1 < be) be = be1;
				}
				if(be) {
					if (act.is_bzyh && zhqty_bz.length > 0) {

						let zhqty_bz_arr = [];
						let zhqty_bz_flag = this.zhqty_bz_fun(zhqty_bz, be, zhqty_bz_arr);
						if (zhqty_bz_flag) {
							for (let i = 0; i < zhqty_bz.length; i++) {
								var vv = zhqty_bz[i];
								for (let j = 0; j < be * vv['zhqty']; j++) {
									let index = no_in_arr.findIndex(i => {
										return vv.goods_id === i.goods_id
									})
									if (index > -1) {
										delete_num++
										no_in_arr.splice(index, 1)
									}
								}
							}
						} else {
							zhqty_len = 1;
							let min_bz_num = Math.min.apply(Math, zhqty_bz_arr.map(function (o) {
								return o['be'];
							}));
							let new_arr = zhqty_bz_arr.filter(ii => {
								return ii['be'] == min_bz_num;
							})
							//-- 要看一下倍数还有没有 ---
							be = new_arr[0].be;
							//如果有总数控制的时候
							if (min_can_buy>0) {
								var be1 = min_can_buy - 1;
								if (be1 < be) be = be1;
							}
							if(be){
								for (let i = 0; i < zhqty_bz.length; i++) {
									let item1 = zhqty_bz[i];
									for (let j = 0; j < be * item1['zhqty']; j++) {
										let index = no_in_arr.findIndex(i => {
											return item1.goods_id === i.goods_id
										})
										if (index > -1) {
											no_in_arr.splice(index, 1)
										}
									}
								}
								act_num+=be;
							}
						}
					}
					if (!zhqty_len) {
						let pop_num = be * act.zhbuyqty - delete_num;
						for (var m = 0; m < pop_num; m++) {
							no_in_arr.pop();
						}
						act_num+=be;
					}
				}
			}

			//-- 参与活动的金额要返回回去 --
			item_act_map.act_num=act_num;
			let goods_map = {};
			//算一下剩余的商品
			if (no_in_arr.length) {
				for (let ii in no_in_arr) {
					let item = no_in_arr[ii];
					if (goods_map[item.goods_id]) {
						var num = goods_map[item.goods_id];
						goods_map[item.goods_id] = ++num;
					} else {
						goods_map[item.goods_id] = 1;
					}
				}
			}
			//超量的部分也要计算一下
			if (out_arr.length) {
				for (var m in out_arr) {
					if (goods_map[item.goods_id]) {
						var num = goods_map[item.goods_id];
						num += item.num
						goods_map[item.goods_id] = num;
					} else {
						goods_map[item.goods_id] = item.num;
					}
				}
			}

			//-- 组合购的商品限购的数组控制 --
			var in_zhact_gdmap={};
			//商品的列表
			for (var u in goods) {
				var ite = goods[u];
				var cart_num = get_num(ite).goods_num;

				if (ite.buyqty > 0 && cart_num > 0) {
					var key = ite.goods_id + "." + act.id;
					if(goods_map[ite.goods_id]){
						cart_num-=goods_map[ite.goods_id];
					}
					if(cart_num>0){
						if (in_zhact_gdmap[key]) {
							in_zhact_gdmap[key] += ite.num;
						} else {
							in_zhact_gdmap[key] = ite.num;
						}
					}

				}
			}
			item_act_map.in_zhact_gdmap=in_zhact_gdmap;
			return true;
		}

		item_act_map.act_num=0;
		return true;
	}



}