import {Component, OnInit} from '@angular/core';
import {TopologyService} from '../../topology.service';
import {LocalStorageService} from 'ngx-webstorage';
import {NzMessageService} from 'ng-zorro-antd';

declare let editor: any;
@Component({
    selector: 'smart-line',
    templateUrl: './line.component.html',
    styles: []
})
export class LineComponent implements OnInit {

    isLine = false;
    hostIds = [];

    inPageNum = 1;
    outPageNum = 1;
    inPageCount = 10;
    outPageCount = 10;

    totalNum;

    inList;
    inSelect = [];

    outList;
    outSelect = [];

    constructor(private topologySer: TopologyService, private message: NzMessageService, private localStorage: LocalStorageService,) {
    }

    //in
    incurrentPageDataChange($event: Array<{ key1: string; key2: number; key3: string; key4: string; checked: boolean }>): void {
        this.inList = $event;
    }

    inselectItem(item,e){
        if(e){
            this.inSelect = item;
        }else{
            this.inSelect = null;
        }
    }

    indeleteSelect(index){
        this.inSelect = null;
    }

    changePageIn(e){

    }

    //out

    outcurrentPageDataChange($event: Array<{ key1: string; key2: number; key3: string; key4: string; checked: boolean }>): void {
        this.inList = $event;
    }

    outselectItem(item,e){
        if(e){
            this.outSelect = item;
        }else{
            this.outSelect = null;
        }
    }

    outdeleteSelect(index){
        this.outSelect = null;
    }

    changePageOut(e){

    }

    ngOnInit() {

    }

    showModal(hostIds) {
        this.hostIds = hostIds;
        this.isLine = true;
        this.getOutList();
        this.getInList();
    }

    getInList(){
        const data = {
            hostIds:[this.hostIds],
            search:"",
            type:"in",
            pageNum:this.inPageNum,
            pageCount:this.inPageCount
        }
        this.topologySer.findFlowItemByHost(data).subscribe(
            (res)=>{
                this.totalNum = res.data.totalNum;
                this.inList = res.data.data;
            }
        )

    }

    getOutList(){
        const data = {
            hostIds:[this.hostIds],
            search:"",
            type:"out",
            pageNum:this.outPageNum,
            pageCount:this.outPageCount
        }
        this.topologySer.findFlowItemByHost(data).subscribe(
            (res)=>{
                this.totalNum = res.data.totalNum;
                this.outList = res.data.data;
            }
        )

    }

    //进出口流量
    handleLineCancel() {
        this.inList = [];
        this.outList = [];
        this.isLine = false;
        localStorage.setItem("line",'false');
    }

    handleLineOk() {
        this.inList = [];
        this.outList = [];
        this.isLine = false;
        console.log(this.inSelect);
        console.log(this.outSelect);
        editor.utils.setLink(this.inSelect,this.outSelect);
        localStorage.setItem("line",'false');
    }

}
