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

@Component({
    selector: 'smart-sys-change-count',
    templateUrl: './sys-change-count.component.html',
    styles: []
})
export class SysChangeCountComponent implements OnInit {

    startTime;
    endTime;
    timeType = '4';
    isLoading = false;
    change_level = [{label: '全部', value: ''}, ...change_level];

    chartTopOption;
    topList;

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

    chartLastOption;
    lastList;

    obj = {
        classifyId: null,
        type: null,
        startTime: '',
        endTime: ''
    };
    typeList;

    isDownload = false;

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

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

    //变更分类
    getType() {
        this.systemChangeSer.findByType('sys_change_type').subscribe(
            (res) => {
                if (res.data) {
                    this.typeList = res.data;
                } else {
                    this.message.warning('暂无变更类型');
                }
            }
        );
    }

    //获取Top图表  变更状态统计
    getTopEcharts() {
        this.analysisSer.statusReport(this.obj).subscribe(
            (res) => {
                this.isLoading = false;
                if (res.errCode == 10000) {
                    res.data.forEach(e => {
                        if (e.status == 1) e.name = '待提交';
                        if (e.status == 2) e.name = '待审核';
                        if (e.status == 3) e.name = '审核通过';
                        if (e.status == 4) e.name = '审核未通过';
                        if (e.status == 5) e.name = '退回关闭';
                        if (e.status == 6) e.name = '处理中';
                        if (e.status == 7) e.name = '变更完成';
                    });
                    if (res.data.length > 0) {
                        this.data.left = false;
                        this.topList = res.data;
                        this.setTopEcharts();
                    } else {
                        this.data.left = true;
                    }
                }
            }
        );
    }

    //变更状态统计
    setTopEcharts() {
        this.chartTopOption = {
            title: {
                text: '变更状态统计'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: '{b}:{c}'
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                data: this.topList.map(e => {
                    return e.name;
                }),

            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    show: true,
                    interval: 'auto',
                },
            },
            series: [{
                name: '变更状态统计',
                data: this.topList.map(e => {
                    return e.count;
                }),
                type: 'bar'
            }]
        };
        this.isLoading = false;
    }

    //获取last图表
    getLastEcharts() {
        this.analysisSer.dateReport(this.obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data.length > 0) {
                        this.data.right = false;
                        this.lastList = res.data;
                        this.setLastEcharts();
                    } else {
                        this.data.right = true;
                        this.isLoading = false;
                    }
                }
            }
        );
    }

    //设置last图表
    setLastEcharts() {
        this.chartLastOption = {
            title: {
                text: '数量变化趋势'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: '{b} : {c}'
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                data: this.lastList.map(e => {
                    return e.createDate;
                })
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    show: true,
                    interval: 'auto',
                },
            },
            series: [{
                name: '数量变化趋势',
                data: this.lastList.map(e => {
                    return e.count;
                }),
                type: 'line'
            }]
        };
        this.isLoading = false;
    }

    //时间改变
    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() {
        this.isLoading = true;
        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.getLastEcharts();
        this.getTopEcharts();
    }

    //导出 可用性
    downLoadCommon() {
        this.isDownload = true;
        //下载文件
        const d = {
            format: 'excel'
        };
        this.systemChangeSer.sysChangeExport(d).subscribe(
            (res) => {
                this.isDownload = false;
                this.commonSer.downloadFile('变更.xlsx', res);
            }
        );
    }

}
