import {Component, OnInit, ViewChild} from '@angular/core';
import {OverAllService} from '../../../overAll.service';
import {ActivatedRoute, Router} from '@angular/router';
import {color, pageSize} from '../../../../app.constants';
import {CheckPrototypeComponent} from '../../modal/check-prototype/check-prototype.component';
import {SystemService} from '../../../../system/system.service';
import {CommonService} from '../../../../shared/common/common.service';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {TriModalComponent} from '../tri-modal/tri-modal.component';
import {BasiCheckComponent} from '../../../../modal/basi-check/basi-check.component';

@Component({
  selector: 'smart-item-list',
  templateUrl: './item-list.component.html',
  styles: [
      `
          .checkTags {
              position: absolute;
              top: 12px;
              left: 233px;
              z-index: 989;
          }
          .time-select{
              position: absolute;
              top: 55px;
              right: 15px;
              z-index: 999;
          }
          .select-border{
              border: 1px solid #6097b7;
              border-radius: 5px;
              padding: 2px;
              margin-right: 5px;
          }
          :host ::ng-deep .tabs-smart .ant-tabs-nav-scroll div.ant-tabs-tab:nth-child(3){
              margin-right: 180px !important;
          }
      `
  ]
})
export class ItemListComponent implements OnInit {
    @ViewChild('smartBasiCheck') smartBasiCheck: BasiCheckComponent;

    tempName;
    disName;
    hostId;
    discoveryids;
    loading = false;
    color = color;

    //
    itemTypeValue;
    checkList: any[];
    itemTypeList: any[];
    isCheck = false;
    searchName;
    changeStates;
    checkStatus = {
        warning: null,
        normal: null,
        disabled: null,
        high: null
    };

    //分页--监控点
    pageIndex = 1;  //当前页数
    totalNum;   //总数据长度
    pageSize = pageSize;
    applicationId;    //分类ID

    constructor(private overAllSer: OverAllService, private routerInfo: ActivatedRoute, private router: Router,
                private message: NzMessageService, private systemSer: SystemService,private modalService:NzModalService,
                private commonSer: CommonService) {
        this.routerInfo.queryParams.subscribe(
            (res) => {
                this.discoveryids = res.discoveryids;
                this.tempName = res.name;
                this.hostId = res.templateid;
            }
        );
    }

    ngOnInit(){
        this.getList();
    }

    //添加or编辑监测点 之后
    getList(){
        this.getCheckList();
        this.findItemType();
    }

    //分页
    change(e) {
        this.pageIndex = e;
        this.getCheckList();
    }

    //监测点列表
    getCheckList() {
        this.getCheckStatus();
        this.loading = true;
        const data = {
            pageNum: this.pageIndex,
            pageCount: pageSize,
            obj:{
                applicationid: this.applicationId,
                hostid: this.hostId,
                priorityName:this.changeStates,
                name:this.searchName
            }
        };
        this.overAllSer.findCheckByType(data).subscribe(
            (res) => {
                this.checkList = res.data.data;
                this.totalNum = res.data.totalNum;
                this.loading = false;
            }
        );
    }

    changeType(item) {
        this.pageIndex = 1;
        if(item.name == "全部"){
            this.changeStates = null;
        }
        this.applicationId = item.applicationid;
        this.getCheckList();
    }

    //监控项分类
    findItemType() {
        const data = {
            'hostids': []
        };
        data.hostids.push(this.hostId);
        this.overAllSer.findItemType(data).subscribe(
            (res) => {
                this.itemTypeList = res.data;
                const data = {
                    applicationid:'',
                    name:'全部'
                }
                if (null!=res.data){
                    this.itemTypeList.push(data);
                }

            }
        );
    }

    //监测点状态
    getCheckStatus() {
        this.overAllSer.findItemCount(this.hostId).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.checkStatus = res.data;
                }
            }
        );
    }

    //添加监测点
    showAddModal(){
        // this.smartBasiCheck.showAddModal(this.hostId,this.tempName,"添加模版监测点");
    }

    //编辑监测点
    showEditModal(item){
    }

    //删除监测点
    deleteCheckItem(item) {
        this.modalService.confirm({
            nzTitle: '删除',
            nzContent: '<b style="color: red;">确认删除该监测点吗？</b>',
            nzOkText: '确定',
            nzOkType: 'danger',
            nzOnOk: () => {
                const data = {
                    itemids:[item.id],
                };
                this.overAllSer.deleteItem(data).subscribe(
                    (res) => {
                        if (res.errCode == 10000) {
                            this.message.success('删除成功');
                            this.getCheckList();
                        }
                    }
                );
            },
            nzCancelText: '取消',
            nzOnCancel: () => console.log('Cancel')
        });
    }

    //停止/启用监控项  0  启用   1停止
    updateItem(itemid, status) {
        const data = {
            'status': status,
            'itemid': itemid
        };
        this.modalService.confirm({
            nzTitle: '',
            nzContent: '<b style="color: red;">确认修改监控项状态吗？</b>',
            nzOkText: '确定',
            nzOkType: 'danger',
            nzOnOk: () => {
                this.overAllSer.itemUpdate(data).subscribe(
                    (res) => {
                        if (res.errCode == 10000) {
                            this.getCheckList();
                        }
                        this.message.info(res.errMsg);
                    }
                );
            },
            nzCancelText: '取消',
            nzOnCancel: () => console.log('Cancel')
        });
    }

}
