import Vue from 'vue'
import Vuex from 'vuex'
import modules from './modules'

// state
const state = {
  currentMode: 'web', // 当前的开发环境
  webSocketState: 0, // websocket状态
  webSocketToken: '', // websocket token
  currentUser: {}, // 当前登陆的用户的信息
  clientOrServer: 'client', // client客户端 server管理端
  adminInfo: {}, // 管理员信息
  httpToken: '' // 管理端返回的token
}

// getters
const getters = {
  actionUrl: () => process.env.VUE_APP_UPLOAD_URL, // element-ui 上传的action地址
  webSocketToken: (state) => state.webSocketToken,
  currentUser: (state) => state.currentUser,
  userId: (state) => state.currentUser.SupplierId * 10000 + state.currentUser.UnionId || '',
  currentMode: (state) => state.currentMode,
  superAdmin: (state) => {
    return state.adminInfo.type && state.adminInfo.type === -1
  }
}

// actions
const actions = {}

// mutations
const mutations = {
  // 设置websocketState
  SET_WEB_SOCKET_STATE(state, wsState) {
    state.webSocketState = wsState
  },
  // 设置ws token
  SET_WEBSOCKET_TOKEN: (state, wsToken) => {
    state.webSocketToken = wsToken
  },
  // 设置用户信息
  SET_USER: (state, userInfo) => {
    state.currentUser = userInfo
  },
  // 设置model
  SET_CURRENT_MODE: (state, mode) => {
    state.currentMode = mode
  },
  // 设置客户端还是管理端
  SET_CLIENT_OR_SERVER: (state, flagStr) => {
    state.clientOrServer = flagStr
  },
  // 设置管理员信息
  SET_ADMIN_INO: (state, adminInfo) => {
    state.adminInfo = adminInfo
  },
  // 设置http token
  SET_HTTP_TOKEN: (state, hToken) => {
    state.httpToken = hToken
  }
}

Vue.use(Vuex)

export default new Vuex.Store({
  modules,
  state,
  getters,
  mutations,
  actions,
  strict: process.env.NODE_ENV !== 'production'
})
