// import { user_info, login } from '@/api/user'
import { eduGet, getUser } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
import { resetRouter } from '@/router'
const getDefaultState = () => {
  return {
    token: getToken(),
    id: '',
    roles: [],
    name: '',
    username: '',
    avatar: '',
    is_init: false,
    userinfo: ''
  }
}

const state = getDefaultState()
const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERNAME: (state, name) => {
    state.username = name
  },
  SET_IS_INIT: (state, is_init) => {
    state.is_init = is_init
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USERINFO: (state, userinfo) => {
    state.userinfo = userinfo
  }
}

const actions = {
  // user login
  // login({ commit }, userInfo) {
  //   const { username, password } = userInfo
  //   return new Promise((resolve, reject) => {
  //     login({ username: username.trim(), password: password })
  //       .then(response => {
  //         const { data } = response
  //         const token = 'JWT ' + data.token
  //         commit('SET_TOKEN', token)
  //         setToken(token)
  //         resolve()
  //       })
  //       .catch(error => {
  //         reject(error)
  //       })
  //   })
  // },
  login({ commit }) {
    return new Promise((resolve, reject) => {
      eduGet()
        .then(response => {
          console.log(response)
          var _user = response[0][0]
          setToken(_user.userid)
          resolve(_user.userid)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getUser({ userid: state.token })
        .then(response => {
          console.log(response)
          // const { data } = response
          const data = response[0][0]
          if (!data) {
            reject('验证失败，请重新登录。')
          }
          // console.log(response)
          commit('SET_USERINFO', data)
          resolve(data)
          // resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // get user info
  // getInfo({ commit, state }) {
  //   return new Promise((resolve, reject) => {
  //     getUser()
  //       .then(response => {
  //         const { data } = response

  //         if (!data) {
  //           reject('验证失败，请重新登录。')
  //         }
  //         console.log(response)

  //         const { id, roles, is_init, cn, avatar, username } = response.data
  //         // roles must be a non-empty array
  //         if (!roles || roles.length <= 0) {
  //           reject('getInfo: 角色必须为非空数组！')
  //         }
  //         commit('SET_NAME', cn)
  //         commit('SET_USERNAME', username)
  //         commit('SET_ROLES', roles)
  //         commit('SET_ID', id)
  //         commit('SET_AVATAR', avatar)
  //         commit('SET_IS_INIT', is_init)
  //         resolve(data)
  //       })
  //       .catch(error => {
  //         reject(error)
  //       })
  //   })
  // },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resetRouter()
      commit('RESET_STATE')
      resolve()
    })
  },

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

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