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';
import {WorkService} from '../../work/work.service';
import {CommonService} from '../../shared/common/common.service';

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

    startTime;
    endTime;
    timeType = '1';
    isLoading = false;

    chartTopOption;
    topList;

    //显示无数据图片
    data = {
        left: false,
        right: false,
    };

    chartLastOption;
    lastList;

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

    isDownload = false;

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

    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.findGroup().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.groupList = res.data;
                    this.obj.groupid = this.groupList[0].groupid + '';
                    this.search();
                }
            }
        );
    }

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

    //设置top图表
    setTopEcharts() {
        this.chartTopOption = {
            title: {
                text: '可用性TOP 10'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: '{b} : {c}%'
            },
            xAxis: {
                type: 'category',
                data: this.topList.map(e => {
                    return e.name;
                }),

            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    show: true,
                    interval: 'auto',
                    formatter: '{value} %'
                },
            },
            series: [{
                name: '可用性TOP 10',
                data: this.topList.map(e => {
                    return (e.value).toFixed(2) * 100;
                }),
                type: 'bar'
            }]
        };
        this.isLoading = false;
    }

    //获取last图表
    getLastEcharts() {
        this.obj.type = 'last';
        this.analysisSer.findFailureRate(this.obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data.length > 0) {
                        this.data.right = false;
                        this.lastList = res.data;
                        this.setLastEcharts();
                    } else {
                        this.data.right = true;
                        this.isLoading = false;
                    }
                }
            }
        );
    }

    //设置last图表
    setLastEcharts() {
        this.chartLastOption = {
            title: {
                text: '可用性LAST 10'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: '{b} : {c}%'
            },
            xAxis: {
                type: 'category',
                data: this.lastList.map(e => {
                    return e.name;
                })
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    show: true,
                    interval: 'auto',
                    formatter: '{value} %'
                },
            },
            series: [{
                name: '可用性LAST 10',
                data: this.lastList.map(e => {
                    return (e.value).toFixed(2) * 100;
                }),
                type: 'bar'
            }]
        };
        this.isLoading = false;
    }

    //时间改变
    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;
                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;
                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() {
        this.isLoading = true;
        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();
    }

    //导出 可用性
    downLoadCommon() {
        this.isDownload = true;
        const data = {
            groupid: this.obj.groupid,
            startTime: this.obj.startTime,
            endTime: this.obj.endTime,
        };
        this.analysisSer.findFailureRateImport(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.workSer.downloadTemplate('failureRate', res.data).subscribe(
                        (data) => {
                            this.isDownload = false;
                            this.commonSer.downloadFile('资源可用性统计.xlsx', data);
                        }
                    );
                } else {
                    this.isDownload = false;
                    this.message.error(res.errMsg);
                }
            }
        );
    }

}
