import {Component, OnInit} from '@angular/core';
import {SystemService} from '../../system.service';
import {pageSize} from '../../../app.constants';
import {NzMessageService} from 'ng-zorro-antd';

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

    pageSize = pageSize;
    //已审批
    pageNoAlready = 1;
    totalNumAlready;
    areadyList = [];

    //未审批
    pageNoNo = 1;
    totalNumNo;
    noList = [];

    allChecked;
    indeterminate;
    displayData = [];

    constructor(private systemSer: SystemService,private message:NzMessageService) {
    }

    ngOnInit() {
        this.getListAlready();
        this.getListNo();
    }

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

    checkAll(value: boolean): void {
        this.noList.forEach(data => {
            if (!data.disabled) {
                data.checked = value;
            }
        });
        this.refreshStatus();
    }

    getListAlready() {
        const data = {
            pageNo: this.pageNoAlready,
            pageSize: this.pageSize
        };
        this.systemSer.approveListSafeManager(data).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.areadyList = res.data;
                    this.areadyList.forEach(e=>{
                        e.checked = false;
                    })
                }else{
                    this.message.error(res.errMsg);
                }

            }
        );
    }

    getListNo() {
        const data = {
            pageNo: this.pageNoNo,
            pageSize: this.pageSize
        };
        this.systemSer.unapproveListSafeManager(data).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.noList = res.data;
                    this.noList.forEach(e=>{
                        e.checked = false;
                    })
                }else{
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    //通过
    passApproval(){

    }

    //退回
    backApproval(){

    }

}
