import {Component, OnInit, ViewChild} from '@angular/core';
import {color, pageSize} from '../../../app.constants';
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 {TriggerPrototypeComponent} from '../modal/trigger-prototype/trigger-prototype.component';

@Component({
    selector: 'smart-trigger-list',
    templateUrl: './trigger-list.component.html',
    styles: []
})
export class TriggerListComponent implements OnInit {

    @ViewChild('smartTriggerPrototype') smartTriggerPrototype: TriggerPrototypeComponent;
    tempName;
    disName;
    hostId;
    discoveryids;
    color = color;

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

    triggerList;

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

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

    currentPageDataChange($event: Array<{ checked: boolean }>): void {
        this.triggerList = $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.triggerList.every(value => value.checked === true);
        const allUnChecked = this.triggerList.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,
            discoveryids: [this.discoveryids]
        };
        this.overAllSer.findPageTrigger(data).subscribe(
            (res) => {
                this.triggerList = res.data.data;
                this.totalNum = res.data.totalNum;
                this.loading = false;
            }
        );
    }

    showAddModal() {
        this.smartTriggerPrototype.showAddModal('新增阈值原型', this.tempName);
    }

    showEditModal(data) {
        this.smartTriggerPrototype.showEditModal('编辑阈值原型', data.triggerid, this.tempName);
    }

    //单个删除
    deleteTrigger(item) {
        this.commonSer.confirmThing('删除', '确定删除当前的阈值原型', () => {
            const data = {
                ids: []
            };
            data.ids.push(item.triggerid);
            this.overAllSer.deleteTrigger(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                    } else {
                        this.message.error(res.errMsg);
                    }
                }
            );
        });
    }

    //批量删除
    deleteBatchTrigger() {
        if (this.selectList.length == 0) {
            this.message.warning('请选择需要删除的阈值原型');
            return false;
        }
        this.commonSer.confirmThing('删除', '确定删除选择的阈值原型', () => {
            const data = {
                ids: this.selectList.map(e => {
                    return e.triggerid;
                })
            };
            this.overAllSer.deleteTrigger(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                    } else {
                        this.message.error(res.errMsg);
                    }
                }
            );
        });
    }

}
