
import moment from 'moment';
function fixedZero(val) {
    return val * 1 < 10 ? `0${val}` : val;
}
// function getrandom(minnum , maxnum ,total,size){
//     var num = total; //定义整数
//     var length= size;  //定义多个整数的数量
//     var numArr = [];
//     while(length > 1){
//         var rnd = Math.floor(Math.random() * num); //通过JS的随机函数生成随机数
//         num -= rnd;
//         numArr.push(rnd);
//         length --;
//     }
//     numArr.push(num);
//     console.info(numArr);
//     return numArr;
// };getrandom(6,30,45,6)
const filters = {
    // 格式化时间展示多长时间之前
    formatTime(time) {
        let formatTime
        const date = new Date(time)
        const dateObj = {
            year: date.getFullYear(),
            month: date.getMonth(),
            date: date.getDate(),
            hours: date.getHours(),
            minutes: date.getMinutes()
        }
        const newTime = new Date()
        const diff = newTime.getTime() - time
        if (newTime.getDate() === dateObj.date && diff < 60000) {
            formatTime = '刚刚'
        } else if (
            newTime.getDate() === dateObj.date &&
            diff > 60000 &&
            diff < 3600000
        ) {
            formatTime = `${Math.floor(diff / 60000)}分钟前`
        } else if (
            newTime.getDate() === dateObj.date &&
            diff > 3600000 &&
            diff < 86400000
        ) {
            formatTime = `${addZero(dateObj.hours)}:${addZero(dateObj.minutes)}`
        } else if (newTime.getDate() !== dateObj.date && diff < 86400000) {
            formatTime = `昨天${addZero(dateObj.hours)}:${addZero(dateObj.minutes)}`
        } else if (newTime.getFullYear() === dateObj.year) {
            formatTime = `${dateObj.month + 1}月${dateObj.date}日`
        } else {
            formatTime = `${dateObj.year}年${dateObj.month + 1}月${dateObj.date}日`
        }
        return formatTime
    },
    //时间日期格式化
    format(time) {
        const hours = 60 * 60 * 1000;
        const minutes = 60 * 1000;

        const h = Math.floor(time / hours);
        const m = Math.floor((time - h * hours) / minutes);
        const s = Math.floor((time - h * hours - m * minutes) / 1000);
        return `${fixedZero(h)}:${fixedZero(m)}:${fixedZero(s)}`
    },
    //年月日格式化
    getDate(value) {
        let date = new Date(value)
        let y = date.getFullYear()
        let MM = date.getMonth() + 1
        MM = MM < 10 ? '0' + MM : MM
        let d = date.getDate()
        d = d < 10 ? '0' + d : d
        return y + '-' + MM + '-' + d
    },
    //年月日时分秒格式化
    formatDate: function (value) {
        let date = new Date(value)
        let y = date.getFullYear()
        let MM = date.getMonth() + 1
        MM = MM < 10 ? '0' + MM : MM
        let d = date.getDate()
        d = d < 10 ? '0' + d : d
        let h = date.getHours()
        h = h < 10 ? '0' + h : h
        let m = date.getMinutes()
        m = m < 10 ? '0' + m : m
        let s = date.getSeconds()
        s = s < 10 ? '0' + s : s
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
    },
    //时间秒转化为时间格式
    formatS: function (s) {
        var sTime = parseInt(s);// 秒
        var mTime = 0;// 分
        var hTime = 0;// 时
        if (sTime > 60) {//如果秒数大于60，将秒数转换成整数
            //获取分钟，除以60取整数，得到整数分钟
            mTime = parseInt(sTime / 60);
            //获取秒数，秒数取佘，得到整数秒数
            sTime = parseInt(sTime % 60);
            //如果分钟大于60，将分钟转换成小时
            if (mTime > 60) {
                //获取小时，获取分钟除以60，得到整数小时
                hTime = parseInt(mTime / 60);
                //获取小时后取佘的分，获取分钟除以60取佘的分
                mTime = parseInt(mTime % 60);
            }
        }
        var result = '';
        if (sTime >= 0 && sTime < 10) {
            result = "0" + parseInt(sTime) + "";
        } else {
            result = "" + parseInt(sTime) + "";
        }
        if (mTime >= 0 && mTime < 10) {
            result = "0" + parseInt(mTime) + ":" + result;
        } else {
            result = "" + parseInt(mTime) + ":" + result;
        }
        if (hTime >= 0 && hTime < 10) {
            result = "0" + parseInt(hTime) + ":" + result;
        } else {
            result = "" + parseInt(hTime) + ":" + result;
        }
        return result;
    },
    auditRechargeName(sta) {
        var data = {
            null: '-',
            1: '待审核',
            2: '待充值',
            3: '充值完成',
            4: '临时充值',
            5: '审核拒绝',
        }
        return data[sta]
    },
    auditStatusName(sta) {
        var data = {
            null: '-',
            1: '待审核',
            3: '审核通过',
            4: '临时通过',
            5: '审核拒绝',
        }
        return data[sta]
    },
    paymentMediaName(sta) {
        var data = {
            null: '-',
            1: '头条',
            2: '快手',
            7: '磁力金牛',
            9: '广点通',
        }
        return data[sta]
    },
    paymentTypeName(sta) {
        var data = {
            null: '-',
            1: '预付款',
            2: '垫款',
        }
        return data[sta]
    },
    //bidType类型
    bidType(sta) {
        var data = {
            null: '-',
            1: 'CPM',
            2: '封面点击(CPC)',
            6: 'OCPC',
            10: '转化(OCPM)',
            12: '最大转化'
        }
        return data[sta]
    },
    //ocpxActionType类型
    ocpxActionType(sta) {
        var data = {
            null: '-',
            0: '封面点击',
            2: '行为数',
            10: '曝光',
            11: '点击',
            31: '下载完成',
            53: '提交线索',
            180: '激活',
            190: '付费',
            191: '首日ROI',
            348: '有效线索',
            383: '授信',
            384: '完件',
            394: '订单提交',
            324: '唤起应用',
            715: '微信复制优化目标',
            716: '多转化事件',
            396: '注册优化目标',
            731: '广告观看5次',
            732: '广告观看10次',
            773: '关键行为'
        }
        return data[sta]
    },
    //角色类型
    roleCode(value) {
        var state = {
            clip: '剪辑',
            shot: '拍摄',
            plan: '策划',
            plane: '平面',
            admin: '管理员',
            operator: '运营专员',
            kuaishouOperationManager: '快手运营经理',
            designTeamLeader: '设计组长',
            planeLeader: '平面组长',
            touTiaoOperationManager: '头条运营经理',
            operationAssistant: '运营助理',
        }

        if (value) {
            return state[value]
        } else {
            return '暂无'
        }
    },
    //保留两位小数并且变成货币格式
    decimalsHandle(val) {
        if (val && typeof val != 'string') {
            if (val >= 0) {
                val = parseFloat(val).toFixed(2);
                let numberStr = val.toString()
                let str = numberStr.split('.')

                let str0 = str[0].split('').reverse();
                for (let i = 0; i < str0.length; i++) {
                    if ((i + 1) % 4 === 0) {
                        str0.splice(i, 0, ',')
                    }
                }
                str0.reverse()
                let handleResult = ''
                for (let j = 0; j < str0.length; j++) {
                    handleResult += str0[j]
                }

                return handleResult + '.' + str[1]
            } else {
                val = parseFloat(val).toFixed(2);
                let numberStr = val.toString()
                let str = numberStr.split('.')

                let str0 = str[0].substr(1).split('').reverse();
                for (let i = 0; i < str0.length; i++) {
                    if ((i + 1) % 4 === 0) {
                        str0.splice(i, 0, ',')
                    }
                }
                str0.reverse()
                let handleResult = ''
                for (let j = 0; j < str0.length; j++) {
                    handleResult += str0[j]
                }

                return '-' + handleResult + '.' + str[1]
            }

        } else if (typeof val == 'string') {
            return val
        }
        else {
            return 0
        }
    },
    //变成货币格式
    formatCurrency(val) {
        if ((val || val == 0) && typeof val != 'string') {
            let numberStr = val.toString()
            let str = numberStr.split('').reverse()
            for (let i = 0; i < str.length; i++) {
                if ((i + 1) % 4 === 0) {
                    str.splice(i, 0, ',')
                }
            }
            str.reverse()
            let handleResult = ''
            for (let j = 0; j < str.length; j++) {
                handleResult += str[j]
            }
            return handleResult
        } else if (typeof val == 'string') {
            return val
        }
        else {
            return '-'
        }

    },
    //加百分比
    toPercentage(val) {
        if (val) {
            if (typeof val != 'string') {
                return (val * 100).toFixed(2) + '%'
            } else {
                return val
            }
        } else if (typeof val == 'string') {
            return val
        }
        else {
            return 0
        }
    },
    //媒体类型
    handleMediaType(val) {
        switch (val) {
            case 1:
                return '头条'
                break

            case 2:
                return '快手'
                break

            case 3:
                return '头条内广'
                break

            case 4:
                return '快手内广'
                break

            default:
                break
        }
    },
    //文件大小
    sizeShow(value) {
        return (value / 1024).toFixed(2)
    },
    //预警计算符号
    conditionName(val) {
        var data = {
            equal: '=',//'等于',
            not_equal: '≠',//不等于,
            greater: '>',//大于
            less: '<',//小于
            greater_equal: '≥',//大于等于
            less_equal: '≤',//小于等于
            between: '介于'
        }

        return data[val]
    },
    //预警计算符号
    cindicatorName(val) {
        var data = {
            charge: '金额',//'等于',
            dayBudget: '预算',//不等于,
            bid: '出价',//大于
            cost: '成本',//小于
        }

        return data[val]
    },
    operationType(val) {
        var data = {
            up: '提高',//'等于',
            down: '降低',//不等于,
            to: '调整至',//大于
        }

        return data[val]
    },
    //快手年龄
    kuaishouAge(val) {
        var data = {
            18: '18-23岁',//'等于',
            24: '24-30岁',//不等于,
            31: '31-40岁',//大于
            41: '41-49岁',//小于
            50: '50+',//大于等于
        }

        return data[val]
    },
    //快手手机价格
    kuaishouPrice(val) {
        var data = {
            1: '1500以下',
            2: '1501~2000',
            3: '2001~2500',
            4: '2501~3000',
            5: '3001~3500',
            6: '3501~4000',
            7: '4001~4500',
            8: '4501~5000',
            9: '5001~5500',
            10: '5500以上',
        }

        return data[val]
    },
    //快手过滤以转化用户
    kuaishouFilterConvertedLevel(val) {
        var data = {
            0: '不限',
            1: '广告组',
            2: '广告计划',
            3: '本账户',
            4: '公司主体',
            5: 'APP'
        }

        return data[val]
    },
    //场景广告位
    kuaishouSceneId(val) {
        var data = {
            1: '优选广告位',
            3: '视频播放页广告',
            6: '上下滑大屏广告',
            7: '信息流广告',
            5: "联盟广告",
            24: '激励视频广告',
            10: '联盟场景',
            27: '开屏广告'
        }

        return data[val]
    },
    platform(str) {
        var data = {
            1: 'Android应用下载',
            2: 'Android网页游戏',
            3: 'iOS应用下载',
            4: 'iOS网页游戏'
        }
        return data[str]
    },
    // 人群包定向
    customCrowdHandler(val) {
        let str = ''
        switch (val) {
            case 1:
                str = '上传人群'
                break;
            case 2:
                str = '广告人群'
                break;
            case 3:
                str = '主题专区'
                break;
            case 4:
                str = '逻辑规则'
                break;
            case 5:
                str = '人群扩展'
                break;
            case 6:
                str = '平台定制'
                break;
            case 7:
                str = '定制付费'
                break;
            case 8:
                str = '网红粉丝类别'
                break;
            case 9:
                str = '内容付费行为'
                break;
            case 10:
                str = '移动应用安装'
                break;
            case 11:
                str = '快手使用活跃度'
                break;
            case 12:
                str = '行业分类'
                break;
            case 13:
                str = '商业兴趣'
                break;
            case 14:
                str = '固化标签'
                break;
            case 15:
                str = '行业偏好'
                break;
            case 16:
                str = '第三方标签'
                break;
            case 17:
                str = '产品关键词'
                break;

            default:
                break;
        }
        return str
    },
    deviceBrand(str) {
        var data = {
            1: "OPPO",
            2: "VIVO",
            3: "华为",
            4: "小米",
            5: "荣耀",
            6: "三星",
            7: "金立",
            8: "魅族",
            9: "乐视",
            10: "其他",
            11: "苹果",
        }
        return data[str]
    },
    //已安装用户
    disableInstalledAppSwitchLable(str) {
        var data = {
            0: "过滤",
            1: "不限",
        }
        return data[str]
    },

}

// 获取当前月的第一天
function getCurrentMonthFirst() {
    let date = new Date();
    date.setDate(1);
    return date;
}
// 获取当前月的最后一天
function getCurrentMonthLast() {
    let date = new Date();
    let currentMonth = date.getMonth();
    let nextMonth = ++currentMonth;
    let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
    let oneDay = 1000 * 60 * 60 * 24;
    return new Date(nextMonthFirstDay - oneDay);
}
// 今日日期小于15号时和今日日期大于等于15号时
function getCurrentMonthFifty() {
    let date = new Date();
    if (date.getDate() < 15) {
        let bb = date.setTime(new Date().getTime() - 3600 * 1000 * 24 * 15);
        return bb;
    } else if (date.getDate() >= 15) {
        let year = date.getFullYear();
        let month = date.getMonth();
        let myDate = new Date(year, month, 0);
        let lastDay = year + "-" + month + "-" + myDate.getDate();//上个月的最后一天
        return lastDay;
    }

}

export default {
    install: function (Vue) {
        Object.keys(filters).forEach(key => {
            Vue.filter(key, filters[key])
        })
    }

}


