import {Component, OnInit, ViewChild} from '@angular/core';
import {OverAllService} from '../overAll.service';
import {color, pageSize} from '../../app.constants';
import {WebsiteComponent} from '../../modal/website/website.component';
import {Router} from '@angular/router';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {PauseComponent} from '../../modal/pause/pause.component';

@Component({
    selector: 'smart-network-check',
    templateUrl: './network-check.component.html',
    styles: [
        `
            :host ::ng-deep .table-dropdown a{
                font-weight: 300;
            }`
    ]
})
export class NetworkCheckComponent implements OnInit {
    @ViewChild('smartWebsite') smartWebsite:WebsiteComponent;
    @ViewChild('smartPause') smartPause:PauseComponent;

    color = color;
    dataSet;
    deviceNo;
    loading = false;
    allChecked;

    name;
    pageIndex = 1;
    pageSize = pageSize;
    totalNum;

    checkStatus = {
        type:{
            seriousNum:null,
            notNum:null,
            warnNum:null,
            normalNum:null
        },
        total:null
    };

    batchSelectList = [];

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

    ngOnInit() {
        this.getwebList();
        this.getCheckStatus();
    }

    //网站列表
    getwebList() {
        this.loading = true;
        const data = {
            name:this.name,
            eventPage:this.pageIndex,
            pageRecords:this.pageSize
        };
        this.overAllSer.findWebscenario(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.dataSet = res.data.data;
                    this.totalNum = res.data.totalNum;
                }
                this.loading = false;
            }
        );
    }

    //详情
    goDetail(item){
        this.router.navigate(['app/main/network-detail'],{
            queryParams:{
                httptestid:item.httptestid,
                name:item.name
            }
        })
    }

    //监测状态
    getCheckStatus() {
        this.overAllSer.countStatus().subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.checkStatus = res.data;
                }else{
                    this.message.info(res.errMsg);
                }
            }
        )
    }

    //选中
    selectChecked(event,item){
        if(event){
                this.batchSelectList.push(item.httptestid);
        }else{
                const index = this.batchSelectList.indexOf(item.hostid);
                this.batchSelectList.splice(index,1);
        }
    }

    //单个开启关闭
    operationHost(item,status,operation){
        this.batchSelectList.push(item.httptestid);
        this.openBatchHost(status,operation);
    }

    //添加网站
    showAddmodal(){
        this.smartWebsite.showAddModal();
    }

    //编辑网站
    editBasicModal(item){
        this.smartWebsite.showEditModal(item.httptestid)
    }

    //批量开启or关闭
    openBatchHost(status,operation){
        if(this.batchSelectList.length == 0){
            this.message.info('请选择需要'+ operation +'的网站监测');
            return false;
        }
        this.modalService.confirm({
            nzTitle: '监控',
            nzContent: '确定'+operation+'网站监测?',
            nzOkText: '确定',
            nzCancelText: '取消',
            nzOnOk: () => {
                const data = {
                    status:status,
                    ids:this.batchSelectList
                }
                this.overAllSer.statusBatch(data).subscribe(
                    (res) => {
                        if (res.errCode == 10000) {
                            this.message.info('修改成功');
                            this.batchSelectList = [];
                            this.getwebList();
                        } else {
                            this.message.info(res.errMsg);
                        }
                    }
                );
            },
            nzOnCancel: () => console.log('Cancel')
        });
    }

    //批量删除
    batchDeleteConfirm(){
        if(this.batchSelectList.length == 0){
            this.message.info('请选择需要删除的网站监测');
            return false;
        }
        this.modalService.confirm({
            nzTitle: '删除',
            nzContent: '<b style="color: red;">确认要删除所选网站监测吗？</b>',
            nzOkText: '确定',
            nzOkType: 'danger',
            nzOnOk: () => {
                const data = {
                    ids:this.batchSelectList
                }
                this.overAllSer.deleteBatch(data).subscribe(
                    (res) => {
                        if (res.errCode == 10000) {
                            this.message.info('删除成功');
                            this.batchSelectList = [];
                            this.getwebList();
                        } else {
                            this.message.info(res.errMsg);
                        }
                    }
                );
            },
            nzCancelText: '取消',
            nzOnCancel: () => console.log('Cancel')
        });
    }

    //单个删除
    showDeleteConfirm(item){
        this.modalService.confirm({
            nzTitle: '删除',
            nzContent: '<b style="color: red;">确认要删除该网站监测吗？</b>',
            nzOkText: '确定',
            nzOkType: 'danger',
            nzOnOk: () => {
                let arr = [];
                arr.push(item.httptestid);
                const data = {
                    ids:arr
                }
                this.overAllSer.deleteBatch(data).subscribe(
                    (res) => {
                        if (res.errCode == 10000) {
                            this.message.info('删除成功');
                            this.batchSelectList = [];
                            this.getwebList();
                        } else {
                            this.message.info(res.errMsg);
                        }
                    }
                );
            },
            nzCancelText: '取消',
            nzOnCancel: () => console.log('Cancel')
        });
    }

    //临时暂停
    showTempStop(item){
        this.smartPause.showModal(item.httptestid,'网站监测');
    }
}
