// 处理时间
export const handleDate = (dateTimeStamp = new Date()) => {
    dateTimeStamp = dateTimeStamp ? dateTimeStamp : null
    if (!dateTimeStamp) return '刚刚'
    var timestamp = new Date(dateTimeStamp)
    timestamp = timestamp.getTime()
    var minute = 1000 * 60
    var hour = minute * 60
    var day = hour * 24
    var now = new Date().getTime()
    var diffValue = now - timestamp
    var result
    if (diffValue < 0) return

    var dayC = diffValue / day
    var hourC = diffValue / (hour + 1)
    var minC = diffValue / minute
    if (dayC >= 7) {
        let date = new Date(timestamp)
        let Y = date.getFullYear() + "-"
        let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-"
        let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
        // let h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":"
        // let m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
        result = "" + Y + M + D
    } else if (dayC >= 1) result = "" + Math.round(dayC) + "天前"
    else if (hourC >= 1) result = "" + Math.round(hourC) + "小时前"
    else if (minC >= 1) result = "" + Math.round(minC) + "分钟前"
    else result = "刚刚"

    return result
}

// 处理 截止时间
export const handleDeadline = (dateTimeStamp = new Date()) => {
    if (typeof dateTimeStamp == "number") dateTimeStamp = dateTimeStamp ? dateTimeStamp * 1000 : null
    if (typeof dateTimeStamp == "string" && dateTimeStamp.match(/^\d{4}-\d{2}-\d{2}$/)) dateTimeStamp += " 23:59:59";


    var timestamp = new Date(dateTimeStamp)
    timestamp = timestamp.getTime()
    var minute = 1000 * 60
    var hour = minute * 60
    var day = hour * 24
    var now = new Date().getTime()
    var diffValue = timestamp - now
    var result
    if (diffValue < 0) return "投票已"

    var dayC = diffValue / day
    var hourC = diffValue / (hour + 1)
    var minC = diffValue / minute
    if (dayC >= 1) result = "" + Math.round(dayC) + "天后"
    else if (hourC >= 1) result = "" + Math.round(hourC) + "小时后"
    else if (minC >= 1) result = "" + Math.round(minC) + "分钟后"

    return result
}

export const timestampToDate = (timestamp) => {
    var date = new Date(timestamp);
    var year = date.getFullYear();
    var month = ("0" + (date.getMonth() + 1)).slice(-2); // Months are zero based. Add leading 0.
    var day = ("0" + date.getDate()).slice(-2);         // Add leading 0.
    return `${year}-${month}-${day}`;
}


// isblank 是否需要 新标签页 默认是新标签页
export const goToURL = (url, isblank = true) => {
    if (typeof document !== "object") return

    let aTab = document.createElement('a')
    document.body.appendChild(aTab)
    aTab.setAttribute('href', url)
    if (isblank) aTab.setAttribute('target', "_blank")
    aTab.click()
}

export const colourValue = [{
    main: "rgba(44, 186, 230, 1)",
    bg: "rgba(234, 245, 248, 1)",
    bc: "rgba(213, 235, 242, 1)",
}, {
    main: "rgba(49, 215, 46, 1)",
    bg: "rgba(244, 247, 244, 1)",
    bc: "rgba(225, 244, 225, 1)",
}, {
    main: "rgba(106, 117, 217, 1)",
    bg: "rgba(237, 238, 247, 1)",
    bc: "rgba(227, 228, 246, 1)",
}, {
    main: "rgba(172, 183, 46, 1)",
    bg: "rgba(245, 246, 228, 1)",
    bc: "rgba(238, 238, 215, 1)",
}, {
    main: "rgba(38, 223, 190, 1)",
    bg: "rgba(237, 247, 245, 1)",
    bc: "rgba(220, 244, 239, 1)",
}, {
    main: "rgba(242, 122, 71, 1)",
    bg: "rgba(255, 244, 239, 1)",
    bc: "rgba(249, 231, 224, 1)",
}]

export const base62ToDecimal = base62 => {
    const base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    let decimal = 0

    for (let i = 0; i < base62.length; i++) {
        decimal += base.indexOf(base62[i]) * Math.pow(62, base62.length - i - 1)
    }

    return decimal
}