import env from './conf/env.js'

export default {
    inArray: (search, array) => {
        for (var i in array) {
            if (array[i] == search) {
                return true
            }
        }
        return false
    },
    rpx2px: (rpx) => {
        return rpx / 750 * uni.getSystemInfoSync().windowWidth
    },
    checkPhone: (phone) => {
        let re = /^[0-9]+.?[0-9]*/;
        if (phone.length === 11) {
            return re.test(phone)
        }
        return false;
    },
    jumpUrl(path) {
        uni.navigateTo({
            url: path
        })
    },
    handleTimeCustom(val) {
        val = this.timeStamp(val, 'Y-m-d H:i:s')
        let currentDate = new Date();
        let currentD = currentDate.getDate();
        let currentYear = currentDate.getFullYear();
        let currentMonth = currentDate.getMonth() + 1;
        let date = val.substring(0, 19);
        date = date.replace(/-/g, '/');
        let valDate = new Date(date);
        let valD = valDate.getDate();
        let valYear = valDate.getFullYear();
        let valMonth = valDate.getMonth() + 1;
        // 判断是否属于今天，计算时分
        let difftime = (currentDate - valDate) / 1000;
        let hour = valDate.getHours();
        hour = hour > 9 ? hour : '0' + hour;
        let minute = valDate.getMinutes();
        minute = minute > 9 ? minute : '0' + minute;
        if (currentYear === valYear && currentMonth === valMonth && currentD === valD) {
            return hour + ':' + minute;
        } else {
            // 计算天
            if (currentYear === valYear && currentMonth === valMonth && currentD !== valD) {
                return valMonth + '月' + valD + '日 ' + hour + '时' + minute;
            } else {
                return valYear + '年' + valMonth + '月' + valD + '日 ' + hour + ':' + minute;
            }

        }
    },
    handleTimeCustomCN(val) {
        val = this.timeStamp(val, 'Y-m-d H:i:s')
        console.log(val, '时间0000000');
        let currentDate = new Date();
        let currentD = currentDate.getDate();
        let currentYear = currentDate.getFullYear();
        let currentMonth = currentDate.getMonth() + 1;
        let date = val.substring(0, 19);
        date = date.replace(/-/g, '/');
        let valDate = new Date(date);
        let valD = valDate.getDate();
        let valYear = valDate.getFullYear();
        let valMonth = valDate.getMonth() + 1;
        // 判断是否属于今天，计算时分
        let difftime = (currentDate - valDate) / 1000;
        if (currentYear === valYear && currentMonth === valMonth && currentD === valD) {
            let minute = parseInt(difftime % 3600 / 60);
            if (minute <= 60) {
                return minute === 0 ? '刚刚' : minute + '分钟前';
            } else {
                return (minute * 60).toFixed(0) + '小时前';
            }
        } else {
            // 计算天
            if (currentYear === valYear && currentMonth === valMonth && currentD - 1 === valD) {
                return '昨天';
            } else {
                let days = Math.abs(currentDate.getTime() - valDate.getTime()) / (1000 * 60 * 60 * 24);
                return Math.ceil(days) + '天前';
            }
        }
    },
    timeStamp(timestamp, formats) {
        /*
         ** 时间戳转换成指定格式日期
         ** eg. 
         ** dateFormat(11111111111111, 'Y年m月d日 H时i分')
         ** → "2322年02月06日 03时45分"
         */
        // formats格式包括
        // 1. Y-m-d
        // 2. Y-m-d H:i:s
        // 3. Y年m月d日
        // 4. Y年m月d日 H时i分
        formats = formats || 'Y-m-d';

        var zero = function(value) {
            if (value < 10) {
                return '0' + value;
            }
            return value;
        };
        var myDate = timestamp ? new Date(timestamp) : new Date();

        var year = myDate.getFullYear();
        var month = zero(myDate.getMonth() + 1);
        var day = zero(myDate.getDate());

        var hour = zero(myDate.getHours());
        var minite = zero(myDate.getMinutes());
        var second = zero(myDate.getSeconds());

        return formats.replace(/Y|m|d|H|i|s/ig, function(matches) {
            return ({
                Y: year,
                m: month,
                d: day,
                H: hour,
                i: minite,
                s: second
            })[matches];
        });
    },
    // 时间字符串转换中文时间
    timeToDate(str) {
        let date = str.substring(0, 19);
        date = date.replace(/-/g, '/');
        date = new Date(str);
        let dateObj = {
            year: date.getFullYear(),
            month: date.getMonth() + 1,
            day: date.getDate(),
            hour: date.getHours(),
            minute: date.getMinutes(),
            second: date.getSeconds()
        }
        let string = dateObj.year + '年' + dateObj.month + '月' + dateObj.day + '日 ' + dateObj.hour + '时' + dateObj
            .minute + '分';
        return string
    },
    getAge(strAge) {
        let birArr = strAge.split("-");
        let birYear = birArr[0];
        let birMonth = birArr[1];
        let birDay = birArr[2];

        d = new Date();
        let nowYear = d.getFullYear();
        let nowMonth = d.getMonth() + 1; //记得加1
        let nowDay = d.getDate();
        let returnAge;

        if (birArr == null) {
            return false
        };
        let d = new Date(birYear, birMonth - 1, birDay);
        if (d.getFullYear() == birYear && (d.getMonth() + 1) == birMonth && d.getDate() == birDay) {
            if (nowYear == birYear) {
                returnAge = 0;
            } else {
                let ageDiff = nowYear - birYear;
                if (ageDiff > 0) {
                    if (nowMonth == birMonth) {
                        let dayDiff = nowDay - birDay;
                        if (dayDiff < 0) {
                            returnAge = ageDiff - 1;
                        } else {
                            returnAge = ageDiff;
                        }
                    } else {
                        let monthDiff = nowMonth - birMonth;
                        if (monthDiff < 0) {
                            returnAge = ageDiff - 1;
                        } else {
                            returnAge = ageDiff;
                        }
                    }
                } else {
                    return "出生日期晚于今天，数据有误"; //返回-1 表示出生日期输入错误 晚于今天
                }
            }
            return returnAge;
        } else {
            return ("输入的日期格式错误！");
        }
    }
}
