import { login, fetchUserInfo, fetchUserVipInfo } from '@/api/user'
import { resetRouter } from '@/router'
import { getToken, getUserInfo, removeToken, removeUserInfo } from '@/utils/auth'

// 用户信息
const state = {
  token: getToken(),
  userInfo: null,
  name: '',
  shopStatus: '',
  authUserId: '',
  userIdentity: '',
  shopType: '',
  infoId: '',
  loginAccount: '',
  mobile: '',
  vipStatus: '',
  roles: [],
  menusList: [],
  permissions: [],
  vipInfo: {},
  prefix: '' // 供应商前缀标识符
}

const mutations = {
  // 重置用户信息
  SAVE_USER_INOF: (state, playload) => {
    const userInfo = playload
    state.name = userInfo.name || ''
    state.shopStatus = userInfo.shopStatus || ''
    state.authUserId = userInfo.authUserId || ''
    state.userIdentity = userInfo.userIdentity || ''
    state.shopType = userInfo.shopType || ''
    state.infoId = userInfo.infoId || ''
    state.loginAccount = userInfo.loginAccount || ''
    state.mobile = userInfo.mobile || ''
    state.vipStatus = userInfo.vipStatus || ''
    state.prefix = userInfo.prefix || ''
  },
  // 设置token
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  // 设置用户角色
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  // 保存菜单列表
  SET_MENUS: (state, menus) => {
    state.menusList = menus
  },
  // 保存权限列表
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  // 保存会员信息
  SET_VIPINFO: (state, info) => {
    state.vipInfo = info
  }
}

const actions = {
  // 登录操作
  async login({ commit }, userInfo) {
    const { username, password } = userInfo
    try {
      const res = await login({ mobileOrName: username.trim(), password: password })
      commit('SAVE_USER_INOF', res.data)
      commit('SET_TOKEN', res.data.token)
      return res
    } catch (error) {
      commit('SAVE_USER_INOF', {})
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      return error
    }
  },

  // 获取用户菜单列表
  fetchUserInfo({ commit, state }) {
    return fetchUserInfo({ authUserId: state.authUserId }).then((res) => {
      console.log(res)
      commit('SET_ROLES', res.data.roles)
      commit('SET_MENUS', res.data.menus)
      commit('SET_PERMISSIONS', res.data.permissions)
    })
  },

  // 获取用户会员信息
  fetchUserVipInfo({ commit, state }) {
    if (state.userIdentity === 1) return
    fetchUserVipInfo({ authUserId: state.authUserId }).then((res) => {
      commit('SET_VIPINFO', res.data)
    })
  },

  // 退出登录
  logout({ commit }) {
    removeToken()
    removeUserInfo()
    resetRouter()
    commit('SAVE_USER_INOF', {})
    commit('SET_TOKEN', '')
    commit('SET_ROLES', [])
  },

  // 代理登录
  proxyLogin({ commit, rootGetters }) {
    commit('SET_ROLES', [])
    commit('SAVE_USER_INOF', rootGetters.proxyInfo)
    if (rootGetters.proxyInfo.token) {
      commit('SET_TOKEN', rootGetters.proxyInfo.token)
    }
  },

  // 代理登出
  proxyLogout({ commit }) {
    commit('SET_ROLES', [])
    commit('SAVE_USER_INOF', getUserInfo())
    commit('SET_VIPINFO', {})
    commit('SET_TOKEN', getToken())
  }
}

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