import {Component, OnInit, ViewChild} from '@angular/core';
import {CreateCompanyComponent} from '../../project-manager/modal/create-company/create-company.component';
import {pageSize} from '../../app.constants';
import {WorkService} from '../../work/work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ProjectService} from '../../project-manager/project.service';
import {Router} from '@angular/router';
import {CommonService} from '../../shared/common/common.service';
import {DatePipe} from '@angular/common';
import {warning_leavel} from '../early.constants';
import {EarlyWarningService} from '../earlyWarning.service';
import {WarngingRuleComponent} from '../modal/warnging-rule/warnging-rule.component';
import {WarningRuleLookComponent} from '../modal/warning-rule-look/warning-rule-look.component';

@Component({
    selector: 'smart-role-warning',
    templateUrl: './role-warning.component.html',
    styles: []
})
export class RoleWarningComponent implements OnInit {
    @ViewChild('smartWarningRule') smartWarningRule: WarngingRuleComponent;
    @ViewChild('smartWarningRuleLook') smartWarningRuleLook: WarningRuleLookComponent;
    warning_leavel = [{label: '全部', value: null}, ...warning_leavel];

    page = {
        list: [],
        isLoading: false,
        pageCount: pageSize,
        pageNum: 1,
        totalNum: null
    };

    obj = {
        level: null,
        keyword: null
    };

    constructor(private workSer: WorkService, private message: NzMessageService, private earlySer: EarlyWarningService,
                private router: Router, private commonSer: CommonService, private datePipe: DatePipe) {
    }

    ngOnInit() {
        this.getList();
    }

    //获取列表
    getList() {
        this.page.isLoading = true;
        const obj = {
            pageNum: this.page.pageNum,
            pageCount: this.page.pageCount
        };
        Object.assign(obj, this.obj);
        this.earlySer.pageWarningRule(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.page.list = res.data.data;
                    this.page.totalNum = res.data.totalNum;
                    this.page.isLoading = false;
                }
            }
        );
    }

    //翻页
    change(e) {
        if (e > 0) {
            this.page.pageNum = e;
            this.getList();
        }
    }

    //搜索
    search() {
        this.page.pageNum = 1;
        this.getList();
    }

    //删除
    handleDelete(item) {
        this.commonSer.confirmThing('删除', '确定删除?', () => {
            const data = {
                ids: [item.id]
            };
            this.earlySer.deleteWarningRule(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.search();
                    }
                }
            );
        });
    }

    //查看
    handleLook(item) {
        this.smartWarningRuleLook.showLookModal(item);
    }

    //新增运维公司
    handleAddModal() {
        this.smartWarningRule.showAddModal('新增预警规则');
    }

    handleEditModa(item) {
        this.smartWarningRule.showEditModal(item, '编辑预警规则');
    }

}
