import { fetch } from '@/api/index'
import { getToken } from '@/utils/auth'

const state = {
  projectArr: [],   // 项目
  departmentArr: [],    // 部门
  roleArr: [],   // 角色
  outletList: [], // 门店列表
  categoryArr: [], // 商品分类列表
  goodsAttrArr: [], // 获取规格列表
  goodsArr: [] // 商品列表
}

const mutations = {
  SET_PROJECTARR: (state, arr) => {
    state.projectArr = arr
  },
  SET_DEPARTMENTARR: (state, arr) => {
    state.departmentArr = arr
  },
  SET_ROLEARR: (state, arr) => {
    state.roleArr = arr
  },
  SET_OUTLETLIST: (state, val) => {
    state.outletList = val
  },
  SET_CATEGORYARR: (state, val) => {
    state.categoryArr = val
  },
  SET_GOODSATTRARR: (state, val) => {
    state.goodsAttrArr = val
  },
  SET_GOODSARR: (state, val) => {
    state.goodsArr = val
  }
}

const actions = {
  async setProjectArr ({ commit }) {
    if (!getToken()) return
    const data = await fetch('/api/auth/project/get_list', {}, 'get')
    if (data.code === 200) {
      commit('SET_PROJECTARR', data.data)
    }
  },
  async setDepartmentArr ({ commit }) {
    const data = await fetch('/api/auth/department/get_department_list', {}, 'get')
    if (data.code === 200) {
      commit('SET_DEPARTMENTARR', data.data)
    }
  },
  async setRoleArr ({ commit }) {
    const data = await fetch('/api/auth/role/get_role_list', {}, 'get')
    if (data.code === 200) {
      commit('SET_ROLEARR', data.data)
    }
  },
  // 获取门店列表
  async fetchOutletList ({ commit }) {
    const { code, data } = await fetch('/v1/bar/list?page=1&page_size=100&bar_status=1', {}, 'get')
    if (code === 200) {
      commit('SET_OUTLETLIST', data.list)
    }
  },
  // 获取商品分类列表
  async fetchCategory ({ commit }) {
    const { code, data } = await fetch('/v1/bar/product/category/list?page=1&page_size=100&category_status=1', {}, 'get')
    if (code === 200) {
      commit('SET_CATEGORYARR', data.list)
    }
  },
  // 获取规格列表
  async fetchGoodsAttr ({ commit }) {
    const { code, data } = await fetch('/v1/bar/product/attach/list?page=1&page_size=500', {}, 'get')
    if (code === 200) {
      commit('SET_GOODSATTRARR', data.list)
    }
  },
  // 获取商品列表
  async fetchGoodsArr ({ commit }) {
    const { code, data } = await fetch('/v1/bar/product/list?page=1&page_size=1000&product_status=1', {}, 'get')
    if (code === 200) {
      commit('SET_GOODSARR', data.list)
    }
  }
}

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