import {AfterViewInit, Component, ElementRef, OnInit, Renderer2, ViewChild} from '@angular/core';
import {JhiEventManager} from 'ng-jhipster';
import { Principal} from '../shared';
import {OverAllService} from '../overAll/overAll.service';
import {color, pageSize} from '../app.constants';
import {AlarmService} from '../alarm/alarm.service';
import {NzMessageService} from 'ng-zorro-antd';
import {LayoutService} from '../layouts/layout.service';
import {SelectGroupComponent} from '../modal/select-group/select-group.component';
import {Router} from '@angular/router';

@Component({
    selector: 'jhi-home',
    templateUrl: './home.component.html',
    styleUrls: [
        'home.css'
    ]

})
export class HomeComponent implements OnInit,AfterViewInit {
    @ViewChild("leftDiv") leftDiv:ElementRef;
    @ViewChild("rightDiv") rightDiv:ElementRef;

    color = color;
    leftOneChart;
    leftTwoChart;
    rightOneChart;
    rightTwoChart;

    warnList= [];

    totalNum;
    groupList;

    pageNum = 1;
    pageCount = 10;
    loading = false;

    obj = {
        leftGroupId:null,
        alarmGroupId:null
    };

    leftOneData = {
        serious:0,
        waring:0,
        normal:0,
        disable:0,
    };
    leftOneDataPre = {
        seriousPer:0,
        warningPer:0,
        normalPer:0,
        disablePer:0,
    };

    leftOneTotal = 0;
    leftTwoData  ={
        serious:0,
        waring:0,
        normal:0,
        disable:0,
    };
    leftTwoDataPre = {
        seriousPer:0,
        warningPer:0,
        normalPer:0,
        disablePer:0,
    };

    leftTwoTotal = 0;

    rightTotal = 0;

    //资源设备数量
    deviceNo;

    constructor(
        private overAllSer:OverAllService,private message:NzMessageService,
        private alarmSer:AlarmService,private renderer:Renderer2,
        private layoutSer:LayoutService,
        private principal: Principal,
        private eventManager: JhiEventManager,
        private router:Router
    ) {
    }

    ngOnInit() {
        this.getAlarmList();
        this.findHostCount();

        this.getGroup();
    }

    ngAfterViewInit(){
        let rightHeight = this.rightDiv.nativeElement.clientHeight;
        this.renderer.setStyle(this.leftDiv.nativeElement,"height",rightHeight +"px");
    }

    //查看详情
    goToDetail(item) {
        this.router.navigate(['app/main/basic-detail'], {
            queryParams: {
                hostId: item.hostid,
                name: item.hostname,
                hostName: item.hostname
            }
        });
    }

    //获取分组
    getGroup() {
        this.overAllSer.findGroup().subscribe(
            (res) => {
                if(res.data){
                    this.groupList = res.data.data;
                    this.obj.leftGroupId = this.groupList[0].groupid;
                    this.countGroupItem();
                }
            }
        );
    }

    //主机组中统计
    countGroupItem(){
        this.layoutSer.findHostCountByStatusByGroup(this.obj.leftGroupId).subscribe(
            (res)=>{
                this.leftOneData = res.data;
                this.setLeftOne(res.data);
            }
        );
        this.layoutSer.findItemStatisticsByGroupid(this.obj.leftGroupId).subscribe(
            (res)=>{
                this.leftTwoData = res.data;
                this.setLeftTwo(res.data);
            }
        )
    }

