// a标签 跳转  isblank 代表跳转新标签
function skipUrl(url, isblank = true) {
    let aTab = document.createElement("a");
    document.body.appendChild(aTab);
    aTab.setAttribute("href", url);
    if (isblank) aTab.setAttribute("target", "_blank");
    aTab.click();
}


// 禁止页面滑动
function pageStop() {
    document.body.style.overflow = "hidden";
}

// 开启页面滑动
function pageMove() {
    document.body.style.overflow = "";
}

// 跳转登录
function goTologin() {
    let url = encodeURIComponent(location.href);
    skipUrl(`https://passport.gter.net/?referer=${url}`, false);
}

// 点击复制
function copy(value, message) {
    let copyInput = document.createElement('input');//创建input元素
    document.body.appendChild(copyInput);//向页面底部追加输入框
    copyInput.setAttribute('value', value);//添加属性，将url赋值给input元素的value属性
    copyInput.select();//选择input元素
    document.execCommand("Copy");//执行复制命令
    //复制之后再删除元素，否则无法成功赋值
    copyInput.remove();//删除动态创建的节点

    if (message) this.$Message.success(message);
    else this.$Message.success('复制成功');
}

// 启动加载中
function startupUnderLoading(that) {
    that.loading = this.$loading({
        lock: true,
        text: '加载中...',
        background: 'rgba(0, 0, 0, 0.7)'
    });
}

// 关闭加载中
function closeUnderLoading(that) {
    that.loading && this.loading.close();
    that.loading = null
}

// 时间戳
function formattedDate(timestamp) {
    if (!timestamp) return
    const date = new Date(timestamp * 1000);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0')
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}`;
    return formattedDate
}

// 不刷新的情况下修改url
function updateURLSearchParams(obj = {}) {
    const params = new URLSearchParams(window.location.search);
    for (const key in obj) {
        params.set(key, obj[key]);
    }
    const newUrl = window.location.pathname + '?' + params.toString();
    window.history.pushState({}, '', newUrl);
}


// 清除所有cookies
function clearCookies() {
    let cookies = document.cookie.split("; ");
    for (let c of cookies) {
        let [name, _] = c.split("=");
        document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
    }
}

export { skipUrl, pageStop, pageMove, goTologin, copy, startupUnderLoading, closeUnderLoading, formattedDate, updateURLSearchParams, clearCookies }