import Cookies from 'js-cookie'
import store from '@/store'

const TokenKey = 'Admin-Token'
const JTOKEN = 'jtoken'
const ATOKEN = 'atoken'
const USER = 'user'
const PASW = 'pasw'

// 获取缓存
export function getCache(key) {
	return JSON.parse(localStorage.getItem(key))
}

// 设置缓存
export function setCache(key, data) {
	localStorage.setItem(key, JSON.stringify(data))
}

// 清除缓存
export function removeCache(key) {
  if(typeof (key) === 'string'){
    	localStorage.removeItem(key)
  }else{
    if(key.length>0){
      key.forEach(item=>{
        localStorage.removeItem(item)
      })
    }
  }
}



// 获取用户列表
export function getEmployeeMap() {
  return JSON.parse(localStorage.getItem('SET_EMPLOYEE_MAP'))
}

export function cookGetToken() {
  return Cookies.get(TokenKey)
}
export function getToken() {
  return localStorage.getItem(TokenKey)
}
export function getJtoken() {
  return Cookies.get(JTOKEN)
}
export function getAtoken() {
  return Cookies.get(ATOKEN)
}
export function getUser() {
  return Cookies.get(USER)
}
export function getPasw() {
  return Cookies.get(PASW)
}


export function localSetToken(token) {
  if(getToken() !== token){
    store.commit('SET_DELETE_USER', {})
    return localStorage.setItem(TokenKey, token)
  }
}
export function setUser(value) {
  return Cookies.set('user', value, { expires: 365 })
}
export function setPasw(value) {
  return Cookies.set('pasw', value, { expires: 365 })
}
export function setJtoken(token) {
  return Cookies.set(JTOKEN, token)
}

export function setAtoken(token) {
  return Cookies.set(ATOKEN, token)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function removeJoken() {
  return Cookies.remove(JTOKEN)
}
export function removeAoken() {
  return Cookies.remove(ATOKEN)
}

export function removeAllToken() {
   localStorage.removeItem(TokenKey);
	 Cookies.remove('user')
	 Cookies.remove('pasw')
	 Cookies.remove(TokenKey)
	 Cookies.remove(ATOKEN)
	 Cookies.remove(JTOKEN)
   return true
}