    setLeftOne(data){
        this.leftOneDataPre = {
            seriousPer:0,
            warningPer:0,
            normalPer:0,
            disablePer:0,
        };
        this.leftOneTotal = data.total;

        if(this.leftOneData.serious > 0){
            let a:any = (this.leftOneData.serious / this.leftOneTotal)* 100;
            this.leftOneDataPre.seriousPer = a.toFixed(2);
        }

        if(this.leftOneData.waring > 0){
            let b:any = ( (this.leftOneData.waring / this.leftOneTotal)* 100 );
            this.leftOneDataPre.warningPer = b .toFixed(2);
        }

        if(this.leftOneData.normal > 0){
            let c:any = ( (this.leftOneData.normal / this.leftOneTotal)* 100);
            this.leftOneDataPre.normalPer = c .toFixed(2);
        }

        if(this.leftOneData.disable > 0){
            let d:any = ( (this.leftOneData.disable / this.leftOneTotal)* 100 );
            this.leftOneDataPre.disablePer = d .toFixed(2);
        }

        this.leftOneChart =
            {
                title: {
                    text: "" +this.leftOneTotal+' 台\n设备',
                    x: '41%',
                    y: '40%',
                    itemGap: 50,
                    textStyle: {
                        color: '#333',
                        fontSize: 18,
                        fontWeight: 'normal',
                    },
                    subtextStyle: {
                        color: '#de4751',
                        fontSize: 12
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },

                series: [
                    {
                        name:"设备",
                        type: 'pie',
                        clockWise: true,
                        center: ["46%", "50%"],
                        radius: ['40%', '60%'],
                        color: ['#f93215', '#ff8400', '#2fb35b', '#a0a0a0'],
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [
                            {value: data.serious, name: '严重'},
                            {value: data.warning, name: '告警'},
                            {value: data.normal, name: '正常'},
                            {value: data.disable, name: '禁止'},
                        ],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
    }

    setLeftTwo(data){
        this.leftTwoDataPre = {
            seriousPer:0,
            warningPer:0,
            normalPer:0,
            disablePer:0,
        };
        this.leftTwoTotal = data.total;
        if(this.leftTwoData.serious > 0){
            let w:any = ( (this.leftTwoData.serious / this.leftTwoTotal)*100 );
            this.leftTwoDataPre.seriousPer = w.toFixed(2);
        }

        if(this.leftTwoData.waring > 0){
            let x:any = ( (this.leftTwoData.waring / this.leftTwoTotal)*100 );
            this.leftTwoDataPre.warningPer = x .toFixed(2);
        }

        if(this.leftTwoData.normal > 0){
            let y:any = ( (this.leftTwoData.normal / this.leftTwoTotal)*100 );
            this.leftTwoDataPre.normalPer = y.toFixed(2);
        }

        if(this.leftTwoData.disable > 0){
            let z:any = ( (this.leftTwoData.disable / this.leftTwoTotal)*100);
            this.leftTwoDataPre.disablePer = z.toFixed(2);
        }

        this.leftTwoChart =
            {
                title: {
                    text: this.leftTwoTotal+'个\n监测点',
                    x: '41%',
                    y: '40%',
                    itemGap: 50,
                    textStyle: {
                        color: '#333',
                        fontSize: 18,
                        fontWeight: 'normal',
                    },
                    subtextStyle: {
                        color: '#de4751',
                        fontSize: 12
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },

                series: [
                    {
                        name:"监测点",
                        type: 'pie',
                        clockWise: true,
                        center: ["47%", "50%"],
                        radius: ['40%', '60%'],
                        color: ['#f93215', '#ff8400', '#2fb35b', '#a0a0a0'],
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [
                            {value: data.serious, name: '严重'},
                            {value: data.warning, name: '告警'},
                            {value: data.normal, name: '正常'},
                            {value: data.disable, name: '禁止'},
                        ],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
    }

    findHostCount(){
        this.layoutSer.findHostCount().subscribe(
            (res)=>{
                if(res.data && res.data.length > 0){
                    this.rightTwoChart = {
                        tooltip : {
                            trigger: 'item',
                            formatter: "{a} <br/>{b} : {c} ({d}%)"
                        },

                        series : [
                            {
                                name: '',
                                type: 'pie',
                                radius : '55%',
                                center: ['50%', '60%'],
                                data:res.data.map(e=>{
                                    const arr = {
                                        value:e.hostcount,
                                        name:e.name
                                    }
                                    return arr;
                                }),
                                itemStyle: {
                                    emphasis: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
                }
            }
        )
    }

    getAlarmList() {
        this.loading = true;
        const data = {
            pageNum: this.pageNum,
            pageCount: this.pageCount,
            obj:{
                isWaring:'yes',
                groupid: this.obj.alarmGroupId,
            }
        };
        this.alarmSer.eventFind(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data) {
                        this.warnList = res.data.data;
                        this.totalNum = res.data.totalNum;
                    }
                }
                this.loading = false;
            }
        );
    }

    change(e){
        if(e > 0){
            this.pageNum = e;
            this.getAlarmList();
        }
    }
}
