import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {OverAllService} from '../../overAll/overAll.service';
import {
    FormBuilder,
    FormGroup,
    Validators
} from '@angular/forms';
import {BasicKeyComponent} from '../basic-key/basic-key.component';
import {NzMessageService} from 'ng-zorro-antd';
import {NewTypeComponent} from '../new-type/new-type.component';

@Component({
    selector: 'smart-basi-check',
    templateUrl: './basi-check.component.html',
    styles: [
            `.tag-button[disabled],
        .tag-button {
            width: 80%;
            height: 30px;
            color: rgba(0, 0, 0, 0.65);
            background-color: #fff;
            border-color: #d9d9d9;
        }`
    ]
})
export class BasiCheckComponent implements OnInit {
    @ViewChild('basicKey') basicKey: BasicKeyComponent;
    @ViewChild('newType') newType: NewTypeComponent;
    @Output() done = new EventEmitter<any>();

    title;
    isCheck = false;
    hostId;   //主机ID
    itemId;  //监控项id
    interfaceList: any[];
    validateForm;
    checkList;  //监测点分类
    interval;    //间隔时长
    conditionType = 'commonly';
    faultConditionList = [
        {
            code: '1',
            equal: '=',
            value: '',
        }
    ];   //危险阀值list
    conditionList = [
        {
            code: '1',
            equal: '=',
            value: '',
        }
    ];    //故障list
    constructor(private overAllSer: OverAllService, private fb: FormBuilder,
                private message: NzMessageService) {
    }

    ngOnInit() {
        this.initForm();
    }

    initForm(){
        this.validateForm = {
            hostid: this.hostId,
            name: null,
            interfaceid: null,
            key_: null,
            value_type: null,
            type: null,
            data_type: null,
            units: null,
            history: '100',
            trends: null,
            valuemapid: null,
            delay: null,
            snmp_oid:null,
            snmp_community:null,
            condition_commonly: null,    //故障
            condition_high: null,    //故障
            faultCondition_commonly: null,   //危险
            faultCondition_high: null,   //危险
            delta: null,
            formula: null,
            applicationids: null
        };
    }

    //新增
    showAddModal(hostId){
        this.title = '添加监测点';
        this.isCheck = true;
        this.hostId = hostId;
        //主机接口
        this.overAllSer.findDetailed(this.hostId).subscribe(
            (res) => {
                this.interfaceList = res.data[0].interfaces;
            }
        );
        this.getItemType();
    }

    //编辑
    showEditModal(hostId,id){
        this.title = "编辑监测点";
        this.hostId = hostId;
        this.isCheck = true;
        this.itemId = id;

        this.overAllSer.findItemDetail(id).subscribe(
            (res) => {
                this.validateForm = res.data[0];
                this.validateForm.type = this.validateForm.type +'';
                this.validateForm.interfaceid = this.validateForm.interfaceid +"";
                this.validateForm.value_type = this.validateForm.value_type +"";
                this.validateForm.data_type = this.validateForm.data_type +"";
                this.validateForm.applicationids = this.validateForm.applications[0].applicationid +"";
            }
        );

        //主机接口
        this.overAllSer.findDetailed(this.hostId).subscribe(
            (res) => {
                this.interfaceList = res.data[0].interfaces;
            }
        );
        this.getItemType();
    }

    //获取监测点分类
    getItemType() {
        //监测点分类
        const data = {
            'hostids': [this.hostId]
        };
        this.overAllSer.findItemType(data).subscribe(
            (res) => {
                this.checkList = res.data;
            }
        );
    }

    //添加
    handleCheckOk(): void {
        const data = {
            hostid: this.hostId,
            name: this.validateForm.name,
            interfaceid: this.validateForm.interfaceid,
            key_: this.validateForm.key_,
            value_type: Number(this.validateForm.value_type),
            type: Number(this.validateForm.type),
            data_type: Number(this.validateForm.data_type),
            units: this.validateForm.units,
            history: this.validateForm.history,
            trends: this.validateForm.trends,
            valuemapid: this.validateForm.valuemapid,
            delay: this.validateForm.delay,
            snmp_oid:this.validateForm.snmp_oid,
            snmp_community:this.validateForm.snmp_community,
            condition: '',    //故障
            faultCondition: '',   //危险
            delta: Number(this.validateForm.delta),
            formula: this.validateForm.formula,
            applicationids: [this.validateForm.applicationids]
        };
        if (this.conditionType == 'commonly') {   //常规
            this.conditionList.forEach(res => {
                data.condition += '{' + this.validateForm.name + ':' + this.validateForm.key_ + '.last()}' + res.equal + '' + res.value ;
            });
            this.faultConditionList.forEach(res => {
                data.faultCondition += '{' + this.validateForm.name + ':' + this.validateForm.key_ + '.last()}' + res.equal + '' + res.value;
            });
        } else {           //高级
            data.condition = this.validateForm.condition_high;
            data.faultCondition = this.validateForm.faultCondition_high;
        }

        if(this.title == "添加监测点"){
            this.create(data);
        }
        if(this.title == "编辑监测点"){
            this.update(data);
        }
    }

    create(data){
        this.overAllSer.create(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isCheck = false;
                    this.initForm();
                    this.done.emit();
                }
                this.message.info(res.errMsg);
            },
            (err) => {
                this.message.info('系统错误');
            }
        );
    }

    update(data){
        data.itemid = this.itemId;
        this.overAllSer.itemUpdata(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isCheck = false;
                    this.initForm();
                    this.done.emit();
                }
                this.message.info(res.errMsg);
            },
            (err) => {
                this.message.info('系统错误');
            }
        );
    }

    handleCheckCancel(): void {
        this.isCheck = false;
        this.initForm();
    }

    //键值查询
    showKeymodal() {
        this.basicKey.showKeyModal();
    }

    //新增分类
    showTypeModal() {
        this.newType.showModal('新建分类');
    }

    //添加分类
    addItemType(data) {
        const obj = {
            hostid: this.hostId,
            name: data
        };
        this.overAllSer.createItemType(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.info('添加成功');
                    this.getItemType();
                } else {
                    this.message.info(res.errMsg);
                }
            }
        );
    }

    //危险add
    addFault() {
        const demo = {
            and: '&',
            code: '1',
            equal: '=',
            value: '',
        };
        this.faultConditionList.push(demo);
    }

    //故障add
    addcondition() {
        const demo = {
            and: '&',
            code: '1',
            equal: '=',
            value: '',
        };
        this.conditionList.push(demo);
    }

    //危险delete
    deleteFault(index) {
        this.faultConditionList.splice(index, 1);
    }

    //故障delete
    deleteCondition(index) {
        this.conditionList.splice(index, 1);
    }

    getKey(keyValue): void {
        this.validateForm.key_ = keyValue;
    }
}
