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 {TriggerPrototypeComponent} from '../../modal/trigger-prototype/trigger-prototype.component';
import {SystemService} from '../../../../system/system.service';
import {CommonService} from '../../../../shared/common/common.service';
import {NzMessageService} from 'ng-zorro-antd';
import {TriModalComponent} from '../tri-modal/tri-modal.component';

@Component({
  selector: 'smart-tri-list',
  templateUrl: './tri-list.component.html',
  styles: []
})
export class TriListComponent implements OnInit {
    @ViewChild('smartTriModal') smartTriModal: TriModalComponent;
    tempName;
    disName;
    templateid;
    queryName;
    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.tempName = res.name;
                this.templateid = res.templateid;
            }
        );
    }

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

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

    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 = {
            queryName:this.queryName,
            templateids: [this.templateid]
        };
        this.overAllSer.findTri(data).subscribe(
            (res) => {
                this.triggerList = res.data;
                this.loading = false;
            }
        );
    }

    showAddModal() {
        this.smartTriModal.showAddModal('新增阈值', this.tempName);
    }

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

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

    //批量删除
    deleteBatchTrigger() {
        let arr = [];
        this.triggerList.forEach(e => {
            if (e.checked) {
                arr.push(e.triggerid);
            }
        });
        if (arr.length == 0) {
            this.message.warning('请选择需要删除的阈值');
            return false;
        }
        this.commonSer.confirmThing('删除', '确定删除选择的阈值', () => {
            const data = {
                ids: arr
            };
            this.overAllSer.deleteTri(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                    }
                }
            );
        });
    }


}
