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';

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

    isLoading = false;
    startTime;
    endTime;
    timeType = "1";
    dealList;
    chartOption;

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

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

    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.analysisSer.operateUser(this.obj).subscribe(
            (res) => {
                    if(res.errCode == 10000){
                        this.dealList = res.data;
                        this.setEcharts();
                    }
            }
        );
    }

    setEcharts(){
        this.chartOption = {
            xAxis: {
                type: 'category',
                data: this.dealList.map(e=>{
                    return e.username;
                })
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: this.dealList.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();
    }

}
