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';
import {CommonService} from '../../../shared/common/common.service';
import {Location} from '@angular/common';
import {AssetsComponent} from '../../modal/assets/assets.component';

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

    invertoryId;
    inventoryExtend;

    totalNum;
    pageNum=1;
    pageCount = pageSize;
    loading = false;
    thingList;
  constructor(private routerInfo:ActivatedRoute,private workSer:WorkService,private commonSer:CommonService,
              private message:NzMessageService,private router:Router,private localSer:Location) {
    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
            }
        });
    }

    //删除
    deleteModal(){
        const data = {
            inventoryIds: [this.invertoryId]
        };
        this.commonSer.confirmThing("删除","确定删除该资产？",()=>{
            this.workSer.deleteInventory(data).subscribe(
                (res)=>{
                    if(res.errCode == 10000){
                        this.localSer.back();
                        this.message.success("删除资产成功");
                    }else{
                        this.message.error(res.errMsg);
                    }
                }
            )
        })
    }

    //编辑
    showEditModal(){
        this.smartAssets.showEditModal("编辑资产",this.invertoryId);
    }

    // 打开添加关联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);
              }
          }
      )
    }

    //查看资源
    goToHost(hostid){
        this.router.navigate(['app/main/basic-detail'], {
            queryParams: {
                hostId: hostid,
                type: 0,
            }
        });
    }
}
