import { login, logout, getInfo, getRouter } from '@/api/user'
import { getToken, setToken, removeToken, getUserid, setUserid, removeUserid } from '@/utils/auth'
import { resetRouter } from '@/router'

import { getUrlParam } from '@/utils/util'

const getDefaultState = () => {
  return {
    token: getUrlParam().token || getToken() || "", // token
    userid: getUrlParam().userid || getUserid() || "", // 用户id
    name: '', // 用户名称
    phone: '', // 用户手机
    menus: "", // 菜单
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_USERID: (state, userid) => {
    state.userid = userid
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
  },
  SET_MENUS: (state, menus) => {
    state.menus = menus
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, code, codeValue } = userInfo
    return new Promise((resolve, reject) => {
      login({ userName: username.trim(), password: password, code: code, codeValue: codeValue }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        commit('SET_USERID', data.adminUserId)
        setToken(data.token)
        setUserid(data.adminUserId)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      console.log(state);
      getInfo(state.userid).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        console.log(data);
        const { nickName, userName } = data
        
        // 模拟请求数据
        // const menus = [
          // {
          //   'path': '/example',
          //   'component': 'Layout',
          //   'redirect': '/example/table',
          //   'name': 'Example',
          //   'meta': { 'title': 'Example', 'icon': 'el-icon-s-help', 'roles': ['admin'] },
          //   'children': [
          //     {
          //       'path': 'table',
          //       'name': 'Table',
          //       'component': 'table/index',
          //       'meta': { 'title': 'Table', 'icon': 'table' }
          //     },
          //     {
          //       'path': 'tree',
          //       'name': 'Tree',
          //       'component': 'tree/index',
          //       'meta': { 'title': 'Tree', 'icon': 'tree' }
          //     }
          //   ]
          // },
        // ]
        // menus.push({ path: '*', redirect: '/404', hidden: true })

        commit('SET_NAME', nickName)
        commit('SET_PHONE', userName)
        // commit("SET_MENUS", menus) // 触发vuex SET_MENUS 保存路由表到vuex

        localStorage.setItem("greemall_user", JSON.stringify(data));

        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  getRouter({ commit, state }) {
    return new Promise((resolve, reject) => {
      getRouter({adminUserId: state.userid}).then(response => {
        const menus = response.data;
        // 模拟请求数据
        // const menus = [
        //   {
        //     'path': '/example',
        //     'component': 'Layout',
        //     'redirect': '/example/table',
        //     'name': 'Example',
        //     'meta': { 'title': 'Example', 'icon': 'el-icon-s-help' },
        //     'children': [
        //       {
        //         'path': 'table',
        //         'name': 'Table',
        //         'component': 'table/index',
        //         'meta': { 'title': 'Table', 'icon': 'table' }
        //       },
        //       {
        //         'path': 'tree',
        //         'name': 'Tree',
        //         'component': 'tree/index',
        //         'meta': { 'title': 'Tree', 'icon': 'tree' }
        //       }
        //     ]
        //   },
        // ]
        // menus.push({ path: '*', redirect: '/404', hidden: true })
        commit("SET_MENUS", menus)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        removeUserid()
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

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

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

