import * as API_Login from '@/api/login'
import Storage from '@/utils/storage'
import * as JwtDecode from 'jwt-decode';
const jwt_decode = JwtDecode.default;

// state
const _user = Storage.getItem('admin_user')
export const state = {
  user: _user ? JSON.parse(_user) : ''
}

// mutations
export const mutations = {
  /**
   * 设置用户信息
   * @param state
   * @param user
   * @constructor
   */
  SET_USER: (state, user) => {
    state.user = user
    Storage.setItem('admin_user', JSON.stringify(user))
    Storage.setItem('admin_uid', user.uid)
  },
  /**
   * 设置访问令牌
   * @param state
   * @param token
   * @constructor
   */
  SET_ACCESS_TOKEN: (state,token) => {
     // 调试信息
   // const expires = new Date(jwt_decode(token).exp * 1000)
    Storage.setItem('admin_access_token', token)
  },
  /**
   * 设置刷新令牌
   * @param state
   * @param token
   * @constructor
   */
  SET_REFRESH_TOKEN: (state,token) => {
   // const expires = new Date(jwt_decode(token).exp * 1000)
    Storage.setItem('admin_refresh_token', token)
  },
  /**
   * 移除用户信息
   * @param state
   * @constructor
   */
  REMOVE_USER: (state) => {
    state.user = ''
    Storage.removeItem('admin_user')
    Storage.removeItem('admin_uid')
  },
  /**
   * 移除访问令牌
   * @param state
   * @constructor
   */
  REMOVE_ACCESS_TOKEN: (state) => {
    Storage.removeItem('admin_access_token')
  },
  /**
   * 移除刷新令牌
   * @param state
   * @constructor
   */
  REMOVE_REFRESH_TOKEN: (state) => {
    Storage.removeItem('admin_refresh_token')
  }
}

// actions
export const actions = {
  /**
   * 用户名登录
   * @param commit
   * @param params
   * @returns {Promise<any>}
   */
  loginAction({ commit }, params) {
    return new Promise((resolve, reject) => {
      if (window.location.href.indexOf('/franchise/login') === -1) {
        API_Login.login(params).then(response => {
          const { access_token, refresh_token, ...user } = response
          commit('SET_USER', user)
          commit('SET_ACCESS_TOKEN', access_token)
          commit('SET_REFRESH_TOKEN', refresh_token)
          resolve()
        }).catch(reject)
      } else {
        API_Login.loginByFranchise(params).then(response => {
          const { access_token, refresh_token, ...user } = response
          commit('SET_USER', user)
          commit('SET_ACCESS_TOKEN', access_token)
          commit('SET_REFRESH_TOKEN', refresh_token)
          resolve()
        }).catch(reject)
      }
    })
  },
  /**
   * 登出
   * @param dispatch
   * @param state
   * @returns {Promise<any>}
   */
  logoutAction({ dispatch }) {
    const uid=JSON.parse(JSON.parse(localStorage.getItem('admin_user')).data).uid
    return new Promise((resolve, reject) => {
      API_Login.logout(uid).then(() => {
        dispatch('fedLogoutAction')
        resolve()
      }).catch(reject)
    })
  },
  /**
   * 设置accessToken
   * @param commit
   * @param accessToken
   */
  setAccessTokenAction: ({ commit }, accessToken) => {
    commit('SET_ACCESS_TOKEN', accessToken)
  },
  /**
   * 设置refreshToken
   * @param commit
   * @param refreshToken
   */
  setRefreshTokenAction: ({ commit }, refreshToken) => {
    commit('SET_REFRESH_TOKEN', refreshToken)
  },
  /**
   * 前端退出
   * @param commit
   */
  fedLogoutAction: ({ commit }) => {
    commit('REMOVE_USER')
    commit('REMOVE_ACCESS_TOKEN')
    commit('REMOVE_REFRESH_TOKEN')
  },
  /* 个人信息更新 */
  updateUserAction({ commit }, user){
    commit('SET_USER', user);
  },
}
