import { login} from '@/api/auth'
import { getToken, setToken, removeToken,setREToken } from '@/utils/auth'
import { resetRouter, asyncRouter } from '@/router'
// import { getMenu } from '@/api/menu'
// import {login} from '@/api/home'
const getDefaultState = () => {
  return {
    token: getToken(),
    refreshToken:'',
    name: '',
    avatar: '',
    menus:null
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  REFRESH_TOKEN: (state, token) => {
    state.refreshToken = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MENU: (state, menus) => {
    state.menus = menus
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { account, password,timestamp } = userInfo
    return new Promise((resolve, reject) => {
      login({ account: account.trim(), pwd: password.trim(),timestamp }).then(response => {
        const { content } = response
        if (response.state == 'Success') {
          commit('SET_TOKEN', content.access_token)
          commit('REFRESH_TOKEN', content.refresh_token)
          setToken(content.access_token)
          setREToken(content.refresh_token)
          window.localStorage.setItem('user_type', content.token_type)
          resolve(content)
        }else{
          reject()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取菜单
  getMenu({ commit, state }) {
    return new Promise((resolve, reject) => {
      getMenu().then(response => {
        let menuList = response.data.menu
        let list = asyncRouter
        let menuIndex = []
        list.map((menu,d)=>{
            if(menu.children){
              let ids = []
              menu.children.map((item,index)=>{//删除不存在的页面
                if(menuList.includes(item.id)){
                  }else{
                  ids.unshift(index)
                }
              })
              ids.map(i=>{
                menu.children.splice(i,1)
              })
            }
          if (!menu.children||!menu.children.length){
              menuIndex.unshift(d)
            }
        })
        menuIndex.map(i=>{
          list.splice(i,1)
        })
        list.push({ path: '*', redirect: '/404', hidden: true })
        commit('SET_MENU', list)
        resolve(list)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
        removeToken() // must remove  token  first
        resetRouter()
        window.localStorage.clear()
        commit('RESET_STATE')
        resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

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

