// initial state
const state = {
  all: 0,
  cartData: [],
  total: 0,
  canUseMenuUrl: [],
  erpGoodsFlag: 0,
};

// getters
const getters = {
  allProducts: (state, getters, rootState) => {
    return state.all;
  },

  allCartData: state => state.cartData,

  total: state => {
    state.total = 0;
    for (const item of state.cartData) {
      state.total += item.price;
    }
    return state.total;
  },
};

// actions
const actions = {
  setAll ({ commit }, data) {
    commit('mutations_setAll', data);
  },

  setCartData ({ commit }, item) {
    commit('mutations_CartData', item);
  },

  removecartData ({ commit }, item) {
    commit('mutations_removeCartData', item);
  },
};

// mutations
const mutations = {
  mutations_setAll (state, num) {
    state.all = num;
  },

  mutations_CartData (state, item) {
    state.cartData.push(item);
  },

  mutations_removeCartData (state, item) {
    for (const i in state.cartData) {
      if (state.cartData[i].id === item.id) {
        state.cartData.splice(i, 1);
      }
    }
  },
  // 更新可用菜单可用url
  mutations_updateMenuUrl (state, url) {
    state.canUseMenuUrl = url;
  },
  // 更新erp商品兑换接口开关
  mutations_erpGoodsFlag (state, val) {
    state.erpGoodsFlag = val;
  },
};

export default {
  state,
  getters,
  actions,
  mutations,
};
