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

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

    isLoading = false;
    startTime;
    endTime;
    timeType = '4';
    chartOption;
    eventList;

    obj = {
        startTime: '',
        endTime: ''
    };

    noData = false;
    isDownload = false;

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

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

    //获取图表
    getEcharts() {
        this.isLoading = true;
        this.analysisSer.eventByType(this.obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data.length > 0) {
                        this.eventList = res.data;
                        this.setEcharts();
                        this.noData = false;
                    } else {
                        this.message.warning("事件分类统计暂无数据")
                        this.noData = true;
                    }
                }
                this.isLoading = false;
            }
        );
    }

    setEcharts() {
        this.chartOption = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: "{b} : {c}个"
            },
            xAxis: {
                type: 'category',
                data: this.eventList.map(e => {
                    return e.typeName;
                })
            },
            yAxis: {
                type: 'value',
                minInterval: 1,
                axisLabel: {
                    show: true,
                    interval: 'auto',
                    formatter: '{value} 个'
                },
                boundaryGap: [0, 0.1],
            },
            series: [{
                data: this.eventList.map(e => {
                    return e.num;
                }),
                type: 'bar'
            }]
        };
    }

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

    //下载
    downLoad() {
        this.isDownload = true;
        const data = {
            startTime: this.obj.startTime,
            endTime: this.obj.endTime,
        };
        this.analysisSer.exportByType(data).subscribe(
            (data) => {
                this.isDownload = false;
                this.commonSer.downloadFile('事件分类统计.xlsx', data);
            }
        );
    }
}
