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-status',
    templateUrl: './analysis-status.component.html',
    styles: []
})
export class AnalysisStatusComponent implements OnInit {

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

    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.analysisSer.operateStatus(this.obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data.length > 0) {
                        this.statusList = 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.statusList.map(e => {
                    return e.operateStatusName;
                })
            },
            yAxis: {
                type: 'value',
                minInterval: 1,
                axisLabel: {
                    show: true,
                    interval: 'auto',
                    formatter: '{value} 个'
                },
                boundaryGap: [0, 0.1],
            },
            series: [{
                data: this.statusList.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.exportByStatus(data).subscribe(
            (data) => {
                this.isDownload = false;
                this.commonSer.downloadFile('解决状态.xlsx', data);
            }
        );
    }
}
