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 = [];
    isLoading = false;

    pageNum;
    pageCount = pageSize;
    totalNum;

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

    ngOnInit() {
        this.getGroupAlarm();
    }

    search(){
        this.pageNum = 1;
        this.getList();
    }

    //获取推送设置列表
    getList() {
        this.isLoading = true;
        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;
                }
                this.isLoading = false;
            }
        );
    }

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

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

    //删除告警组
    deleteGroup() {
        if (!this.alertGroupId) {
            this.message.info('请选择需要删除的告警组');
            return false;
        }
        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.search();
                            this.message.info('删除成功');
                        }
                    }
                );
            },
            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) {
        if(e > 0){
            this.pageNum = e;
            this.getList();
        }
    }

    //新增告警推送
    showAlarmModal() {
        this.smartAlarmModal.showAddModal("添加告警",null);
    }

    //编辑告警推送
    showEditModal(item){
        this.smartAlarmModal.showEditModal("编辑告警推送",item.actionid);
    }

    //启用或暂停告警组 --单个
    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.search();
                        }
                    }
                );
            },
            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.search();
                            }
                        }
                    );
                },
                nzCancelText: '取消',
                nzOnCancel: () => console.log('Cancel')
            }
        );
    }
}
