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';
import {OverAllService} from '../../../overAll/overAll.service';

@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;

    groupList = [];
    equipmentList = [];

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

    ngOnInit() {
        this.initForm();
        this.getGroup();
        this.getType();
    }

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

    //获取资源分组
    getGroup() {
        this.overAllSer.getEditGroups(0).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.groupList = res.data;
                }
            }
        );
    }

    //获取设备类型
    getType() {
        this.overAllSer.findType().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.equipmentList = res.data;
                }
            }
        );
    }

    //新增
    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() {
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if (this.validateForm.invalid) {
            return false;
        }
        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;
    }

}
