import {Component, OnInit, ViewChild, ElementRef, OnDestroy} from '@angular/core';
import {AlarmService} from '../alarm.service';
import {NzMessageService} from 'ng-zorro-antd';
import {SendLogComponent} from '../modal/send-log/send-log.component';
import {color, pageSize} from '../../app.constants';
import {WarnListComponent} from '../../modal/warn-list/warn-list.component';
import {OverAllService} from '../../overAll/overAll.service';
import {CommonService} from '../../shared/common/common.service';
import {WorkService} from '../../work/work.service';

@Component({
    selector: 'smart-now-alarm',
    templateUrl: './now-alarm.component.html',
    styles: [
            `
            .tag-warn{
                padding: 5px;
            }
            .tag-warn span{
                border: 1px solid #ccc;
                margin-right: 10px;
                display: inline-block;
                margin-bottom: 10px;
                padding: 2px 4px;
                border-radius: 4px;
            }

            .tag-warn span.select-border {
                border: 1px solid #1626e2;
                border-radius: 5px;
                padding: 2px;
                margin-right: 5px;
            }

            :host ::ng-deep .tag-warn .ant-tag {
                margin-right: 0px;
            }
        `
    ]
})
export class NowAlarmComponent implements OnInit, OnDestroy {
    @ViewChild('sendLog') sendLog: SendLogComponent;
    @ViewChild('warnList') warnList: WarnListComponent;

    color = color;
    alarmTotal;
    warnCountList = [];
    loading = false;
    pageCount = pageSize;
    pageNum = 1;
    totalNum;
    time_;

    selectHostId;
    isDownload;
    obj = {
        name: ''
    };

    constructor(public alarmSer: AlarmService, public message: NzMessageService, private workSer: WorkService,
                public overAllSer: OverAllService, private commonSer: CommonService) {
    }

    ngOnInit() {
        this.getType();
        this.search();
        this.time_ = setInterval(() => {
            this.search();
        }, 2 * 60 * 1000);
    }

    ngOnDestroy() {
        clearInterval(this.time_);
    }

    search() {
        const obj = {
            isWaring: 'yes',
            name: this.obj.name,
        };
        this.warnList.outGet(obj);
    }

    getType() {
        this.overAllSer.findHostWarningCount().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.warnCountList = res.data;
                }
            }
        );
    }

    change(e) {
        this.pageNum = e;
    }

    //打开modal
    showModal(data) {
        this.sendLog.showModal(data);
    }

    getTotal(num) {
        this.alarmTotal = num;
    }

    //导出文件
    downLoadExcel() {
        this.isDownload = true;
        const data = {
            isWaring: 'yes',
            hostid: this.selectHostId,
        };
        this.alarmSer.getExportUrlEvent(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.workSer.downloadTemplate('alarm', res.data).subscribe(
                        (data) => {
                            this.isDownload = false;
                            this.commonSer.downloadFile('当前告警.xlsx', data);
                        }
                    );
                }
                this.isDownload = false;
            }
        );
    }
}
