import {Component, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {WorkService} from '../../work.service';
import {pageSize} from '../../../app.constants';
import {NzMessageService} from 'ng-zorro-antd';
import {SelectGroupComponent} from '../../../modal/select-group/select-group.component';

@Component({
  selector: 'smart-assets-detail',
  templateUrl: './assets-detail.component.html',
  styles: []
})
export class AssetsDetailComponent implements OnInit {
    @ViewChild('smartSelectGroup') smartSelectGroup:SelectGroupComponent;

    invertoryId;
    inventoryExtend;

    totalNum;
    pageNum=1;
    pageCount = pageSize;
    loading = false;
    thingList;
  constructor(private routerInfo:ActivatedRoute,private workSer:WorkService,
              private message:NzMessageService,private router:Router) {
    this.routerInfo.queryParams.subscribe(
        (res)=>{
          this.invertoryId = res.invertoryId;
        }
    )
  }

  ngOnInit() {
    this.getDetail();
    this.getEventList();
  }

  //资产详情
    getDetail(){
        this.workSer.selectByPrimaryKey(this.invertoryId).subscribe(
            (res)=>{
                    this.inventoryExtend = res.data;
            }
        )
    }

    //资产关联事件
    getEventList(){
      this.loading = true;
      const data = {
          pageCount: this.pageCount,
          pageNum: this.pageNum,
          "inventoryId":this.invertoryId
      };
      this.workSer.find(data).subscribe(
          (res)=>{
              this.thingList = res.data.data;
              this.totalNum = res.data.totalNum;
              this.loading = false;
          }
      )
    }

    //翻页
     change(e){
      this.pageNum = e;
      this.getEventList();
    }

    //查看事件详情
    lookEvent(item) {
        this.router.navigate(['app/main/handleDetail'], {
            queryParams: {
                eventId: item.id
            }
        });
    }

    // 打开添加关联modal
    linkThen(){
        this.smartSelectGroup.showAddModal("选择资源");
    }

    //设置关联
    setLink(e){
      const data = {
          hostid:e[0],
          id:this.invertoryId
      };
      this.workSer.updateHostid(data).subscribe(
          (res)=>{
              if(res.errCode == 10000){
                  this.message.success("设置关联主机成功");
                  this.getDetail();
              }else{
                  this.message.error(res.errMsg);
              }
          }
      )
    }
}
