import {Component, OnInit, ViewChild} from '@angular/core';
import {NzMessageService, NzModalService, NzTreeNode, NzTreeNodeOptions} from 'ng-zorro-antd';
import {SystemService} from '../system.service';
import {UserComponent} from '../modal/user/user.component';
import {GroupModalComponent} from '../modal/group-modal/group-modal.component';
import {SelectRoleComponent} from '../../modal/select-role/select-role.component';
import {CommonService} from '../../shared/common/common.service';

@Component({
    selector: 'jhi-group',
    templateUrl: './group.component.html',
    styles: []
})
export class GroupComponent implements OnInit {
    @ViewChild("smartSelectRole") smartSelectRole:SelectRoleComponent;
    @ViewChild('power') power;
    @ViewChild('smartUser') smartUser: UserComponent;
    @ViewChild('smartGroupModal') smartGroupModal: GroupModalComponent;

    dataSet: any[];
    groupList: any[];
    allChecked = false;
    disabledButton = true;
    indeterminate = false;
    checkedNumber = 0;
    nodes: any[];
    isVisible = true;
    userId;
    displayData: Array<{ key1: string; key2: number; key3: string; key4: string; checked: boolean }> = [];

    constructor(private systemSer: SystemService, private message: NzMessageService,
                private modalSer: NzModalService, private commonSer: CommonService) {
    }

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

    currentPageDataChange($event: Array<{ key1: string; key2: number; key3: string; key4: string; checked: boolean }>): void {
        this.displayData = $event;
    }

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

    mouseAction(name: string, e: any): void {
        console.log(name, e);
    }

    ngOnInit() {
        this.getUser();
        this.getGroup();
    }

    getUser() {
        const data = {
            'name': '',
            'orgId': '',
            'pageNumber': '1',
            'pageSize': '10'
        };
        this.systemSer.user(data).subscribe(
            (res) => {
                this.dataSet = res.data;
            }
        );
    }

    //组织信息
    getGroup() {
        this.systemSer.organization().subscribe(
            (res) => {
                this.groupList = res.data;
                this.groupList.forEach(
                    res => {
                        res.title = res.name;
                        res.key = res.id;
                    }
                );
                this.nodeTree();
            }
        );
    }

    nodeTree() {
        const tree = this.commonSer.listToTree('id', 'parentId', this.groupList,);
        const list = tree.map(res => {
            return new NzTreeNode(res);
        });
        this.nodes = list;
    }

    showModal(): void {
        this.isVisible = true;
    }

    handleOk(): void {
        this.isVisible = false;
    }

    handleCancel(): void {
        this.isVisible = false;
    }

    selectJ() {
        this.power.hello();
    }

    //新增用户
    showUserModal() {
        this.smartUser.showModal(this.groupList);
    }

    //删除用户
    deleteUser(data) {
        this.modalSer.confirm({
            nzTitle: '删除',
            nzContent: '<b style="color: red">确定删除该用户吗</b>',
            nzOkText: '确定',
            nzCancelText: '取消',
            nzOnOk: () => {
                this.systemSer.deleteUser(data.id).subscribe(
                    (res) => {
                        // if(res.errCode == 10000){

                        // }
                    }
                );
            },
            nzOnCancel: () => {
            }
        });
    }

    //新增部门
    showGroupModal() {
        this.smartGroupModal.showModal(this.groupList);
    }

    //授予角色
    grantRole(data){
        this.userId = data.id;
        const res = {
            userId:data.id
        }
        this.systemSer.roleList(res).subscribe(
            (res)=>{

            }
        )
        this.smartSelectRole.showModal("选择角色");
    }

    setRole(event){
        let str ="";
        event.forEach(res=>{
            str += res.id +",";
        });
        str = str.substring(0,str.length-1);
        let formdata = new FormData();
        formdata.append("userId",this.userId);
        formdata.append("roles",str);
        this.systemSer.batchGrantRoleToUser(formdata).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.message.success("授予角色成功");
                }else{
                    this.message.error(res.errMsg);
                }
            }
        )
    }

}
