import { getIsShowSelf } from '@/service/api/commonApi.js';

// initial state
const state = {
  enterpriseId: '', // 企业id
  limitCodeList: [],
  isLimit: false, // 是否正在限流
  all: 0,
  cartData: [],
  total: 0,
  leftMenu: [],
  storeObj: {},
  asideShow: false,
  breadcrumb: [],
  layoutTips: '',
  isShowSelf: false, // 展示是否显示 '仅看本人' btn  在app.vue调用 每次刷新都会获取
  openFlag: false, // 列表是否显示创建人
  appletEnable: false, // 小程序是否可用,
  xsxsFlag: false, // 是否开启销售线索
  userId: '',
  departAuth: 0,
  superAdmin: 0,
  keepAlive: ''
};

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

  allCartData: state => state.cartData,

  total: state => {
    state.total = 0;
    for (let item of state.cartData) {
      state.total += item.price;
    }
    return state.total;
  },
  getLimitCodeList: state => state.limitCodeList,
  // 查询是否被限制使用微信模版消息 true：被限制
  getLimitWechatTemplateMessage: state => state.limitCodeList.some(el => el == 'wxTemplateMessage')
};

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

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

  removecartData({ commit }, item) {
    commit('mutations_removeCartData', item);
  },
  async getIsShowSelfData(state) {
    let res = await getIsShowSelf();
    this.commit('mutations_auth', res.result);
  }
};

// mutations
const mutations = {
  // 更新小程序是否可用
  updateAppletEnable(state, val) {
    state.appletEnable = !!val;
  },
  // 更新是否限流
  updateLimit(state, val) {
    state.isLimit = !!val;
  },

  // 更新企业id
  updateEnterpriseId(state, id) {
    state.enterpriseId = id;
  },

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

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

  mutations_removeCartData(state, item) {
    for (let i in state.cartData) {
      if (state.cartData[i].id === item.id) {
        state.cartData.splice(i, 1);
      }
    }
  },
  aside_handler(state, val) {
    state.asideShow = val;
  },
  mutations_breadcrumb(state, val) {
    state.breadcrumb = val;
  },
  mutations_layoutTips(state, val) {
    state.layoutTips = val;
  },
  mutations_auth(state, { openFlag, showFlag }) {
    state.isShowSelf = Boolean(showFlag);
    state.openFlag = Boolean(openFlag);
  },
  updateLimitCodeList(state, limitCodeList) {
    state.limitCodeList = limitCodeList;
  },
  updateSuperAdmin(state, val) {
    state.superAdmin = val;
  },
  updateDepartAuth(state, val) {
    state.departAuth = val;
  },
  updateUserId(state, val) {
    state.userId = val;
  },
  setKeepAlive(state, val) {
    state.keepAlive = val;
  }
};

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