const TokenKey = 'A-Token'
const UserIdKey = 'userData'
const TypesKey = 'types'


export function getUserData () {
    return JSON.parse(localStorage.getItem(UserIdKey)); 
}
export function setUserData (data) {
     localStorage.setItem(UserIdKey,JSON.stringify(data))
}
// 获取积分类型
export function getTyps (id) {
	 if(id){
		 var arr=JSON.parse(localStorage.getItem(TypesKey));
		 var item=arr.filter(element => {
		       return  typeof(id)=='string'?element.code==id:element.id==id
		 });
		 return item[0]
	 }else{
		 return JSON.parse(localStorage.getItem(TypesKey));
	 }
}
// 获取积分类型名称
export function getTypsName (id) {
	return getTyps(id).name
}
// 判断是否创始人
export function getIsCreator (key) {
	 let is=false;
	 getUserData().employee_detail.role_list.forEach(item=>{ 
		 if(key==item.name){
			 is=true;
		 }
	 })
	 return is
}

export function supremeAuthority () {//获取当前角色最高权限     判断是否为某项权限：this.$authoritys('dept_manager') 或 this.$supremeAuthority() == 'dept_manager'
	if(getIsCreator('creator')){
		return 'creator'
	}else if(getIsCreator('admin')){
		return 'admin'
	}else if(getIsCreator('point_manager')){
		return 'point_manager'
	}else if(getIsCreator('dept_manager')){
		return 'dept_manager'
	}else if(getIsCreator('employee')){
		return 'employee'
	}
}
export function authoritys (key) {//判断是否为某项权限：this.$authoritys('dept_manager')
	return supremeAuthority() == key
}
//过滤绩效分
 function GlTypes(arr){
	return arr.filter(function(item){
		return item.code!='JX'
	})
}
export function setTyps (data) {
    var arr=GlTypes(data);
    localStorage.setItem(TypesKey,JSON.stringify(arr))
}

export function getToken () {
    return localStorage.getItem(TokenKey)
}

export function setToken (token) {
     localStorage.setItem(TokenKey, token)
}

// 防抖
export function _debounce (fn, delay=500) {
    let timer = null
    return function () {
        let arg = arguments
        clearTimeout(timer)
        timer = setTimeout(() => {
            fn.apply(this, arg)
        }, delay)
    }
}
// 节流
export function _throttle(fn, interval=500) {
  var last;
  var timer;
  return function() {
    var th = this;
    var args = arguments;
    var now = +new Date();
    if (last && now - last < interval) {
      clearTimeout(timer);
      timer = setTimeout(function() { //用户最后一次点击时间间隔小于设置时间执行
        last = now;
        fn.apply(th, args);
      }, interval);
    } else {
      last = now;
      fn.apply(th, args);
    }
  }
}


