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-system',
  templateUrl: './system.component.html',
  styles: []
})
export class SystemComponent 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();
    }

    currentPageDataChange($event: Array<{ entityName: string; checked: boolean; disabled: boolean; }>): void {
        this.displayData = $event;
        this.refreshStatus();
    }

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

    checkAll(value: boolean): void {
        this.displayData.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;
                }else{
                    this.message.error(res.errMsg);
                }
            }
        );
    }

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

}
