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

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

    timeType = '4';
    color = color;
    startTime;
    endTime;
    groupList;
    title;
    isVisiable = false;
    isLoading = false;

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

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

    commonList;
    reportList;

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

    //告警明细
    pageCount1 = 10;
    pageNum1 = 1;
    totalNum1;

    modalList = [];

    //下载
    isDownload1 = false;
    isDownload2 = false;

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

    ngOnInit() {
        this.obj.startTime = this.commonSer.getTimeByType('4').startTime;
        this.obj.endTime = this.commonSer.getTimeByType('4').endTime;
        this.getGroup();
    }

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

    //获取分组
    getGroup() {
        this.overAllSer.findGroup().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.groupList = res.data;
                    this.obj.groupid = this.groupList[0].groupid;
                    this.getData();
                }
            }
        );
    }

    //告警总数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) => {
                if (res.errCode == 10000) {
                    if (res.data.length > 0) {
                        this.data.left = false;
                        const response = res.data;
                        this.setTopEcharts(response);
                    } else {
                        this.data.left = true;
                    }
                }
            }
        );
    }

    //设置告警总数TOP10 echarts
    setTopEcharts(data) {
        this.chartTopOption = {
            title: {
                text: '告警总数TOP 10'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
            },
            xAxis: {
                type: 'category',
                data: data.map(e => {
                    return e.name;
                })
            },
            yAxis: {
                type: 'value',
                minInterval: 1,
                axisLabel: {
                    show: true,
                    interval: 'auto',
                    formatter: '{value} 个'
                },
                boundaryGap: [0, 0.1],
            },
            series: [{
                name: '告警总数TOP10',
                data: data.map(e => {
                    return e.size;
                }),
                color: [this.color.yellow],
                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) => {
                if (res.errCode == 10000) {
                    if (res.data.length > 0) {
                        this.data.right = false;
                        const response = res.data;
                        this.setAlarmTrend(response);
                    } else {
                        this.isLoading = false;
                        this.data.right = true;
                    }
                }

            }
        );
    }

    //设置告警趋势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',
                    minInterval: 1,
                    axisLabel: {
                        show: true,
                        interval: 'auto',
                        formatter: '{value} 个'
                    },
                    boundaryGap: [0, 0.1],
                }
            ],
            series: [

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

        this.isLoading = false;
    }

    //常发问题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) => {
                if (res.errCode == 10000) {
                    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) => {
                if (res.errCode == 10000) {
                    this.reportList = res.data.data;
                    this.totalNum = res.data.totalNum;
                }
            }
        );
    }

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

    //时间改变
    changeType() {
        if(this.timeType =='99') return ;
        this.obj.startTime = this.commonSer.getTimeByType(this.timeType).startTime;
        this.obj.endTime = this.commonSer.getTimeByType(this.timeType).endTime;
    }

    //搜索
    search() {
        if (this.timeType == '99') {
            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(item) {
        this.title = '告警明细';
        this.isVisiable = true;
        const data = {
            triggerids: item.waringid
        };
        this.analysisSer.reportCount(data).subscribe(
            (res) => {
                this.modalList = res.data;
            }
        );
    }

    //严重弹窗
    showErrorModal(item) {
        this.title = '严重告警明细';
        this.isVisiable = true;
        const data = {
            triggerids: item.errorid
        };
        this.analysisSer.reportCount(data).subscribe(
            (res) => {
                this.modalList = res.data;
            }
        );
    }

    handleCancel() {
        this.modalList = [];
        this.isVisiable = false;
    }

    //导出 常见问题top 10
    downLoadCommon() {
        this.isDownload1 = true;
        const data = {
            groupid: this.obj.groupid,
            startTime: this.obj.startTime,
            endTime: this.obj.endTime,
            priorityName: this.obj.priorityName,
        };
        this.analysisSer.commonProblemTopImport(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.workSer.downloadTemplate('commonProblemTop', res.data).subscribe(
                        (data) => {
                            this.isDownload1 = false;
                            this.commonSer.downloadFile('常见问题top10.xlsx', data);
                        }
                    );
                } else {
                    this.isDownload1 = false;
                }
            }
        );
    }

    // 导出 统计报告
    downLoadReport() {
        this.isDownload2 = true;
        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.statisticalReportImport(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.workSer.downloadTemplate('statisticalReport', res.data).subscribe(
                        (data) => {
                            this.isDownload2 = false;
                            this.commonSer.downloadFile('统计报告.xlsx', data);
                        }
                    );
                } else {
                    this.isDownload2 = false;
                }
            }
        );
    }
}
