export function debounce(func, wait) {
    let timeout;
    return function () {
        let context = this;
        let args = arguments;

        if (timeout) clearTimeout(timeout);

        let callNow = !timeout;
        timeout = setTimeout(() => {
            timeout = null;
        }, wait)

        if (callNow) func.apply(context, args)
    }
}


// 获取出生日期
export function getBirth(idCard) {
    if (!idCard) return
    var birthday = "";
    if (idCard != null && idCard != "") {
        if (idCard.length == 15) {
            birthday = "19" + idCard.slice(6, 12);
        } else if (idCard.length == 18) {
            birthday = idCard.slice(6, 14);
        }
        birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
        //通过正则表达式来指定输出格式为:1990-01-01
    }
    return birthday;
}

// 通过身份证号获取性别
export function getSex(idCard) {
    if (!idCard) return
    var sexStr = '';
    if (parseInt(idCard.slice(-2, -1)) % 2 == 1) {
        sexStr = '男';
    } else {
        sexStr = '女';
    }
    return sexStr;
}

// 转换 类似 2 => 二
export function numberToUpperCase(textIndex) {

    let newString = '';
    let newTextIndex = (textIndex + 1) + '';

    function sum(value, index) {
        var newValue = '';
        if ((textIndex === 9)) {
            return !index ? '十' : '';
        }
        let isSeat = (~~textIndex > 9 && ~~textIndex < 19);
        switch (~~value) {
            case 1:
                newValue = !index ? (isSeat ? '' : '一') : '十一';
                break;
            case 2:
                newValue = !index ? (isSeat ? '' : '二') : '十二';
                break;
            case 3:
                newValue = !index ? (isSeat ? '' : '三') : '十三';
                break;
            case 4:
                newValue = !index ? (isSeat ? '' : '四') : '十四';
                break;
            case 5:
                newValue = !index ? (isSeat ? '' : '五') : '十五';
                break;
            case 6:
                newValue = !index ? (isSeat ? '' : '六') : '十六';
                break;
            case 7:
                newValue = !index ? (isSeat ? '' : '七') : '十七';
                break;
            case 8:
                newValue = !index ? (isSeat ? '' : '八') : '十八';
                break;
            case 9:
                newValue = !index ? (isSeat ? '' : '九') : '九十';
                break;
            case 0:
                newValue = '十';
                break;
            default:
                break;
        }
        return newValue;
    }

    for (let i = 0; i < newTextIndex.length; i++) {
        newString += sum(newTextIndex.substring(i, i + 1), i);
    }
    return newString;
}

export function formatMonth(date) {
    if (!date) return '';
    if (typeof date === 'number') date = new Date(date);
    const year = date.getFullYear(); // 获取年份
    const month = date.getMonth() + 1; // 获取月份，月份是从0开始的，所以需要加1
    const formattedMonth = month < 10 ? '0' + month : month; // 如果月份小于10，前面补0

    return `${year}-${formattedMonth}`; // 返回格式化后的字符串
}

export function subtractMonths(value, date = new Date()) {
    // 获取原始日期的年、月、日
    const year = date.getFullYear();
    const month = date.getMonth();
    const day = date.getDate();

    let _month = month - Math.abs(value);
    const _year =  year + Math.floor((_month - (month + 1)) / 12);

    _month = _month - Math.floor((_month - (month + 1)) / 12) * 12;

    // 处理月份天数不足的情况，比如1月31日加1个月不能是2月31日
    let _date = new Date(_year, _month, day);
    if (_date.getMonth() !== _month) {
        const _day = Math.min(day, new Date(_year, _month + 1, 0).getDate());
        _date = new Date(_year, _month, _day);
    }

    return _date;
}

export function numberToChinese(num) {
    if (num === 0) return "零";

    const units = ["", "十", "百", "千"];
    const bigUnits = ["", "万", "亿", "兆"];
    const digits = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];

    let result = "";
    let section = 0; // 当前处理的节（每四位为一节）

    while (num > 0) {
        let part = num % 10000; // 取出当前节的四位数字
        let partStr = "";

        if (part !== 0) {
            let unitIndex = 0; // 当前处理的位（个、十、百、千）
            while (part > 0) {
                let digit = part % 10; // 当前位的数字
                if (digit !== 0) {
                    partStr = digits[digit] + units[unitIndex] + partStr;
                } else if (!partStr.startsWith(digits[0])) {
                    partStr = digits[0] + partStr;
                }
                part = Math.floor(part / 10);
                unitIndex++;
            }
        }

        if (partStr !== "") {
            partStr += bigUnits[section];
            result = partStr + result;
        } else if (result !== "" && !result.startsWith(digits[0])) {
            result = digits[0] + result;
        }

        num = Math.floor(num / 10000);
        section++;
    }

    // 处理特殊情况：连续的零
    result = result.replace(/零{2,}/g, "零");

    // 处理特殊情况：末尾的零
    if (result.endsWith("零")) {
        result = result.slice(0, -1);
    }

    return result;
}