import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { IPermission } from '../shared/core/IPermission'; import { IRole } from '../shared/core/IRole'; import { SelectItem, MessageService } from 'primeng/api'; import { RoleService } from '../shared/service/role.service'; import { PermissionService } from '../shared/service/permission.service'; import { StaffService } from '../shared/service/staff.service'; import { IStaff } from '../shared/core/IStaff'; @Component({ selector: 'app-staff-permission', templateUrl: './staff-permission.component.html', styleUrls: ['./staff-permission.component.css'], providers:[PermissionService,MessageService] }) export class StaffPermissionComponent implements OnInit { @Input() role:IRole; @Input() organisationId:number; @Input() permission:IPermission; @Output() bindClosePopup: EventEmitter = new EventEmitter(); @Input() permissionType:string = ""; @Input() staffId:number; showAdvancedMode:boolean; saveOptions: SelectItem[]= []; roleList: IRole[]=[]; staffList: IStaff[]=[]; selectedRoles: IRole[]=[]; selectedStaffs:IStaff[]=[]; showRoleList:boolean; showStaffList:boolean; selectedSaveOption:string; settingOptions: SelectItem[]; constructor(private roleService:RoleService, private permissionService:PermissionService,private messageService: MessageService,private staffService:StaffService) { } ngOnInit() { if(this.permissionType == "STAFF"){ this.saveOptions = [ {label:'Apply settings for this staff only', value:1}, {label:'Apply settings for all satff', value:2}, {label:'Also apply settings for selected staff only', value:3} ]; } else{ this.permissionType = "ROLE"; this.saveOptions = [ {label:'Apply settings for this role only', value:1}, {label:'Apply settings for all roles', value:2}, {label:'Also apply settings for selected roles only', value:3} ]; } this.selectedSaveOption = "1"; this.settingOptions = [ {label:'Everything', value:1}, {label:'Own organisation only', value:2}, {label:'None', value:0} ]; } // import the selected role permissions importRole(){ this.messageService.add({key: 'importSuccess',severity:'success',summary:'Success Message', detail: 'Settings imported successfully'}); } // select the saving mode from the dropdown selectSaveOption(value:string){ this.selectedSaveOption = value; if(value == '3'){ if(this.permissionType == "STAFF"){ // get staff list this.getStaffList(); } else{ // get role list this.getRoleList(); } } else{ this.showRoleList = false; this.showStaffList = false; this.selectedRoles = []; this.selectedStaffs = []; } } // get organisation roles getRoleList(){ this.roleService.getRoleList(this.organisationId).subscribe(roles=>{ this.roleList = roles; this.showRoleList = true; }) } // get staff getStaffList(){ this.staffService.getStaffList('0',null).subscribe(staffs=>{ this.staffList = staffs; this.showStaffList = true; }) } // save role permission settings savePermission(){ //console.log(this.selectedRoles); if(this.selectedSaveOption=='3'){ var selectedIdList:string[] = []; if(this.permissionType=='STAFF'){ this.selectedStaffs.forEach(element => { selectedIdList.push(element.Id.toString()); }); } else{ this.selectedRoles.forEach(element => { selectedIdList.push(element.Id.toString()); }); } this.permission.ParentId = selectedIdList.join('~'); } else{ if(this.permissionType=='STAFF'){ this.permission.ParentId = this.staffId.toString(); } else{ this.permission.ParentId = this.role.Id.toString(); } } this.permission.ParentType =this.permissionType; console.log(this.permission); // save permission in backend this.permissionService.savePermissions(this.permission, this.selectedSaveOption, this.organisationId).subscribe(data=>{ //close the popup if(this.permissionType !='STAFF'){ this.bindClosePopup.emit(true); } }) } // cancel role permission popup cancelSavePermission(){ this.bindClosePopup.emit(true); } // View advanced mode viewAdvancedMode(){ this.permission.IsAdvanceMode = true; this.permission.IsSimpleMode = false; this.showAdvancedMode= true; // reset simple mode settings this.permission.LimitToOrganisation=false; this.permission.AllowEditOrganisation=false; this.permission.AllowEditStaff=false; this.permission.AllowRemoveItems=false; this.permission.AllowEditPermissions=false; } // view simple mode viewSimpleMode(){ this.permission.IsSimpleMode = true; } }