import {Component, OnInit, ViewChild} from '@angular/core';
import {WarnListComponent} from '../../modal/warn-list/warn-list.component';
import {OverAllService} from '../../overAll/overAll.service';
import {DatePipe} from '@angular/common';
import {AlarmService} from '../alarm.service';
import {pageSize} from '../../app.constants';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {AlarmTargetComponent} from '../modal/alarm-target/alarm-target.component';
import {CommonService} from '../../shared/common/common.service';
import {WorkService} from '../../work/work.service';

@Component({
  selector: 'smart-alarm-log',
  templateUrl: './alarm-log.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;
        }
        :host ::ng-deep .tag-warn .ant-tag{
            margin-right: 0px;
        }
    `]
})
export class AlarmLogComponent implements OnInit {

    @ViewChild('warnList') warnList: WarnListComponent;
    @ViewChild('smartAlarmTarget') smartAlarmTarget:AlarmTargetComponent;
    logList = [];
    warnCountList = [];
    groupList = [];

    pageNum = 1;
    pageCount = pageSize;
    totalNum;

    //发送方式
    sendList;

    //条件
    timeType = '1';
    obj = {
        time_from:<any>'',
        time_till:<any>'',
        alertGroupId:null,
        mediatypeids:null,
        hostids:null,
    };

    startTime;
    endTime;

    //modal
    isVisible = false;
    title;
    content;
    loading = false;
    isDownload;
    constructor(public alarmSer: AlarmService,public overAllSer:OverAllService,public message:NzMessageService,
                public datePipe:DatePipe,public modalSer:NzModalService,private commonSer:CommonService,
                private workSer:WorkService) {
        const today = new Date();
        this.obj.time_from = this.datePipe.transform(today,"yyyy-MM-dd") +" 00:00:00";
        this.obj.time_till = this.datePipe.transform(today,"yyyy-MM-dd") +" 23:59:59";
    }

    ngOnInit() {
        this.getType();
        this.getGroupAlarm();
        this.getList();
    }

    //获取list
    getList(){
        this.loading = true;
        const data = {
            "eventPage":this.pageNum,
            "pageRecords":this.pageCount,
            "alertGroupId":this.obj.alertGroupId,
            "time_from":new Date( this.obj.time_from ).getTime(),
            "time_till":new Date( this.obj.time_till).getTime(),
            "mediatypeids":[this.obj.mediatypeids],
            "hostids":[this.obj.hostids]
        };
        if(!this.obj.mediatypeids){
            data.mediatypeids = []
        }
        if(!this.obj.hostids){
            data.hostids = []
        }
        this.alarmSer.alertFind(data).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.logList = res.data.data;
                    this.totalNum = res.data.totalNum;
                }else{
                    this.message.info(res.errMsg);
                }
                this.loading = false;
            }
        )
    }

    //获取告警组
    getGroupAlarm() {
        this.alarmSer.alertGroupFind().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.groupList = res.data;
                }
            }
        );
    }

    //获取告警组
    getType(){
        this.alarmSer.mediaTypeFind({}).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.sendList = res.data;
                }
            }
        )
    }

    //获取告警目标弹窗
    getTarget(){

    }

    //时间改变
    changeType(){
        const nowDate = new Date().getTime();
        let day1,day2;
        switch (this.timeType){
            case'1':{
                this.obj.time_from = this.datePipe.transform(nowDate,'yyyy-MM-dd') +' 00:00:00';
                this.obj.time_till = this.datePipe.transform(nowDate,'yyyy-MM-dd') +' 23:59:59';
                break;
            }
            case'2':{
                day1 = nowDate - 1*24*60*60*1000;
                this.obj.time_from = this.datePipe.transform(day1,'yyyy-MM-dd') + ' 00:00:00';
                this.obj.time_till = this.datePipe.transform(day1,'yyyy-MM-dd') + ' 23:59:59';
                break;
            }
            case'3':{
                this.obj.time_from = nowDate - 3*24*60*60*1000;
                this.obj.time_till = nowDate;
                break;
            }
            case'4':{
                this.obj.time_from = nowDate - 7*24*60*60*1000;
                this.obj.time_till = nowDate;
                break;
            }
            case'5':{
                break;
            }
        }
    }

    //查询
    search(){
        if(this.timeType == "5"){
            this.obj.time_from = new Date(this.startTime).getTime();
            this.obj.time_till = new Date(this.endTime).getTime();
        }
        this.pageNum = 1;
        this.getList();
    }

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

    //查看消息内容
    showDeleteModal(data){
        this.isVisible = true;
        this.title = data.subject;
        this.content = data.message;
    }

    //modal取消
    handleCancel(){
        this.isVisible = false;
    }

    //删除告警日志
    deleteLog(data){
        this.modalSer.confirm({
            nzTitle: '删除',
            nzContent: '<b style="color: red;">确认删除该告警组吗？</b>',
            nzOkText: '确定',
            nzOkType: 'danger',
            nzOnOk: () => {
                const arr = {
                    ids:[]
                };
                arr.ids.push(data.alertid);
                this.alarmSer.alertDelete(arr).subscribe(
                    (res) => {
                        if (res.errCode == 10000) {
                            this.message.info('删除成功');
                        } else {
                            this.message.info(res.errMsg);
                        }
                    }
                );
            },
            nzCancelText: '取消',
            nzOnCancel: () => console.log('Cancel'),
        })
    }

    //选择告警目标--主机
    showTargetModal(){
        this.smartAlarmTarget.showModal();
    }

    //导出文件
    downLoadExcel(){
        this.isDownload = true;
        const data = {
            "alertGroupId":this.obj.alertGroupId,
            "time_from":new Date( this.obj.time_from ).getTime(),
            "time_till":new Date( this.obj.time_till).getTime(),
            "mediatypeids":[],
            "hostids":[]
        };
        if(this.obj.mediatypeids){
            data.mediatypeids.push(this.obj.mediatypeids);
        }
        if(this.obj.hostids){
            data.hostids.push(this.obj.hostids);
        }
        this.alarmSer.getExportUrlAlert(data).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.workSer.downloadTemplate('alert',res.data).subscribe(
                        (data)=>{
                            this.isDownload = false;
                            this.commonSer.downloadFile('告警推送日志.xlsx',data);
                        }
                    )
                }else{
                    this.message.error(res.errMsg);
                }
                this.isDownload = false;
            }
        )
    }

}
