import { Component, OnInit, Input, OnDestroy } from '@angular/core'; import { SortDescriptor, orderBy } from '@progress/kendo-data-query'; import { GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; import { HealthFacilityService } from '../../../health-facility.service'; import { VpMProfileView, VPDetail, VpDProfileView } from '../../../../vacancy-position/vacancy-position.class'; import { DropDownsHR } from '../../../../../_helpers/dropdowns.class'; import { Subject } from 'rxjs/Subject'; import { Subscription } from 'rxjs/Subscription'; import { RootService } from '../../../../../_services/root.service'; import { AuthenticationService } from '../../../../../_services/authentication.service'; import { debounceTime } from 'rxjs/operators/debounceTime'; @Component({ selector: 'app-vacancy', templateUrl: './vacancy.component.html', styles: [ ` .details-head-vacancy { font-size: 14px; font-family: "Roboto-Regular"; } .details-content-vacancy { font-size: 14px; font-family: "Roboto-Light"; } ` ] }) export class VacancyComponent implements OnInit, OnDestroy { @Input() public hfmisCode: string = ''; @Input() public vacancy: any[] = []; @Input() public employementModes: any[] = []; public vpMaster: VpMProfileView; public loading: boolean = true; public saving: boolean = false; public vacancyChanged: boolean = false; public vacancyBefore: number = 0; public vacancySanctionedBefore: number = 0; public filledExceed: boolean = false; public dropDowns: DropDownsHR = new DropDownsHR(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public multiple = false; public allowUnsort = true; public sort: SortDescriptor[] = []; public gridView: GridDataResult; public showProfile = false; public showProfileEmpMode_Id: number = 0; public profiles: any[] = []; public profileWindowView = false; public profilesWindow: any = { dialogOpened: false, data: null, profiles: [], } constructor(private _authenticationService: AuthenticationService, private _rootService: RootService, private _healthFacilityService: HealthFacilityService) { } ngOnInit() { this.getVacancy(this.hfmisCode); this.handleSearchEvents(); } public saveVacancy() { this.checkFilledExceed(); if (this.filledExceed) return; this.saving = true; this._healthFacilityService.saveVacancy(this.vpMaster).subscribe((res: any) => { if (res && res.Id) { this.closeWindow(); } console.log(res); }, err => { this.handleError(err); }); } public sortChange(sort: SortDescriptor[]): void { if (sort[0].field == 'asd') { return; } this.sort = sort; this.loadVacancy(); } private getVacancy(code: string): void { this.loading = true; this._healthFacilityService.getHFVacancy(code).subscribe((data: any) => { if (data) { this.vacancy = data; this.loadVacancy(); this.saving = false; this.loading = false; } }, err => { this.handleError(err); }); } private loadVacancy(): void { this.gridView = { data: orderBy(this.vacancy, this.sort), total: this.vacancy.length }; } public closeWindow() { if (this.vacancyBefore != this.vpMaster.TotalWorking || this.vacancySanctionedBefore != this.vpMaster.TotalSanctioned) { this.vacancyChanged = true; this.getVacancy(this.hfmisCode); } else { this.saving = false; } this.showProfile = false; this.profilesWindow.dialogOpened = false; this.profilesWindow.data = null; this.employementModes.forEach(empMode => { empMode.totalWorking = undefined; empMode.profiles = undefined; }); this.vpMaster = new VpMProfileView(); this.checkFilledExceed(); } public loadDetails(item) { console.log(item); } public openWindow(dataItem) { this.profilesWindow.data = dataItem; this._healthFacilityService.getVpDProfileViews(dataItem.Id).subscribe((res: any) => { if (res) { this.vpMaster = res.vpMaster; this.vacancySanctionedBefore = this.vpMaster.TotalSanctioned; this.vacancyBefore = this.vpMaster.TotalWorking; this.vpMaster.VPDetails = res.vpDetails; this.setEmpModeDetail(); this._healthFacilityService.getProfilesAgainstVacancy(this.profilesWindow.data.HF_Id, this.profilesWindow.data.Desg_Id).subscribe(data => { this.profilesWindow.profiles = data; console.log(this.vpMaster); }); } }); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x.filter == 'TotalSanctioned') { this.checkFilledExceed(); } else if (x.filter == 'employementMode') { console.log(x); this.empModeValueChanged(x.Id, +x.event.target.value); } }); } public empModeValueChanged(id: number, total: number) { let vpDetail = this.vpMaster.VPDetails.find(x => x.EmpMode_Id == id); if (vpDetail) { vpDetail.TotalWorking = total; } else { vpDetail = new VpDProfileView(); vpDetail.EmpMode_Id = id; vpDetail.TotalWorking = total; this.vpMaster.VPDetails.push(vpDetail); } this.checkFilledExceed(); } public checkFilledExceed() { let totalFilled = this.vpMaster.TotalWorking; this.vpMaster.TotalWorking = 0; this.vpMaster.VPDetails.forEach(vpDetail => { this.vpMaster.TotalWorking += vpDetail.TotalWorking ? vpDetail.TotalWorking : 0; }); this.filledExceed = this.vpMaster.TotalWorking > this.vpMaster.TotalSanctioned ? true : false; if (this.filledExceed) { this.vpMaster.TotalWorking = totalFilled; } } public setEmpModeDetail() { this.vpMaster.VPDetails.forEach(vpDetail => { let empMode = this.employementModes.find(x => x.Id == vpDetail.EmpMode_Id); if (empMode) { empMode.totalWorking = vpDetail.TotalWorking; empMode.profiles = vpDetail.Profiles; } }); this.profilesWindow.dialogOpened = true; } public action(status) { console.log(`Dialog result: ${status}`); this.profilesWindow.dialogOpened = false; } public showProfileClicked(empModeId: number) { this.profiles = []; this.showProfileEmpMode_Id = empModeId; console.log(this.showProfileEmpMode_Id); if (this.showProfileEmpMode_Id == 0) { this.profiles = this.profilesWindow.profiles; } else { this.profilesWindow.profiles.forEach(profile => { if (profile.EmpMode_Id == this.showProfileEmpMode_Id) { this.profiles.push(profile); } }); } console.log(this.profilesWindow.profiles); console.log(this.profiles); this.showProfile = !this.showProfile; } public loadPhoto(src: string) { let photo = new Image(); photo.src = src; photo.onload = () => { return true; } } public onTabSelect(e) { console.log(e); //this.selectedTab = e.heading; } public dashifyCNIC(cnic: string) { return cnic[0] + cnic[1] + cnic[2] + cnic[3] + cnic[4] + '-' + cnic[5] + cnic[6] + cnic[7] + cnic[8] + cnic[9] + cnic[10] + cnic[11] + '-' + cnic[12]; } private handleError(err: any) { this.loading = false; if (err.status == 403) { this._authenticationService.logout(); } } ngOnDestroy(): void { this.searchSubcription.unsubscribe(); } }