import { login } from '@/api/user'
import { getToken, setToken, removeToken, setUser, getUser, removeUser } from '@/utils/auth'
import { resetRouter } from '@/router'
const userInfo = getUser() && JSON.parse(getUser())

const formatRoles = (userIdentity) => {
  let roles = []
  switch (userIdentity) {
    case 1:
      roles = ['admin']
      break
    case 2:
      roles = ['normal']
      break
    default:
      roles = []
      break
  }
  return roles
}

const state = {
  token: getToken(),
  name: userInfo?.name || '',
  roles: formatRoles(userInfo?.userIdentity),
  shopStatus: userInfo?.shopStatus || '',
  authUserId: userInfo?.authUserId || '',
  userIdentity: userInfo?.userIdentity || '',
  proxyInfo: null,
  shopType: userInfo?.shopType || '',
  brandId: userInfo?.brandId || ''
}

const mutations = {
  // 设置token
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  // 设置用户名
  SET_NAME: (state, name) => {
    state.name = name
  },
  // 设置角色
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  // 设置用户信息
  SET_USERINFO: (state, userInfo) => {
    state.shopStatus = userInfo.shopStatus
    state.authUserId = userInfo.authUserId
    state.userIdentity = userInfo.userIdentity
    state.shopType = userInfo.shopType
    state.brandId = userInfo.brandId
  },
  // 设置代理供应商信息
  SET_PROXY_INFO: (state, proxyInfo) => {
    state.proxyInfo = proxyInfo
  },
  // 设置供应商类型
  SET_SHOP_TYPE: (state, shopType) => {
    state.shopType = shopType
  },
  // 重置用户状态
  RESET_STATE: (state) => {
    state.token = getToken()
    state.name = userInfo?.name || ''
    state.roles = formatRoles(userInfo?.userIdentity)
    state.shopStatus = userInfo?.shopStatus || ''
    state.authUserId = userInfo?.authUserId || ''
    state.userIdentity = userInfo?.userIdentity || ''
    state.proxyInfo = null
    state.shopType = userInfo?.shopType || ''
    state.brandId = userInfo?.brandId || ''
  }
}

const actions = {
  // 登录操作
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ mobileOrName: username.trim(), password: password })
        .then(response => {
          console.log(response)
          const { token, userIdentity, name } = response.data
          commit('SET_TOKEN', token) // 保存token
          commit('SET_USERINFO', response.data) // 保存用户信息
          commit('SET_ROLES', formatRoles(userIdentity)) // 根据用户类型生成角色列表
          commit('SET_NAME', name) // 保存用户名称
          setToken(token) // cokies保存token
          setUser(response.data) // cokies保存当前用户信息
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // 代理用户登录
  proxyLogin({ commit }, proxy) {
    const { name, userIdentity } = proxy
    commit('SET_USERINFO', proxy)
    commit('SET_ROLES', formatRoles(userIdentity)) // 根据用户类型生成角色列表
    commit('SET_NAME', name) // 保存用户名称
  },
  // 代理用户登出
  proxyLogout({ commit }) {
    // 从cookie中重新获取用户信息
    const userInfo = getUser() && JSON.parse(getUser())
    commit('SET_USERINFO', userInfo)
    commit('SET_ROLES', formatRoles(userInfo.userIdentity)) // 根据用户类型生成角色列表
    commit('SET_NAME', userInfo.name) // 保存用户名称
  },
  // 退出登录
  logout({ commit }) {
    commit('RESET_STATE')
    removeToken() // 从cokies中移除token
    resetRouter() // 重置路由
    removeUser() // 从cokies中移除用户信息
  },

  // 移除token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  }
}

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