// 返回两个日期之间的月份
export function getDiffDate(minDate, maxDate) {
    let startDate = new Date(minDate.replace(/-/g, '/'));
    let endDate = new Date(maxDate.replace(/-/g, '/'));
    let months = [];
    //把时间的天数都设置成当前月第一天
    startDate.setDate(1)
    endDate.setDate(1)
    // new Date(yyyy-MM-dd) 不知为何有时候小时是 08 有时候是00 
    endDate.setHours(0)
    startDate.setHours(0)
    while (endDate.getTime() >= startDate.getTime()) {
        let year = startDate.getFullYear();
        let month = startDate.getMonth() + 1;
        //加一个月
        startDate.setMonth(month)
        if (month.toString().length == 1) {
            month = "0" + month;
        }
        months.push(year + "-" + month)
    }
    return months;
}

// 将rgb颜色转成hex
export function RtoH(color) {
    var rgb = color.split(',');
    var r = parseInt(rgb[0].split('(')[1]);
    var g = parseInt(rgb[1]);
    var b = parseInt(rgb[2].split(')')[0]);

    var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
    return hex;
}
// 将hex颜色转成rgb
export function HtoR(hex, opacity) {
    var RGBA = "rgba(" + parseInt("0x" + hex.slice(1, 3)) + "," + parseInt("0x" + hex.slice(3, 5)) + "," + parseInt("0x" + hex.slice(5, 7)) + "," + opacity + ")";
    // return { red: parseInt("0x" + hex.slice(1, 3)), green: parseInt("0x" + hex.slice(3, 5)), blue: parseInt("0x" + hex.slice(5, 7)), rgba: RGBA }
    return RGBA;
}

// 189 **** 0222
export function phoneNumA(value) {
    if (value && value.length > 8) {
        return `${value.substring(0, 3)} ${"*".repeat(value.length - 7).replace(/(.{4})/g, "$1 ")}${value.length % 4 ? " " : ""}${value.slice(-4)}`;
    }
    return value;
}

// goto
export function goto(url, json) {
    let params = "";
    if (json) {
        params = [];
        let da = Object.keys(json).map(function (key) {
            if (json[key]) return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
        });
        da.forEach(e => {
            if (e) params.push(e)
        });
        params = params.join("&")
    }
    uni.navigateTo({ url: url + '?' + params });
}

// tidyTpye
export function tidyTpye(va) {
    let a = Number(va)
    switch (a) {
        case 1: return "零售专区";
        case 2: return "批发专区";
        case 3: return "货易优选";
        case 4: return "甄选好物";
        case 5: return "茶宝兑换";
        case 6: return "天天捡漏";
        default: return ""
    }
}

export function hotelStarf(va) {
    switch (va) {
        case 1: return "一星级";
        case 2: return "二星级";
        case 3: return "三星级";
        case 4: return "四星级";
        case 5: return "五星级";
        case 6: return "经济型";
        case 7: return "舒适型";
        case 8: return "高档型";
        case 9: return "豪华型";
        default: return ""
    }
}

// json对象数组按照某个属性排序:降序排列
let compareDesc = (propertyName) => {
    return function (object1, object2) {
        var value1 = object1[propertyName];
        var value2 = object2[propertyName];
        if (value2 < value1) {
            return -1;
        } else if (value2 > value1) {
            return 1;
        } else {
            return 0;
        }
    }
}
// json对象数组按照某个属性排序:升序排列
let compareAsc = (propertyName) => {
    return function (object1, object2) {
        var value1 = object1[propertyName];
        var value2 = object2[propertyName];
        if (value2 < value1) {
            return 1;
        } else if (value2 > value1) {
            return -1;
        } else {
            return 0;
        }
    }
}
// json排序
export function Desc(arr = [], key = "id") {
    return arr.sort(compareDesc(key));
}
export function Asc(arr = [], key = "id") {
    return arr.sort(compareAsc(key));
}

// 判断今明后天
export function getThatDay(d) {
    var td = new Date();
    td = new Date(td.getFullYear(), td.getMonth(), td.getDate());
    var od = new Date(d);
    od = new Date(od.getFullYear(), od.getMonth(), od.getDate());
    var xc = (od - td) / 1000 / 60 / 60 / 24;
    if (xc == 0) {
        return "今天";
    } else if (xc < 2) {
        return "明天";
    } else if (xc < 3) {
        return "后天";
    } else {
        return "";
    }
}
// 计算两个日期之间的天数
export function getDaysBetween(dateString1, dateString2) {
    var startDate = Date.parse(dateString1);
    var endDate = Date.parse(dateString2);
    if (startDate > endDate) {
        return 0;
    }
    if (startDate == endDate) {
        return 1;
    }
    var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
    return days;
}