// 全局mixin
import { mapMutations } from 'vuex'
import { store } from '@/store'
export default {
	methods: {
		...mapMutations({
			showLoad: 'showLoad',
			hideLoad: 'hideLoad'
		}),
		// 物料计算
		computedBagsPrice(skuList) {
			const canBuy = store.getters['indexPage/canBuy']
			const bagOverload = store.getters['indexPage/bagOverload']
			const bagSkuIDs = store.getters['indexPage/bagSkuIDs']
			if (canBuy) {
				// 收费袋子
				if (bagOverload !== '未超出购买限制') {
					skuList.forEach(item => {
						if (bagSkuIDs.includes(item.id)) {
							item.curPrice = item.jxPrice
							item.oriPrice = 0
						}
					})
				} else {
					// 免费袋子
					skuList.forEach(item => {
						if (bagSkuIDs.includes(item.id)) {
							item.curPrice = 1
							item.oriPrice = item.jxPrice
						}
					})
				}
			} else {
				// 收费袋子
				skuList.forEach(item => {
					if (bagSkuIDs.includes(item.id)) {
						item.curPrice = item.jxPrice
						item.oriPrice = 0
					}
				})
			}
		},
		computedBagsPriceObj(obj) {
			const keys = Object.keys(obj)
			keys.forEach(storeID => {
				const list = obj[storeID]
				this.computedBagsPrice(list)
			})
		},

		/**
		 * 全局登录
		 * data 登录的参数
		 * notGetCart 是否获取购物车列表
		 */
		async globalLogin(data, notGetCart) {
			// 登录
			await store.dispatch('login/apiLogin', data ? data : '')

			// 获取购物车
			if (store.getters['login/isLogin'] && !notGetCart && store.getters['indexPage/isMaterial']) {
				await store.dispatch('cartPage/getCarList', store.getters['indexPage/storeID'])		// 加载购物车
				await store.dispatch('addressVue/getList')		// 加载地址列表
			}
		}
	}
}