import {Component, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {CommonService} from '../../../shared/common/common.service';
import {SystemService} from '../../../system/system.service';
import {pageSize, SERVER_API_URL} from '../../../app.constants';
import {NzMessageService} from 'ng-zorro-antd';
import {OverAllService} from '../../overAll.service';
import {DiscoveryComponent} from '../../../modal/discovery/discovery.component';

@Component({
    selector: 'smart-discovery-list',
    templateUrl: './discovery-list.component.html',
    styles: []
})
export class DiscoveryListComponent implements OnInit {
    @ViewChild('smartDiscovery') smartDiscovery: DiscoveryComponent;

    templateid;
    discoveryList;
    tempName;

    loading = false;
    pageNum = 1;
    pageCount = pageSize;
    totalNum;

    allChecked = false;
    selectList = [];
    disabledButton = true;
    indeterminate = false;

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

    checkAll(value: boolean): void {
        this.discoveryList.forEach(data => data.checked = value);
        this.refreshStatus();
    }

    currentPageDataChange($event: Array<{ checked: boolean }>): void {
        this.discoveryList = $event;
    }

    selectItem(item, e) {
        if (e) {
            this.selectList.push(item);
        } else {
            this.selectList.forEach((value, index) => {
                if (value.id == item.id) {
                    this.selectList.splice(index, 1);
                }
            });
        }
        this.refreshStatus();
    }

    refreshStatus(): void {
        const allChecked = this.discoveryList.every(value => value.checked === true);
        const allUnChecked = this.discoveryList.every(value => !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
    }

    ngOnInit() {
        this.getList();
    }

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

    getList() {
        this.loading = true;
        const data = {
            pageNum: this.pageNum,
            pageCount: this.pageCount,
            hostIds: [this.templateid]
        };
        this.overAllSer.findPageLldrule(data).subscribe(
            (res) => {
                this.discoveryList = res.data.data;
                this.totalNum = res.data.totalNum;
                this.loading = false;
            }
        );
    }

    //添加自动发现
    showAddModal() {
        this.smartDiscovery.showAddModal(this.templateid, '添加自动发现');
    }

    //编辑自动发现
    showEditModal(data) {
        this.smartDiscovery.showEditModal(this.templateid, data.itemid, '编辑自动发现');
    }

    //goto 监测项原型
    goToCheck(data) {
        this.router.navigate(['app/main/checkList'], {
            queryParams: {
                hostId: this.templateid,
                discoveryids: data.itemid,
                name: data.name,
                tempName: this.tempName
            }
        });
    }

    //goto 阈值原型
    goToTrigger(data) {
        this.router.navigate(['app/main/triggerList'], {
            queryParams: {
                id: data.itemid,
                name: data.name,
                tempName: this.tempName
            }
        });
    }

    //单个删除
    deleteDiscovery(item) {
        this.commonSer.confirmThing('删除', '确定删除当前的自动发现', () => {
            const data = {
                ids: []
            };
            data.ids.push(item.itemid);
            this.overAllSer.deleteLldrule(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                    } else {
                        this.message.error(res.errMsg);
                    }
                }
            );
        });
    }

    //批量删除
    deleteBatchDiscovery() {
        if (this.selectList.length == 0) {
            this.message.warning('请选择需要删除的自动发现');
            return false;
        }
        this.commonSer.confirmThing('删除', '确定删除选择的自动发现', () => {
            const data = {
                ids: this.selectList.map(e => {
                    return e.itemid;
                })
            };
            this.overAllSer.deleteLldrule(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                    } else {
                        this.message.error(res.errMsg);
                    }
                }
            );
        });
    }
}
