import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {EarlyWarningService} from '../../earlyWarning.service';
import {NzMessageService} from 'ng-zorro-antd';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {warning_type} from '../../early.constants';

@Component({
    selector: 'smart-warnging-rule',
    templateUrl: './warnging-rule.component.html',
    styles: []
})
export class WarngingRuleComponent implements OnInit {

    @Output() done = new EventEmitter();
    modal = {
        isVisible: false,
        title: '',
        isOkLoading: false
    };
    warningType = warning_type;
    tabNum = 0;
    msgId;
    msgObj;
    newestRemark;
    validateForm: FormGroup;

    constructor(private earlySer: EarlyWarningService, private message: NzMessageService,
                private fb: FormBuilder,) {
    }

    ngOnInit() {
        this.initForm();
    }

    initForm() {
        this.validateForm = this.fb.group({
            name: [null, [Validators.required]],
            type: ['1'], level: [null],
            cycle: [null], content: [null],
            fileId: [null], resourceGroupType: [null],
            resourceGroupValue: [null], resourceWarningLevel: [null],
            resourceDays: [null], resourceSymbol: [null],
            resourceWarningTims: [null], businessDataType: [null],
            businessDays: [null], businessWarningSymbol: [null],
            businessWarningValue: [null], businessThresholdSymbol: [null],
            businessThresholdValue: [null], jobDataType: [null],
            jobUntreatedSymbol: [null], jobUntreatedDays: [null],
        });
    }

    //新增
    showAddModal(title) {
        this.modal.isVisible = true;
        this.modal.title = title;
    }

    showEditModal(item, title) {
        this.modal.title = title;
    }

    showLookModal(item, title) {
        this.modal.isVisible = true;
        this.msgId = item.id;
        this.modal.title = title;
        this.getDetail();
    }

    //详情
    getDetail() {
        this.earlySer.findWarningMsg(this.msgId).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.msgObj = res.data;
                }
            }
        );
    }

    //确定
    handleOk() {

    }

    //添加批注
    saveNotation() {
        this.modal.isOkLoading = true;
        this.earlySer.addWarningRule(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.done.emit();
                    this.message.success('添加规则成功');
                    this.initForm();
                    this.handleCancel();
                }
            }
        );
    }

    //取消
    handleCancel() {
        this.modal.isOkLoading = false;
        this.modal.isVisible = false;
    }

    //切换tab
    tabsChange(e) {
        this.tabNum = e;
    }

}
