export default {
	computed: {
		// 整合购物车数据
		$S_tabbar_shopCarts() {
			let shopCarts = this.$M_GS('common', '$S_tabbar_shopCarts');
			
			if (!this.$M_IsEmpty(shopCarts)) {
				let cartList = [];
				for (let key in shopCarts) {
					cartList.push(shopCarts[key]);
				}
				return cartList;
			} else {
				return [];
			}
		},
	},
	methods: {
		// 当数量超出范围，比如最小范围1触发
		numsOverlimit(e, row) {
			if (e === 'minus') {
				this.deleteShops = row;
				this.uModalShow = true;
			}
		},
		goodPlus(e, row) {
			this.handleCar(e, row, 1);
		},
		// 点击商品-
		goodMinus(e, row) {
			this.handleCar(e, row, 2);
		},
		// 点击数量的+-
		numsChg(e, row) {
			// 增加用户体验
			uni.showLoading({
				title: '请稍等',
				mask: true,
			});
			setTimeout(() => {
				uni.hideLoading();
			}, 200);
			this.handleCar(e, row, 3);
		},
		// 处理购物车数据
		handleCar(e, row, idx) {
			let shopCarts = this.$S_tabbar_shopCarts;
			if (shopCarts.length > 0) {
				shopCarts.forEach(item => {
					if (item.imgId === row.imgId) {
						switch (idx) {
							// 给优惠码和优惠价的列表添加一个0值
							case 1:
								item.codeList.push(0);
								item.discountList.push(0);
								break;
								// 去掉优惠码和优惠价的列表的最后一个
							case 2:
								item.codeList.pop();
								item.discountList.pop();
								break;
								// 找到购物车数据对应商品的，重新计算总价格
							case 3:
								item.nums = e.value;
								item.totalPrice = this.$M_Big(item.goodsPrice)
									.mul(item.nums)
									.toNumber();
								break;
						}
					}
				});
			}
			// 把数组转成对象储存到vuex中
			let cartList = this.arrAttrToOBj(shopCarts, 'imgId');
			this.$M_Dp('ACTI_SHOPCARTS', cartList);
		},
		// 根据数组的某个属性转成对象
		arrAttrToOBj(arr, attr, obj = {}) {
			if (arr.length > 0) {
				arr.forEach(item => {
					obj[item[attr]] = item;
				});
				return obj;
			} else {
				return {};
			}
		},
		// 点击删除弹窗的确定按钮
		uModalConfirm() {
			let shopCarts = this.$S_tabbar_shopCarts;
			let list = [];
			if (shopCarts.length > 0) {
				// 根据id筛选出不是删除的那个商品
				list = shopCarts.filter(item => {
					if (item.imgId !== this.deleteShops.imgId) {
						return item;
					} else {
						// 要把优惠码和优惠价清空
						item.codeList = [];
						item.discountList = [];
					}
				});
			}
			// 把数组转成对象储存到vuex中
			let cartList = this.arrAttrToOBj(list, 'imgId');
			this.$M_Dp('ACTI_SHOPCARTS', cartList);
			this.uModalShow = false;
		},
	}
};
