import {Component, OnInit, ViewChild} from '@angular/core';
import {AlarmService} from '../alarm.service';
import {pageSize} from '../../app.constants';
import {AlarmGroupComponent} from '../modal/alarm-group/alarm-group.component';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {AlarmModalComponent} from '../../modal/alarm-modal/alarm-modal.component';

@Component({
    selector: 'smart-alarm-set',
    templateUrl: './alarm-set.component.html',
    styles: []
})
export class AlarmSetComponent implements OnInit {
    @ViewChild('smartAlarmGroup') smartAlarmGroup: AlarmGroupComponent;
    @ViewChild('smartAlarmModal') smartAlarmModal: AlarmModalComponent;
    setList = [];
    alertGroupId;
    groupList = [];

    pageNum;
    pageCount = pageSize;
    totalNum;

    constructor(private alarmSer: AlarmService, private modalSer: NzModalService,
                private message: NzMessageService, private modalService: NzModalService) {
    }

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

    //获取推送设置列表
    getList() {
        const data = {
            eventPage: this.pageNum,
            pageRecords: this.pageCount,
            alertGroupId: this.alertGroupId
        };
        this.alarmSer.actionFind(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.setList = res.data.data;
                    this.totalNum = res.data.totalNum;
                }
            }
        );
    }

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

    //添加告警组
    showGroupModal() {
        this.smartAlarmGroup.addModal();
    }

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

    //修改告警组
    editGroup() {
        if (!this.alertGroupId) {
            this.message.info('请选择需要编辑的分组');
            return false;
        }
        let arr = [];
        this.groupList.forEach(res => {
            if (res.id == this.alertGroupId) {
                arr = res;
            }
        });
        this.smartAlarmGroup.editModal(arr);
    }

    change(e) {
    }

    //新增告警推送
    showAlarmModal() {
        this.smartAlarmModal.showModal(null);
    }

    //启用或暂停告警组
    changeStatus(title, status, item) {
        this.modalService.confirm({
            nzTitle: title,
            nzContent: '<b style="color: red;">确定要' + title + '当前告警吗</b>',
            nzOkText: '确定',
            nzOkType: 'danger',
            nzOnOk: () => {
                this.alarmSer.actionStatus(item.actionid, status).subscribe(
                    (res) => {
                        if (res.errCode == 10000) {
                            this.message.info(title + '成功');
                            this.getList();
                        } else {
                            this.message.info(res.errMsg);
                        }
                    }
                );
            },
            nzCancelText: '取消',
            nzOnCancel: () => console.log('Cancel')
        });
    }

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