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

@Component({
    selector: 'jhi-examine',
    templateUrl: './examine.component.html',
    styles: []
})
export class ExamineComponent implements OnInit {

    pageSize = pageSize;
    name;
    tabNum;

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

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

    allChecked;
    indeterminate;
    displayData = [];

    selectList = [];
    menuList = [];

    roleType;   //1.系统管理员 2.安全管理员 3。二者都有

    constructor(private systemSer: SystemService,private message:NzMessageService,private commonSer:CommonService,
                private layoutSer:LayoutService) {
    }

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

    selectItem(item,e){
        if(e){
            this.selectList.push(item.id);
        }else{
            this.selectList.forEach((value,index)=>{
                if(value.id == item.id){
                    this.selectList.splice(index,1)
                }
            });
        }
        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();
    }

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

    /**
     * 获取所有菜单
     */
    getAllMenu(){
        this.layoutSer.getAllMenu().subscribe(
            (res)=>{
                this.menuList = res.data;
            }
        )
    }

    /**
     * 获取当前登录人角色
     * 1.系统管理员 2.安全管理员 3。二者都有
     */
    getCurrentRole(){
        this.systemSer.checkRole().subscribe(
            (res)=>{
                    this.roleType = res.data;
            }
        )
    }

    getListAlready() {
        const data = {
            pageNum: this.pageNoAlready,
            pageSize: this.pageSize,
            name:this.name
        };
        this.systemSer.getApproveOfDone(data).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.areadyList = res.data.data;
                    this.totalNumAlready = res.data.totalCount;
                }else{
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    changeAlreadyPage(e){
        this.pageNoAlready = e;
        this.getListAlready()
    }

    getListNo() {
        const data = {
            pageNum: this.pageNoNo,
            pageSize: this.pageSize,
            name:this.name
        };
        this.systemSer.getUnApprove(data).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.noList = res.data.data;
                    this.totalNumNo = res.data.totalCount;
                }else{
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    changeNoPage(e){
        this.pageNoNo = e;
        this.getListNo();
    }

    //审批通过
    approvalPass(){
        if(this.selectList.length == 0){
            this.message.warning("请选择需要审核数据")
            return false;
        }
        this.commonSer.confirmThing("通过","确定通过所选请求",()=>{
            this.systemSer.approvalPass(this.selectList).subscribe(
                (res)=>{
                    if(res.errCode == 10000){
                        this.message.success("已通过");
                        this.getListNo();
                        this.getListAlready();
                    }else{
                        this.message.error(res.errMsg);
                    }
                }
            )
        })

    }

    //审批不通过
    approvalUnPass(){
        if(this.selectList.length == 0){
            this.message.warning("请选择需要审核数据")
            return false;
        }
        this.commonSer.confirmThing("退回","确定拒绝所选请求",()=>{
            this.systemSer.approvUnPass(this.selectList).subscribe(
                (res)=>{
                    if(res.errCode == 10000){
                        this.message.success("退回成功");
                        this.getListNo();
                        this.getListAlready();
                    }else{
                        this.message.error(res.errMsg);
                    }
                }
            )
        })

    }

    //审批取消
    approvalCancle(){
        if(this.selectList.length == 0){
            this.message.warning("请选择需要审核数据")
            return false;
        }
        this.commonSer.confirmThing("撤回","确定取消所选请求",()=>{
            this.systemSer.approvCancle(this.selectList).subscribe(
                (res)=>{
                    if(res.errCode == 10000){
                        this.message.success("撤回成功");
                        this.getListNo();
                        this.getListAlready();
                    }else{
                        this.message.error(res.errMsg);
                    }
                }
            )
        })
    }

    changeIndex(e){
        this.tabNum = e;
    }

    search(){
        if(this.tabNum == 0){
            this.getListNo();
        }
        if(this.tabNum == 1){
            this.getListAlready();
        }
    }

}
