import storage from 'store'
import { login, UserInfo, logout } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          if (response.code === 1) {
            const result = response.data
            storage.set(ACCESS_TOKEN, result, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        UserInfo().then(response => {
          if (response.code === 1) {
            const result = response.data
            var list = []
            if (result.menulist && result.menulist.length > 0) {
              // 遍历菜单进行拼接
              result.menulist.map((per) => {
                if (per.parentid === '00000000-0000-0000-0000-000000000000') {
                    var json = {
                      name: per.name,
                        key: per.id,
                        path: per.path,
                        parentid: per.parentid,
                        component: per.component,
                        children: [],
                        meta: {
                          title: per.title,
                          icon: per.icon,
                          show:per.show
                        }
                      }
                    if (per.redirect && per.redirect.length > 0) {
                        json['redirect'] = per.redirect
                    }
                  list.push(json)
                }
              })
              result.menulist.map((per) => {
                for (var i = 0; i < list.length; i++) {
                  if (per.parentid === list[i].key) {
                    list[i].children.push({
                      key: per.id,
                      path: per.path,
                      component: per.component,
                      children: [],
                      parentid: per.parentid,
                      meta: {
                        title: per.title,
                        icon: per.icon,
                        show:per.show
                      }
                    })
                  }
                }
              })
              commit('SET_ROLES', list)
              commit('SET_INFO', result)
              commit('SET_NAME', { name: result.realname, welcome: welcome() })
              commit('SET_AVATAR', result.avatar)
              resolve(list)
            } else {
              reject(new Error('getInfo: roles must be a non-null array !'))
            }
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        }).finally(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          storage.remove(ACCESS_TOKEN)
        })
      })
    }

  }
}

export default user
