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

@Component({
    selector: 'smart-alarm-list',
    templateUrl: './alarm-list.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 AlarmListComponent implements OnInit {
    @ViewChild('warnList') warnList: WarnListComponent;
    eventList = [];
    warnCountList = [];
    groupList = [];
    pageNum = 1;
    pageCount = pageSize;
    timeFormat = 'yyyy-MM-dd';

    //条件
    timeType = '1';
    obj = {
        isRecord:"isRecord"
    };

    dateBegin = null;
    dateEnd = null;
    isDownload;

    constructor(public alarmSer: AlarmService, public overAllSer: OverAllService, public message: NzMessageService,
                private monitorSer: MonitorService,
                public datePipe: DatePipe, private commonSer: CommonService, private workSer: WorkService) {
        this.changeType('1');
    }

    ngOnInit() {
        this.getType();
        this.getWarnGroup();
        this.searchValue();
    }

    //获取分组
    getWarnGroup() {
        this.overAllSer.findGroup().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.groupList = res.data.data;
                }
            }
        );
    }

    //获取资源类型
    getType() {
        this.overAllSer.findType().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.warnCountList = res.data;
                }
            }
        );
    }

    //时间改变
    changeType(e) {
        if (e == '99') return false;
        const obj = this.commonSer.getTimeByType(e);
    }

    //查询
    searchValue() {
        if (this.timeType == '99') {
        }
        console.log(this.obj);
        this.warnList.getList(this.obj);
    }

    //导出文件
    downLoadExcel() {
        this.isDownload = true;
        this.alarmSer.getExportUrlEvent(this.obj).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;
            }
        );
    }

    monitorAlarm() {
        const data =
            {
                'module': 'PEMS',
                'data': {
                    'D04': {
                        '告警': '正常1.0',
                        '告警数据': '空1.0'
                    }
                }
            };
        this.monitorSer.data(data).subscribe(
            (res)=>{

            }
        )
    }
}
