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

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

    timeType = '1';
    startTime;
    endTime;
    groupList;
    title;
    isVisiable = false;
    isLoading = false;

    obj = {
        priorityName: null,
        groupid: null,
        startTime: '',
        endTime: '',
    };
    chartTopOption;
    charTrendOption;

    commonList;
    reportList;

    //统计报告分页
    pageCount = pageSize;
    pageNum = 1;
    totalNum;

    warnList = [];

    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();
    }

    getData() {
        this.getAlarmTopEchart();
        this.getAlarmTrend();
        this.getCommonTop();
        this.getAnalysis();
    }

    //获取分组
    getGroup() {
        this.overAllSer.getgroups({}).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.groupList = res.data;
                }
            }
        );
    }

    //告警总数TOP10
    getAlarmTopEchart() {
        const data = {
            groupid: this.obj.groupid,
            startTime: this.obj.startTime,
            endTime: this.obj.endTime,
            priorityName: this.obj.priorityName,
        };
        this.analysisSer.waringCountTop(data).subscribe(
            (res) => {
                    const response = res.data;
                    this.setTopEcharts(response);
            }
        );
    }

    //设置告警总数TOP10 echarts
    setTopEcharts(data){
        this.chartTopOption = {
            title: {
                text: '告警总数TOP 10'
            },
            xAxis: {
                type: 'category',
                data: data.map(e=>{
                    return e.name;
                })
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: data.map(e=>{
                    return e.size;
                }),
                type: 'bar'
            }]
        };
    }

    //告警总数趋势
    getAlarmTrend() {
        const data = {
            groupid: this.obj.groupid,
            startTime: this.obj.startTime,
            endTime: this.obj.endTime,
            priorityName: this.obj.priorityName,
        };
        this.analysisSer.waringTrend(data).subscribe(
            (res) => {
                const response = res.data;
                this.setAlarmTrend(response);
            }
        );
    }

    //设置告警趋势echarts图表
    setAlarmTrend(data) {
        this.charTrendOption = {
            title: {
                text: '告警总数趋势'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            legend: {
                data:['严重','告警']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    data: data.map((e) => {
                        return e.date;
                    })
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [

                {
                    name: '严重',
                    type: 'bar',
                    stack: '严重',
                    data: data.map(e=>{
                        return e.errorcount;
                    })
                },
                {
                    name: '告警',
                    type: 'bar',
                    stack: '告警',
                    data: data.map(e=>{
                        return e.waringcount;
                    })
                }
            ]
        };
    }

        //常发问题TOP10
    getCommonTop() {
        const data = {
            groupid: this.obj.groupid,
            startTime: this.obj.startTime,
            endTime: this.obj.endTime,
            priorityName: this.obj.priorityName,
        };
        this.analysisSer.commonProblemTop(data).subscribe(
            (res) => {
                this.commonList = res.data;
            }
        );
    }

    //统计报告
    getAnalysis() {
        const data = {
            pageCount: this.pageCount,
            pageNum: this.pageNum,
            obj: {
                startTime: this.obj.startTime,
                endTime: this.obj.endTime,
                priorityName: this.obj.priorityName,
                groupid: this.obj.groupid,
            }
        };
        this.analysisSer.statisticalReport(data).subscribe(
            (res) => {
                    this.reportList = res.data.data;
                    this.totalNum = res.data.totalNum;
            }
        );
    }

    //翻页
    change(e){
        this.pageNum = e;
        this.getAnalysis();
    }

    //时间改变
    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.getData();
    }

    //告警弹窗
    showWarnModal(){
        this.title = '告警明细';
        this.isVisiable = true;
    }

    //严重弹窗
    showErrorModal(){
        this.title = '严重告警明细';
        this.isVisiable = true;
    }

    handleCancel(){
        this.isVisiable = false;
    }
}
