<template>
    <div></div>
</template>

<script>
var dayjs = require("dayjs");
var daytime = 24*60*60*1000;
export default  {
    init(vue){
        vue.filter('timeFormat', function(value) {
            if(!value) return "";
            return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
        })
        vue.filter('dateFormat', function(value) {
            if(!value) return "";
            return dayjs(value).format('YYYY-MM-DD')
        })

        var getStartDay = (date)=>{
            var day = new Date(date.getTime());
            day.setHours(0)
            day.setMinutes(0)
            day.setSeconds(0)
            day.setMilliseconds(0)
            return day;
        }
        var getEndDay = (date)=>{
            var day = new Date(date.getTime());
            day.setHours(23)
            day.setMinutes(59)
            day.setSeconds(59)
            day.setMilliseconds(999)
            return day;
        }
        
        vue.prototype.getPickerOptions = ()=>{
            return {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        var start = new Date();
                        if(picker.diffTimeZone){
                            start = picker.$dayjs().add(picker.diffTimeZone, "hour").toDate();
                        }
                        picker.$emit('pick', [getStartDay(start), getEndDay(start)]);
                    }
                },{
                    text: '昨天',
                    onClick(picker) {
                        var start = new Date();
                        if(picker.diffTimeZone){
                            start = picker.$dayjs().add(picker.diffTimeZone, "hour").toDate();
                        }
                        start.setTime(start.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', [getStartDay(start), getEndDay(start)]);
                    }
                },{
                    text: '本周',
                    onClick(picker) {
                        const cur = new Date();
                        var curweek = cur.getDay() -1;
                        var endday = 6-curweek;
                        var start = new Date(cur.getTime()-curweek*daytime);
                        var end = new Date(cur.getTime()+endday*daytime);
                        picker.$emit('pick', [getStartDay(start), getEndDay(end)]);
                    }
                },{
                    text: '本月',
                    onClick(picker) {
                        const start = new Date();
                        start.setDate(1);
                        const end = new Date();
                        var days = dayjs().daysInMonth()
                        end.setDate(days);
                        picker.$emit('pick', [getStartDay(start), getEndDay(end)]);
                    }
                },{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        end.setTime(end.getTime() - 3600 * 1000 * 24 );
                        picker.$emit('pick', [getStartDay(start), getEndDay(end)]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [getStartDay(start), getEndDay(end)]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [getStartDay(start), getEndDay(end)]);
                    }
                }]
            }
        }
        
        
        vue.prototype.isWeek = (dataarr)=>{
            if(!dataarr || !dataarr.length || dataarr.length<2 ) return false;
            try{
                if(dataarr[0].getDay()==1 && dataarr[1].getDay()==0){ return true; }
            }catch(ex){ }
            return false;
        }
        
        vue.prototype.isMonth = (dataarr)=>{
            if(!dataarr || !dataarr.length || dataarr.length<2 ) return false;
            try{
                if(dataarr[0].getDate()!=1){ return false; }
                var startmonth = dataarr[0].getMonth();
                var endmonth = dataarr[1].getMonth();
                if(startmonth!=endmonth) return false;
                var nextDay = new Date(dataarr[1].getTime()+24*60*60*1000);
                if(endmonth == nextDay.getMonth()){ return false; }
            }catch(ex){ }
            return true;
        }
    }
}

</script>