import { setLocalToken } from '@centit/api-auth'

export default {
  namespaced: true,
  state: {
    token: '',

    isLogin: false,

    userInfo: {},

    unitInfo: {},

    userOptList: {},

    userRoles: [],

    userSettings: {},

    userPosition: {},

    userUnits: [],

    theme: localStorage.getItem('$Theme'),
  },
  mutations: {
    setTheme (state, theme = '') {
      localStorage.setItem('$Theme', theme)
      state.theme = theme
    },
    setToken (state, token) {
      setLocalToken(token)
      state.token = token
    },
    setLoginInfo (state, info) {
      state.isLogin = true
      state.userInfo = info.userInfo || {}
      state.unitInfo = info.unitInfo || {}
      state.userOptList = info.userOptList || {}
      state.userRoles = info.userRoles || []
      state.userSettings = info.userSettings || {}
      state.userPosition = info.userPosition || {}
      state.userUnits = info.userUnits || []
    },
    clearLoginInfo (state) {
      setLocalToken('')
      state.token = ''

      state.isLogin = false
      state.userInfo = {}
      state.userOptList = {}
      state.userRoles = []
      state.userSettings = {}
      state.userPosition = {}
      state.userUnits = []
      state.unitInfo = {}
    },
  },
}
