import Cookies from "js-cookie";
import dayjs from "dayjs";
const timeKey = "hrsaas-timestamp-key";
// 设置时间戳的存储变量
const TokenKey = "accessToken";
const UserIdKey = "userId";
const Username = "userName";

export function getCookie(name) {
    return Cookies.get(name);
}

export function setCookie(name, value) {
    return Cookies.set(name, value);
}

// cookie存储Username
export function setName(name) {
    return Cookies.set(Username, name);
}
// cookie存储token
export function setToken(token) {
    return Cookies.set(TokenKey, token);
}
// cookie存储UserId
export function setUserId(userId) {
    return Cookies.set(UserIdKey, userId);
}

// cookie删除token
export function removeToken() {
    return Cookies.remove(TokenKey);
}
// cookie删除UserId
export function removeUserId() {
    return Cookies.remove(UserIdKey);
}
// cookie删除Username
export function removeName() {
    return Cookies.remove(Username);
}

// 获取时间戳
export function getTimeStamp() {
    return Cookies.get(timeKey);
}
// 设置时间戳
export function setTimeStamp() {
    return Cookies.set(timeKey, Date.now());
}

// 时间格式化
export function parseTime() {
    var myDate = new Date();
    const formatObj = {
        year: myDate.getFullYear(),
        month: myDate.getMonth() + 1 >= 10 ?
            myDate.getMonth() + 1 :
            "0" + (myDate.getMonth() + 1),
        day: myDate.getDate() >= 10 ? myDate.getDate() : "0" + myDate.getDate(),
    };
    return formatObj.year + "-" + formatObj.month + "-" + formatObj.day;
}
//获取1天前时间
export function getStampTime() {
    let stamp1 = new Date(new Date().setHours(0, 0, 0, 0));
    stamp1 = dayjs(stamp1).format("YYYY-MM-DD HH:mm:ss");
    let stamp2 = new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000);
    stamp2 = dayjs(stamp2).format("YYYY-MM-DD HH:mm:ss");
    return {
        startTime: stamp1,
        endTime: stamp2,
    };
}

export function transTreeData(arr, idStr, pidStr, chindrenStr) {
    let r = [],
        hash = {},
        id = idStr,
        pid = pidStr,
        children = chindrenStr,
        len = arr.length;
    for (let i = 0; i < len; i++) {
        hash[arr[i][id]] = arr[i];
    }
    for (let j = 0; j < len; j++) {
        let aVal = arr[j],
            hashVP = hash[aVal[pid]];
        if (hashVP) {
            !hashVP[children] && (hashVP[children] = []);
            hashVP[children].push(aVal);
        } else {
            r.push(aVal);
        }
    }
    return r;
}
export function checkIn(item, arr) {
    const result = arr.find((value) => {
        return value.name == item.meta.title;
    });
    return result;
}