const TokenKey = 'A-Token'
const UserIdKey = 'userData'
const TypesKey = 'types'
import Vue from 'vue'

// 获取唯一标识（uid）
export function generateUUID() {
	var d = new Date().getTime();
	var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		var r = (d + Math.random() * 16) % 16 | 0;
		d = Math.floor(d / 16);
		return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
	});
	return uuid;
};
// 获取缓存
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) {
	localStorage.removeItem(key)
}

function getClass(o) { //判断数据类型
	return Object.prototype.toString.call(o).slice(8, -1);
}

function deepCopy(obj) { //深度拷贝对象
	var result, oClass = getClass(obj);
	if (oClass == "Object") result = {}; //判断传入的如果是对象，继续遍历
	else if (oClass == "Array") result = []; //判断传入的如果是数组，继续遍历
	else return obj; //如果是基本数据类型就直接返回
	for (var i in obj) {
		var copy = obj[i];
		if (getClass(copy) == "Object" || getClass(copy) == "Array") result[i] = deepCopy(
		copy); //递归方法 ，如果对象继续变量obj[i],下一级还是对象，就obj[i][i]
		// else if (getClass(copy) == "Array") result[i] = deepCopy(copy); //递归方法 ，如果对象继续数组obj[i],下一级还是数组，就obj[i][i]
		else result[i] = copy; //基本数据类型则赋值给属性
	}
	return result;
}

export function openError(data) {
	if (process.env.NODE_ENV == 'production') {
		Vue.prototype.$axios('post', '/api/e', data)
	}
}
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);
		}
	}
}
