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

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

    isLogin: false,

    userInfo: {},

    unitInfo: {},

    userOptList: {},

    userRoles: [],

    userSettings: {},

    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.userOptList = info.userOptList || {}
      state.userRoles = info.userRoles || []
      state.userSettings = info.userSettings || {}
      state.userUnits = info.userUnits || []
      state.unitInfo = state.userUnits.filter(u => u.isPrimary === 'T')[0] || {}
    },
    clearLoginInfo (state) {
      setLocalToken('')
      state.token = ''

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