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 {OverAllService} from '../../overAll.service';
import {NzMessageService} from 'ng-zorro-antd';
import {pageSize} from '../../../app.constants';
import {CheckPrototypeComponent} from '../modal/check-prototype/check-prototype.component';

@Component({
    selector: 'smart-check-list',
    templateUrl: './check-list.component.html',
    styles: []
})
export class CheckListComponent implements OnInit {
    @ViewChild('smartCheckProtoType') smartCheckProtoType: CheckPrototypeComponent;

    tempName;
    disName;
    hostId;
    discoveryids;

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

    checkList;

    selectList = [];
    allChecked;
    indeterminate;

    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.discoveryids = res.discoveryids;
                this.tempName = res.name;
                this.hostId = res.hostId;
            }
        );
    }

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

    currentPageDataChange($event: Array<{ checked: boolean }>): void {
        this.checkList = $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.checkList.every(value => value.checked === true);
        const allUnChecked = this.checkList.every(value => !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
    }

    ngOnInit() {
        this.getList();
    }

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

    getList() {
        this.loading = true;
        const data = {
            pageNum: this.pageNum,
            pageCount: this.pageCount,
            discoveryids: [this.discoveryids]
        };
        this.overAllSer.findItemPrototype(data).subscribe(
            (res) => {
                this.checkList = res.data.data;
                this.totalNum = res.data.totalNum;
                this.loading = false;
            }
        );
    }

    //新增监控项原型
    showAddModal() {
        this.smartCheckProtoType.showAddModal(this.hostId, this.discoveryids, '添加监控项原型');
    }

    //编辑监控项原型
    showEditModal(data) {
        this.smartCheckProtoType.showEditModal(this.hostId, this.discoveryids, data.itemid, '编辑监控项原型');
    }

    //单个删除
    deleteCheck(item) {
        this.commonSer.confirmThing('删除', '确定删除当前的监控项原型', () => {
            const data = {
                ids: []
            };
            data.ids.push(item.itemid);
            this.overAllSer.deleteItemPrototype(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                    }
                }
            );
        });
    }

    //批量删除
    deleteBatchCheck() {
        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.deleteItemPrototype(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                    }
                }
            );
        });
    }
}
