import * as api from './index1'

function formatNumber(n) {
    n = n.toString();
    return n[1] ? n : "0" + n;
}

function toDate(number, format) {
    var formateArr = ["Y", "M", "D", "h", "m", "s"];
    var returnArr = [];

    if (number === 0) {
        return 0;
    } else {
        var date = new Date(number * 1000);
    }
    returnArr.push(date.getFullYear());
    returnArr.push(formatNumber(date.getMonth() + 1));
    returnArr.push(formatNumber(date.getDate()));

    returnArr.push(formatNumber(date.getHours()));
    returnArr.push(formatNumber(date.getMinutes()));
    returnArr.push(formatNumber(date.getSeconds()));

    for (var i in returnArr) {
        format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
}

function getWeek(date) {
    //date:'Y-M-D'
    let time = new Date(date).getDay()
    let week;
    if (time == 0) week = "星期日"
    if (time == 1) week = "星期一"
    if (time == 2) week = "星期二"
    if (time == 3) week = "星期三"
    if (time == 4) week = "星期四"
    if (time == 5) week = "星期五"
    if (time == 6) week = "星期六"
    return week;
}

/************************************将数字搞成带逗号的那种*********************************************/
function autoComma(number) {
    // let number = parseInt(numb)
    if (number) {
        let newNum = ""; //中间变量
        let arr = [];
        let arr1 = [];
        let num = ""; //最终结果
        let isDecimal = false; //是不是小数
        let decimal = 0; //小数点所在位置
        let decimalNum = ""; //小数点和以后的数据
        let negative = false; //是不是负数
        // 判断百万级别或者上亿级别
        let thousand = false;
        let Billion = false;

        if (
            (number >= 10000 || number <= -10000) &&
            number < 100000000 &&
            number > -100000000
        ) {
            thousand = true;
            number = number / 10000;
        }
        if (number >= 100000000 || number <= -100000000) {
            thousand = false;
            Billion = true;
            number = number / 100000000;
        }

        if (number < 0) {
            number = Math.abs(number);
            negative = true;
        }
        let numStr = JSON.stringify(number);
        // 如果传入的是小数，逗号的添加位置和整数是有区别滴
        for (let i = 0; i < numStr.length; i++) {
            if (numStr[i] === ".") {
                isDecimal = true;
                decimal = i;
                decimalNum = numStr.slice(decimal, numStr.length);
                // 保留两位小数
                if (decimalNum.length > 2) {
                    decimalNum = decimalNum.slice(0, 3);
                }
                numStr = numStr.slice(0, decimal);
            }
        }
        for (let i = 0; i < numStr.length; i++) {
            arr.push(numStr[numStr.length - i - 1]);
        }
        for (let i = 0; i < arr.length; i++) {
            if ((i + 1) % 3 === 0 && i + 1 !== 0 && i + 1 < arr.length) {
                newNum += arr[i];
                newNum = newNum + ",";
            } else {
                newNum += arr[i];
            }
        }
        for (let i = 0; i < newNum.length; i++) {
            arr1.push(newNum[newNum.length - i - 1]);
        }
        for (let i = 0; i < arr1.length; i++) {
            num += arr1[i];
        }
        if (isDecimal) {
            if (negative) {
                // num = '-' + num + decimalNum
                // return num
                if (thousand) {
                    num = "-" + num + decimalNum + "万";
                    return num;
                } else if (Billion) {
                    num = "-" + num + decimalNum + "亿";
                    return num;
                } else {
                    num = "-" + num + decimalNum;
                    return num;
                }
            } else {
                // num = num + decimalNum
                // return num
                if (thousand) {
                    num = num + decimalNum + "万";
                    return num;
                } else if (Billion) {
                    num = num + decimalNum + "亿";
                    return num;
                } else {
                    num = num + decimalNum;
                    return num;
                }
            }
        } else {
            if (negative) {
                // num = '-' + num
                // return num
                if (thousand) {
                    num = "-" + num + "万";
                    return num;
                } else if (Billion) {
                    num = "-" + num + "亿";
                    return num;
                } else {
                    num = "-" + num;
                    return num;
                }
            } else {
                if (thousand) {
                    num = num + "万";
                    return num;
                } else if (Billion) {
                    num = num + "亿";
                    return num;
                } else {
                    return num;
                }
            }
        }
    } else {
        return 0;
    }
}

//节流
function throttle(fn, delay = 200) {
    var timer = null;
    return function () {
        console.log('throttle')
        var context = this, args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    };
}




//滚动加载信息
const scrollLoad = (e) => {
    // console.log("滚动", e, b);
    const { target } = e;
    const scrllHeight = target.scrollHeight - target.scrollTop;
    const clientHeight = target.clientHeight;
    // console.log("scrllHeight", scrllHeight, clientHeight);
    if (scrllHeight === 0 && clientHeight === 0) {
        return 1
    } else if (scrllHeight - clientHeight == 0) {
        return 2
    }
};

//添加归属权
function changeOwnership(classify, selectProjectId, selectPeopleArr) {
    let obj = {
        keyWord: "",
        tag: 2, //归属权
        opt: 3, //修改
        type:
            classify === "project"
                ? 2
                : classify === "learnPath"
                    ? 1
                    : classify === "course"
                        ? 3
                        : null,
        refId: selectProjectId, //项目id
        // refId: 64,
        pageNo: 1,
        pageSize: 10,
        // studentList: state.selectPeopleArr,
        deptList: [],
        groupList: [],
        studentList: selectPeopleArr,
    };
    console.log("修改归属权obj", obj);
    api
        .optionAuthPerm(obj)
        .then((res) => {
            console.log("修改授权成功", res);
            // closeDrawer();
        })
        .catch((err) => {
            console.log("修改授权失败", err);
        });
}

//name 字段名   value 字段值   perpetual  有效期
const setCookie = (name, value, perpetual) => {
    console.log('存储token到cookie')
    let exdate = new Date()
    exdate.setDate(perpetual * 24 * 60 * 60 * 1000)  //exdate.setDate(exdate.getDate() + 365)
    document.cookie = `${name}=${value};expires=${exdate.toGMTString()};path=/`
    //永久有效
    //document.cookie = name + '=' + value + ';expires=' + 'Fri, 31 Dec 9999 23:59:59 GMT'    
}
//获取cookie数据
//先写一个方法
export function getCookieForName(name) {
    return document.cookie?.split(";").find(e => e.includes(name))?.replace(`${name}=`, '') || ''
}

function getCookie(name) {
    //1.获取cookie字符串
    var cookies = document.cookie;
    //通过；来分割字符串
    var cookie = cookies.split("; ");
    // console.log('cookie', cookie)
    //遍历，使键值对匹配上
    for (var i = 0; i < cookie.length; i++) {
        var arr = cookie[i].split("token=");
        // console.log('arr', arr)
        console.log('name', name)
        // if (arr[0] == name) {
        //   console.log('arr[1]', arr[1])
        //   return arr[1];
        // }
        // console.log('arr[1]', arr[1])
        return arr[1]
    }
    return "";
}

//新建延迟
const commonData = {
    timeout: 50,
}
// const organizationalTree = [// ]

//组织树
const organizationalTree = []

//嵌套页面
const iframeUrl = process.env.VUE_APP_IFRAME_URL
//学员端路由
const studentUrl = process.env.VUE_APP_IFRAME_STUDENT_URL

//二维码
const codeUrl = "https://u-pre.boe.com"
export {
    throttle,
    toDate,
    getWeek,
    autoComma,
    formatNumber,
    setCookie,
    getCookie,
    scrollLoad,
    changeOwnership,
    commonData,
    organizationalTree,
    iframeUrl,
    studentUrl,
    codeUrl,
}