import reComputedShopCar from '@/utils/reComputedShopCar.js'
import { switchWeight } from '@/utils'
export default {
    /**
     * 购物车列表
     */
    cartList: (state, getters, allState, allGetters) => {
        let list = state.list

        // #ifdef MP-WEIXIN
        // const fromStoreInfo = allGetters['indexPage/materialInfo'] ? allGetters['indexPage/materialInfo'].fromStoreInfo : null
        // const isMaterial = allGetters["indexPage/isMaterial"]

        // if (isMaterial && fromStoreInfo && fromStoreInfo.packageSwitch === 1) {
        //     // 过滤掉物料袋
        //     list = list.filter(item => item.id !== 6039382)
        // }
        // #endif

        return list
    },

    /**
     * 重新计算购物车数量
     */
    carCount: (state, getters) => {
        const { cartList } = getters
        return reComputedShopCar(cartList)
    },

    /**
     * 获取勾选的数量
     */
    carCheckedCount: (state) => {
        const list = state.list
        let count = 0
        list.forEach(item => {
            if (item.checked) count += item.count
        })
        return count
    },

    /**
     * 计算购物车总价
     */
    totalPrice: (state) => {
        const list = state.list
        let price = 0
        list.forEach(item => {
            if (item.checked) {
                // 被勾选
                if (item.actType && item.count > 1) {
                    price += item.curPrice
                    price += (item.count - 1) * item.oriPrice
                } else {
                    price += item.count * item.curPrice
                }
            }
        })
        return price
    },

    /**
     * 获取有效的购物车商品
     */
    validCarList: (state) => {
        let list = state.list
        return list.filter(item => item.checked)
    },

    /**
     * 重载购物车
     */
    cartNeedReload: (state) => {
        return state.cartNeedReload
    },

    /**
     * 商品总重量
     */
    totalWeight: (state, getters) => {
        const { validCarList } = getters
        return switchWeight(validCarList.reduce((pre, next) => {
            return pre += next.weight * next.count
        }, 0))
    },

    /**
     * 购买备注
     */
    buyerNote: (state) => {
        return state.buyerNote
    }
}