// import {
// getUserToken
// userlogout,
// getUserInfo,
// getUserMenu
// } from '@/api/shoppingmall/common'
import { getUserToken, userlogout, getUserInfo, getUserMenu } from '@/api/common/loginCenter'
import { setToken, removeToken } from '@/assets/js/utils/auth'
// web页面全局配置文件
import website from '@/assets/js/config/website.js'
import router from '@/router'

const user = {
  state: {
    info: {}, // 用户信息
    menu: [], // 用户侧边栏菜单列表
    crumbsNav: '',
    crumbsNava: '',
    crumbsNavb: '',
    activesa: 1,
    searchNavData: [],
    token: '' // 暂时用不到
  },

  mutations: {
    SET_SHOPMALL_INFO: (state, info) => {
      state.info = info
    },
    SET_SHOPMALL_TOKEN: (state, token) => {
      state.token = token
    },
    SET_SHOPMALL_MENU: (state, menu) => {
      state.menu = menu
    },
    SET_BREAD_NAV: (state, crumbsNav) => {
      state.crumbsNav = crumbsNav
    },
    SET_BREAD_NAVA: (state, crumbsNava) => {
      state.crumbsNava = crumbsNava
    },
    SET_BREAD_NAVB: (state, crumbsNavb) => {
      state.crumbsNavb = crumbsNavb
    },
    SET_ACTIVE_CLASS: (state, activesa) => {
      state.activeas = activesa
    },
    SET_NAV_DATA: (state, searchNavData) => {
      state.searchNavData = searchNavData
    },
    // 登出
    SHOP_SET_LOGOUTTRST: (state) => {
      window.localStorage.clear()
      window.sessionStorage.clear()
      if (website.loginUrl(state.info.currentRol && state.info.currentRol.roleCode)) {
        window.location.href = website.loginUrl(state.info.currentRol && state.info.currentRol.roleCode)// 返回路径
      } else {
        router.push('/shopLogin')
      }
    }

  },

  actions: {
    // 用户登录
    setShopMallUserToken({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        getUserToken(userInfo.userName.trim(), userInfo.password)
          .then(response => {
            commit('SET_SHOPMALL_TOKEN', response.data)
            setToken(response.data)
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    setToken({ dispatch, commit }, token) {
      return new Promise((resolve, reject) => {
        commit('SET_SHOPMALL_TOKEN', token)
        setToken(token)
        dispatch('setShopMallUserToken')// 获取用户信息
        resolve()
      })
    },
    // 获取用户信息
    setShopMallUserInfo({ dispatch, commit }) {
      return new Promise((resolve, reject) => {
        getUserInfo().then(response => {
          commit('SET_SHOPMALL_INFO', response.data)
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      }).then(() => {
        // dispatch('shopmallSetMenu')// 用户侧边栏菜单列表
      }).catch(() => {
        dispatch('shopmallRest')// 登出
      })
    },
    // 登出
    shopmallRest({ dispatch, commit }) {
      return new Promise((resolve, reject) => {
        userlogout()
          .then(response => {
            // commit('SET_SHOPMALL_TOKEN', '')
            dispatch('shopmallLogout')
            resolve()
            // location.href = response.data
          })
          .catch(error => {
            reject(error)
            // 登出
            dispatch('shopmallLogout')
          })
      })
    },
    // 登出
    shopmallLogout({ commit }) {
      return new Promise(resolve => {
        // commit('SET_SHOPMALL_TOKEN', '')
        // commit('SHOP_SET_LOGOUTTRST')
        commit('SET_TOKEN', '')
        commit('SHOP_SET_LOGOUTTRST')
        removeToken()
        resolve()
      })
    },
    shopmallSetMenu({ commit }) {
      return new Promise((resolve, reject) => {
        getUserMenu()
          .then(response => {
            commit('SET_SHOPMALL_MENU', response.data)
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default user
