import {listPmsProductCategory} from "@/api/pms/productCategory";
import {allBrand} from "@/api/pms/brand";
import {areaSelect} from "@/api/system/common";
import {listWechatStock} from "@/api/marketing/wechatStock";
import {listUmsMember} from "@/api/ums/member";

const state = {
  productCategories: [],
  brandList: [],
  areaSelect: [],
  couponList: [],
  memberList: []
}
const mutations = {
  SET_PRODUCT_CATEGORIES: (state, value) => {
      state.productCategories = value
    },
  SET_areaSelect: (state, value) => {
    state.areaSelect = value
  },
  SET_BRAND_LIST: (state, value) => {
    state.brandList = value
  },
  SET_COUPON_LIST: (state, value) => {
      state.couponList = value
    },
  SET_MEMBER_LIST: (state, value) => {
    state.memberList = value
  },
}
const actions = {
  loadProductCategories({ commit, state }, force = false) {
      if (!force && state.productCategories.length > 0) {
        return Promise.resolve();
      }
      return listPmsProductCategory({}).then(res => {
        commit('SET_PRODUCT_CATEGORIES', res);
      })
    },
  loadAreaSelect({ commit, state }, force = false) {
    if (!force && state.areaSelect.length > 0) {
      return Promise.resolve();
    }
    return areaSelect({},{}).then(res => {
      commit('SET_areaSelect', res.data);
    })
  },
  loadBrandList({ commit, state }, force = false) {
    if (!force && state.brandList.length > 0) {
      return Promise.resolve();
    }
    return allBrand({}).then(res => {
      commit('SET_BRAND_LIST', res);
    })
  },

  loadCouponList({ commit, state }, force = false) {
    if (!force && state.couponList.length > 0) {
      return Promise.resolve();
    }
    return listWechatStock({}).then(res => {
      commit('SET_COUPON_LIST', res.rows);
    })
  },
  loadMemberList({ commit, state }, force = false) {
    if (!force && state.memberList.length > 0) {
      return Promise.resolve();
    }
    const pageReq = {page: 0, size: 1000};
    return listUmsMember({}, pageReq).then(res => {
      commit('SET_MEMBER_LIST', res.content);
    })
  },

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
