import { Component, OnInit, Input } from '@angular/core'; import { RoleService } from '../shared/service/role.service'; import { IOrganisationStaff } from '../shared/core/IOrganisationStaff'; import { IRole } from '../shared/core/IRole'; import { IOrganisation } from '../shared/core/IOrganisation'; import { IOrganisationType } from '../shared/core/IOrganisationType'; import { IIndustry } from '../shared/core/IIndustry'; import { OrganisationService } from '../shared/service/organisation.service'; import { DialogService, Message } from 'primeng/api'; import { PermissionsComponent } from '../permissions/permissions.component'; import { NewStaffComponent } from '../new-staff/new-staff.component'; @Component({ selector: 'app-roles', templateUrl: './roles.component.html', styleUrls: ['./roles.component.css'], providers:[RoleService,OrganisationService,DialogService] }) export class RolesComponent implements OnInit { @Input() organisationId:number=0; @Input() organisation:IOrganisation; @Input() isNewOrganisation:boolean; @Input() selectedType:IOrganisationType; @Input() selectedIndustry:IIndustry; notAssignedCount:number=0; notAssignedByCompanyCount :number=0; expandNotAssigned:boolean = true; notAssignedStaffs:IOrganisationStaff[]=[]; notAssignedStaffsByCompany:IOrganisationStaff[]=[]; selectedNotAssignedStaff:IOrganisationStaff; showNewRole:boolean=false; newRoleName:string; newRoleDescription:string; titleNotAssignedAny:string; titleNotAssignedCompany:string; roleList:IRole[]=[]; draggedStaff: IOrganisationStaff; editRoleName:string; editRoleDescription:string; msgs: Message[] = []; constructor(private roleService:RoleService, private organisationService:OrganisationService,public dialogService: DialogService) { } ngOnInit() { // get not assigned staff list this.getNotAssignedStaff(); // get not assigned staff list under the company if(this.organisationId>0){ this.getNotAssignedStaffByCompany(); // get role list this.getRoleList(); } this.msgs = []; } // get not assigned staff list from backend getNotAssignedStaff(){ this.roleService.getNotAssignedStaff(0,"All",null).subscribe(staff=>{ if(staff!=null && staff != undefined){ //bind not assigned staff list this.notAssignedStaffs = staff; // bind not assigned staff list count this.notAssignedCount = staff.length; // bind title if(this.organisationId > 0){ this.titleNotAssignedAny = "Not assigned to any organisations (" + this.notAssignedCount + ")"; } else{ this.titleNotAssignedAny = "Not assigned (" + this.notAssignedCount + ")"; } } }) } // get not assigned staff list under the company from backend getNotAssignedStaffByCompany(){ this.roleService.getNotAssignedStaff(this.organisationId,"ByCompany",null).subscribe(staff=>{ if(staff!=null && staff != undefined){ //bind not assigned staff list this.notAssignedStaffsByCompany = staff; this.notAssignedByCompanyCount = staff.length; // bind title this.titleNotAssignedCompany = "Not assigned under "+this.organisation.CompanyName+" (" + staff.length + ")"; } }) } // save new role saveNewRole(){ this.msgs = []; if(this.newRoleName!=''){ console.log(this.newRoleName,this.newRoleDescription); // check the availability of the organisation if(this.organisationId>0){ this.roleService.saveNewRole(this.organisationId,this.newRoleName,this.newRoleDescription).subscribe(roles=>{ this.roleList = roles; this.newRoleName = ''; this.newRoleDescription = ''; this.showNewRole = false; }) } else{ if(this.selectedType !=null && this.selectedType!= undefined && this.selectedIndustry !=null && this.selectedIndustry!= undefined){ // save the company details this.organisation.TypeId = this.selectedType.Id; this.organisation.IndustryId = this.selectedIndustry.Id; this.organisationService.saveOrganisationDetails(this.organisation).subscribe(organisationId=>{ this.organisationId = organisationId; //save role this.roleService.saveNewRole(this.organisationId,this.newRoleName,this.newRoleDescription).subscribe(roles=>{ this.roleList = roles; this.newRoleName = ''; this.newRoleDescription = ''; this.showNewRole = false; }) }) } else{ this.msgs = []; this.msgs.push({severity:'error', summary:'Unable to save', detail:'Please fill organisation details'}); } } } } // cancel new role cancelNewRole(){ this.newRoleName = ''; this.newRoleDescription = ''; this.showNewRole = false; } // get organisation roles getRoleList(){ this.roleService.getRoleList(this.organisationId).subscribe(roles=>{ this.roleList = roles; }) } // drag start staff dragStart(event,staff: IOrganisationStaff) { this.draggedStaff = staff; } // drag end staff dragEnd(event) { this.draggedStaff = null; } // drop staff drop(event,role:IRole) { if(this.draggedStaff) { role.StaffList.push(this.draggedStaff); // save staff added to the role this.roleService.addRoleStaff(this.organisationId,this.draggedStaff.PersonId,role.Id).subscribe(data=>{ // get not assigned staff list this.getNotAssignedStaff(); // get not assigned staff list under the company if(this.organisationId>0){ this.getNotAssignedStaffByCompany(); // get role list this.getRoleList(); } }) } } // save edited staff editStaff(staff:IOrganisationStaff){ console.log(staff.Name); if(staff.Name!=''){ this.roleService.editStaff(staff).subscribe(data=>{ }) } staff.ShowEdit = false; } // delete role staff deleteRoleStaff(role:IRole,staff:IOrganisationStaff){ this.roleService.deleteRoleStaff(staff.PersonId,role.Id,this.organisationId).subscribe(data=>{ role.StaffCount-=1; role.StaffList = role.StaffList.filter(x=>x.Id!==staff.Id); // Refresh not assigned by the company list this.getNotAssignedStaffByCompany(); }) } // delete staff deleteStaff(staffId:string,actionType:string){ if(actionType=='ByCompany'){ this.roleService.getNotAssignedStaff(this.organisationId,"DELETE",staffId).subscribe(staff=>{ if(staff!=null && staff != undefined){ //bind not assigned staff list this.notAssignedStaffsByCompany = staff; this.notAssignedByCompanyCount = staff.length; // bind title this.titleNotAssignedCompany = "Not assigned under "+this.organisation.CompanyName+" (" + staff.length + ")"; } }) // this.notAssignedStaffsByCompany = this.notAssignedStaffsByCompany.filter(x=>x.Id!==staffId); // this.notAssignedByCompanyCount-=1; // this.titleNotAssignedCompany = "Not assigned under "+this.organisation.CompanyName+" (" + this.notAssignedByCompanyCount + ")"; } else{ this.roleService.getNotAssignedStaff(0,"DELETE",staffId).subscribe(staff=>{ if(staff!=null && staff != undefined){ //bind not assigned staff list this.notAssignedStaffs = staff; // bind not assigned staff list count this.notAssignedCount = staff.length; // bind title if(this.organisationId > 0){ this.titleNotAssignedAny = "Not assigned to any organisations (" + this.notAssignedCount + ")"; } else{ this.titleNotAssignedAny = "Not assigned (" + this.notAssignedCount + ")"; } } }) // this.notAssignedStaffs = this.notAssignedStaffs.filter(x=>x.Id!==staffId); // this.notAssignedCount-=1; // if(this.organisationId > 0){ // this.titleNotAssignedAny = "Not assigned to any organisations (" + this.notAssignedCount + ")"; // } // else{ // this.titleNotAssignedAny = "Not assigned (" + this.notAssignedCount + ")"; // } } } // edit role editRole(role:IRole){ this.roleService.editRole(this.organisationId,role.Id,this.editRoleName,this.editRoleDescription).subscribe(data=>{ role.RoleName = this.editRoleName; role.RoleDescription = this.editRoleDescription; role.ShowEdit = false; this.editRoleName=''; this.editRoleDescription=''; }) } // cancel edit role cancelEditRole(role:IRole){ role.ShowEdit = false; this.editRoleName=''; this.editRoleDescription=''; } // delete role deleteRole(roleId:number){ this.roleService.deleteRole(this.organisationId,roleId).subscribe(data=>{ this.roleList = this.roleList.filter(x=>x.Id!==roleId); }) } //edit role permissions editPermissions(role:IRole){ const ref = this.dialogService.open(PermissionsComponent, { data: { role: role, organisationId: this.organisationId }, header: 'Edit role permissions', width: '75%' }); } showNewRolePanel(){ this.msgs = []; this.showNewRole=true; } showEditRolePanel(role:IRole){ role.ShowEdit=true; this.editRoleName = role.RoleName; this.editRoleDescription = role.RoleDescription; } //show add/edit staff dialog box showAddStaff(id:string) { const ref = this.dialogService.open(NewStaffComponent, { data: { id: id }, header: (id=='-1') ? 'Invite Staff' : 'Edit staff', width: '80%' }); } }