import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {FormBuilder} from '@angular/forms';
import {Router} from '@angular/router';
import {JhiMainComponent} from '../../layouts';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {OverAllService} from '../../overAll/overAll.service';

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

    @Output() done = new EventEmitter<any>();
    isVisible = false;
    title;
    tabNum = 0;
    nzSize = 'large';

    validateForm;
    tempId;   //模版Id
    templateList = [];  //模版组
    templatesResoure;  //old模版

    tempList;  //模版

    //宏
    macroTYpe = 'macro';      //宏tabs
    mainHost = 'hostType1';      //主机tabs
    macroList1 = [];
    macroList2 = [];      //配置宏数组

    constructor(private fb: FormBuilder, private overAllSer: OverAllService, private router: Router,
                private modalService: NzModalService, private message: NzMessageService, private main: JhiMainComponent) {
    }

    ngOnInit() {
        this.getTemplateGroup();
        this.getTemplate();
        this.initForm();
    }

    initForm() {
        this.tabNum = 0;
        this.templateList.forEach(e => {
            e.checked = false;
        });
        this.macroList1 = [];
        this.macroList2 = [];
        this.validateForm = {
            host: null,
            name: null,
            groups: [],
            templates: [],
            macros: [],
            hosts: [],
        };
    }

    //获取模版组
    getTemplateGroup() {
        this.templateList = [];
        this.overAllSer.getEditGroups(1).subscribe(
            (res) => {
                const list = res.data;
                list.forEach(e => {
                    const gObj = {
                        label: e.name, value: e.groupid, checked: false
                    };
                    this.templateList.push(gObj);
                });
            }
        );
    }

    //获取模版
    getTemplate() {
        this.tempList = [];
        this.overAllSer.templates().subscribe(
            (res) => {
                this.tempList = res.data;
            }
        );
    }

    tabsChange(e) {
        this.tabNum = e;
    }

    showAddModal(title): void {
        this.isVisible = true;
        this.title = title;
    }

    showEditModal(title, id) {
        this.isVisible = true;
        this.title = title;
        this.tempId = id;
        const data = {
            templateids: [this.tempId]
        };
        this.overAllSer.findTemplates(data).subscribe(
            (res) => {
                const data = res.data[0];
                this.validateForm.host = data.host;
                this.validateForm.name = data.name;
                if (data.groups) {
                    //所在组回显
                    this.templateList.forEach(e1 => {
                        data.groups.forEach(e2 => {
                            if (e1.value == e2.groupid) {
                                e1.checked = true;
                            }
                        });
                    });
                }

                if (data.parentTemplates) {
                    //模版回显
                    let tempArr = [];
                    tempArr = data.parentTemplates.map(e => {
                        return e.templateid;
                    });
                    this.validateForm.templates = tempArr;
                    this.templatesResoure = tempArr;
                }
                if(data.macros){
                    //宏回显
                    let macroArr = [];
                    macroArr = data.macros.map(e=>{
                        const d = {
                            macro:e.macro,
                            value:e.value,
                        };
                        return d;
                    })
                    this.macroList1 = macroArr;
                }
            }
        );
    }

    check() {
        const han = /.*[\u4e00-\u9fa5]+.*$/;
        if (han.test(this.validateForm.host)) {
            this.message.error('模版真实名称不能输入中文');
            return false;
        };
        return true;
    }

    handleOk() {
        if (!this.check()) {
            return false;
        }
        const arr = [];
        this.templateList.forEach(e => {
            if (e.checked) {
                const d = {
                    groupid: e.value
                };
                arr.push(d);
            }
        });
        if (this.macroTYpe == 'macroExpand') {
            this.validateForm.macros = this.macroList2;
        }
        if (this.macroTYpe == 'macro') {
            this.validateForm.macros = this.macroList1;
        }
        if (this.title == '添加模版') {
            this.create(arr);
        }
        if (this.title == '编辑模版') {
            this.update(arr);
        }
    }

    create(arr) {
        const data = {
            host: this.validateForm.host,
            name: this.validateForm.name,
            groups: arr,
            templates: this.validateForm.templates.map(e => {
                const d = {
                    templateid: e
                };
                return d;
            }),
            macros: this.validateForm.macros,
            hosts: [],
        };
        this.overAllSer.createTemplates(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('添加模版成功');
                    this.initForm();
                    this.done.emit();
                    this.isVisible = false;

                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    update(arr) {
        //比较监控模版是否发生变化
        const clearArr = [];
        this.templatesResoure.forEach(res => {
            if (this.validateForm.templates.indexOf(res) == -1) {
                const d = {
                    templateid:res
                }
                clearArr.push(d);
            }
        });
        const data = {
            templateid: this.tempId,   //模版id
            host: this.validateForm.host,
            name: this.validateForm.name,
            groups: arr,
            templates: this.validateForm.templates.map(e => {
                const d = {
                    templateid: e
                };
                return d;
            }),
            templates_clear:clearArr,
            macros: this.validateForm.macros,
            hosts: [],
        };
        this.overAllSer.updateTemplates(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('修改模版成功');
                    this.initForm();
                    this.done.emit();
                    this.isVisible = false;
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    handleCancel(): void {
        this.isVisible = false;
        this.initForm();
    }

    //新增宏
    addMacro1() {
        const data = {
            macro: '{$SNMP_COMMUNITY}',
            value: 'public',
        };
        this.macroList1.push(data);
    }

    //移除宏
    deleteMacro1(index) {
        this.macroList1.splice(index, 1);
    }

    //新增主机宏
    addMacro2() {
        const data = {
            macro: '{$SNMP_COMMUNITY}',
            value: 'public',
        };
        this.macroList2.push(data);
    }

    //移除主机宏
    deleteMacro2(index) {
        this.macroList2.splice(index, 1);
    }

}
