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 = '1';
    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() {
        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.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;
                    }
                } else {
                    this.message.error(res.errMsg);
                }
                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() {
        const nowDate = new Date();
        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 = new Date(nowDate.getTime() - (nowDate.getDay() - 1) * 86400000);
                this.obj.startTime = this.datePipe.transform(day1, 'yyyy-MM-dd') + ' 00:00:00';
                this.obj.endTime = this.datePipe.transform(nowDate, 'yyyy-MM-dd') + ' 23:59:59';
                break;
            }
            case'3': {   //月报
                day1 = new Date(nowDate.getFullYear(), nowDate.getMonth(), 1);
                this.obj.startTime = this.datePipe.transform(day1, 'yyyy-MM-dd') + ' 00:00:00';
                this.obj.endTime = this.datePipe.transform(nowDate, 'yyyy-MM-dd') + ' 23:59:59';
                break;
            }
            case'4': {   //自定义
                break;
            }
        }
    }

    //搜索
    search() {
        if (this.timeType == '4') {
            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);
            }
        );
    }
}
