import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {AlarmService} from '../../alarm/alarm.service';
import {pageSize} from '../../app.constants';
import {NzMessageService} from 'ng-zorro-antd';
import {SendLogComponent} from '../../alarm/modal/send-log/send-log.component';
import {CommonService} from '../../shared/common/common.service';

@Component({
    selector: 'smart-warn-list',
    templateUrl: './warn-list.component.html',
    styles: [
            ` .list-icon {
            font-size: 20px;
            text-align: center;
            color: #8cb6ce;
        }
        `
    ]
})
export class WarnListComponent implements OnInit {
    @ViewChild('sendLog') sendLog: SendLogComponent;
    @Output() total = new EventEmitter<any>();

    warnList = [];
    obj;
    pageNum = 1;
    pageCount = pageSize;
    totalNum;
    loading = false;

    constructor(public alarmSer: AlarmService, public message: NzMessageService,
                private commonSer: CommonService) {
    }

    ngOnInit() {
        // this.getList();
    }

    outGet(obj) {
        this.pageNum = 1;
        this.getList(obj);
    }

    getList(obj) {
        this.obj = obj;
        this.loading = true;
        const data = {
            pageNum: this.pageNum,
            pageCount: this.pageCount,
            obj: obj
        };
        this.alarmSer.eventFind(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data) {
                        res.data.data.forEach(e => {
                            if (e.endTime && e.createTime) {
                                const endTime = e.endTime.replace(/-/g, '/');
                                const createTime = e.createTime.replace(/-/g, '/');
                                const continuedTime = (new Date(endTime).getTime()) - (new Date(createTime).getTime());
                                e.continuedTime = this.commonSer.unitTime(continuedTime);
                            }
                        });
                        this.warnList = res.data.data;
                        this.totalNum = res.data.totalNum;
                        this.total.emit(this.totalNum);
                    }
                }
                this.loading = false;
            }
        );
    }

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

    showLog(data) {
        this.sendLog.showModal(data);
    }
}
