import {Component, OnInit} from '@angular/core';
import {AnalysisService} from '../analysis.service';
import {NzMessageService} from 'ng-zorro-antd';
import {DatePipe} from '@angular/common';
import {OverAllService} from '../../overAll/overAll.service';

@Component({
    selector: 'smart-resource-usabil',
    templateUrl: './resource-usabil.component.html',
    styles: []
})
export class ResourceUsabilComponent implements OnInit {

    startTime;
    endTime;
    timeType = '1';

    chartTopOption;
    topList;
    isTopLoading = false;

    chartLastOption;
    lastList;
    isLastLoading = false;

    obj = {
        type: '',
        groupid: '',
        startTime: '',
        endTime: ''
    }
    ;
    groupid;
    groupList;

    constructor(private analysisSer: AnalysisService, private message: NzMessageService, private datePipe: DatePipe,
                private overAllSer: OverAllService,) {
    }

    ngOnInit() {
        const today = new Date().getTime();
        this.obj.startTime = this.datePipe.transform(today, 'yyyy-MM-dd') + ' 00:00:00';
        this.obj.endTime = this.datePipe.transform(today, 'yyyy-MM-dd') + ' 23:59:59';
        this.getGroup();
    }

    //获取分组
    getGroup() {
        this.overAllSer.getgroups({}).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.groupList = res.data;
                    this.obj.groupid = this.groupList[0].groupid + '';
                    this.getTopEcharts();
                    this.getLastEcharts();
                }else{
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    //获取Top图表
    getTopEcharts() {
        this.obj.type = 'top';
        this.analysisSer.findFailureRate(this.obj).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.topList = res.data;
                    this.setTopEcharts();
                }else{
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    //设置top图表
    setTopEcharts(){
        this.chartTopOption = {
            title: {
                text: '可用性TOP 10'
            },
            xAxis: {
                type: 'category',
                data: this.topList.map(e=>{
                    return e.name;
                }),

            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    show: true,
                    interval: 'auto',
                    formatter: '{value} %'
                },
            },
            series: [{
                data: this.topList.map(e=>{
                    return e.value *100;
                }),
                type: 'bar'
            }]
        };
    }

    //获取last图表
    getLastEcharts() {
        this.obj.type = 'last';
        this.analysisSer.findFailureRate(this.obj).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.lastList = res.data;
                    this.setLastEcharts();
                }else{
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    //设置last图表
    setLastEcharts(){
        this.chartLastOption = {
            title: {
                text: '可用性LAST 10'
            },
            xAxis: {
                type: 'category',
                data: this.lastList.map(e=>{
                    return e.name;
                })
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    show: true,
                    interval: 'auto',
                    formatter: '{value} %'
                },
            },
            series: [{
                data: this.lastList.map(e=>{
                    return e.value *100;
                }),
                type: 'bar'
            }]
        };
    }

    //时间改变
    changeType() {
        const nowDate = new Date().getTime();
        let day1, day2;
        switch (this.timeType) {
            case'1': {
                this.obj.startTime = this.datePipe.transform(nowDate, 'yyyy-MM-dd') + ' 00:00:00';
                this.obj.endTime = this.datePipe.transform(nowDate, 'yyyy-MM-dd') + ' 23:59:59';
                break;
            }
            case'2': {
                day1 = nowDate - 1 * 24 * 60 * 60 * 1000;
                this.obj.startTime = this.datePipe.transform(day1, 'yyyy-MM-dd') + ' 00:00:00';
                this.obj.endTime = this.datePipe.transform(day1, 'yyyy-MM-dd') + ' 23:59:59';
                break;
            }
            case'3': {
                day1 = nowDate - 3 * 24 * 60 * 60 * 1000;
                day2 = nowDate - 1 * 24 * 60 * 60 * 1000;
                this.obj.startTime = this.datePipe.transform(day1, 'yyyy-MM-dd') + ' 00:00:00';
                this.obj.endTime = this.datePipe.transform(day2, 'yyyy-MM-dd') + ' 23:59:59';
                break;
            }
            case'4': {
                day1 = nowDate - 7 * 24 * 60 * 60 * 1000;
                day2 = nowDate - 1 * 24 * 60 * 60 * 1000;
                this.obj.startTime = this.datePipe.transform(day1, 'yyyy-MM-dd') + ' 00:00:00';
                this.obj.endTime = this.datePipe.transform(day2, 'yyyy-MM-dd') + ' 23:59:59';
                break;
            }
            case'5': {
                break;
            }
        }
    }

    //搜索
    search() {
        if (this.timeType == '5') {
            this.obj.startTime = this.datePipe.transform(this.startTime, 'yyyy-MM-dd HH:mm:ss');
            this.obj.endTime = this.datePipe.transform(this.endTime, 'yyyy-MM-dd HH:mm:ss');
        }
        this.getLastEcharts();
        this.getTopEcharts();
    }

}
