import {Component, OnInit, ViewChild} from '@angular/core';
import {SystemService} from '../system.service';
import {RoleComponent} from '../modal/role/role.component';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {SelectPersonComponent} from '../../modal/select-person/select-person.component';
import {SeparationComponent} from '../modal/separation/separation.component';

@Component({
    selector: 'jhi-power',
    templateUrl: './power.component.html',
    styles: []
})
export class PowerComponent implements OnInit {
    @ViewChild('smartRole') smartRole: RoleComponent;
    @ViewChild('smartSelectPerson') smartSelectPerson:SelectPersonComponent;
    @ViewChild('smartSeparation') smartSeparation:SeparationComponent;

    dataSet: any[];
    isVisible = false;

    roleId;  //角色id
    isAdmin;  //是否管理员

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

    ngOnInit() {
        this.getRole();
        this.systemSer.userFind().subscribe(
            (res)=>{
                this.isAdmin = res.data.isAdmin;
            }
        )
    }

    getRole() {
        this.systemSer.role().subscribe(
            (res) => {
                this.dataSet = res.data;
            }
        );
    }

    //新增角色
    showAddRoleModal() {
        this.smartRole.showAddModal();
    }

    //编辑角色
    showEditModal(data){
        if( this.isAdmin == 0){
            this.message.warning("该角色不可编辑");
            return false;
        }
        this.smartRole.showEditModal(data.id);
    }

    //授予用户
    grantUser(data){
        this.roleId = data.id;
        this.smartSelectPerson.showModal("授予用户",this.roleId);
    }

    //但一角色批量添加用户
    batchAdduserToRole(e){
        const data = {
            roleId:this.roleId,
            addUsers:e.select.map(e=>{
                return e.id;
            }),
            deleteUsers:e.delete.map(e=>{
                return e.id;
            }),
        };
        this.systemSer.batchAuthRoleToUser(data).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.message.success("授予成功")
                }else{
                    this.message.error(res.errMsg);
                }
            }
        )
    }

    //删除角色
    deleteRole(item) {
        if(item.isEdit == 0){
            this.message.warning("该角色不可删除");
            return false;
        }
        this.modalSer.confirm({
            nzTitle:'删除',
            nzContent:'<b style="color: red">确定删除该角色</b>',
            nzOkText:'确定',
            nzOkType:'danger',
            nzOnOk:()=>{
                this.systemSer.deleteRole(item.id).subscribe(
                    (res) => {
                        if(res.errCode == 10000){
                            this.message.success("删除成功");
                            this.getRole();
                        }else{
                            this.message.error(res.errMsg);
                        }
                    }
                );
            },
            nzCancelText:'取消',
            nzOnCancel:()=>{}
        })

    }

    //三员分立
    showSeparationModal(){
        this.smartSeparation.showModal("三员分立");
    }

    getCaptcha(e: MouseEvent): void {
        e.preventDefault();
    }

    lookRole(data){

    }

}
