import { Component, OnInit, ViewChild } from '@angular/core'; import { SortDescriptor, orderBy } from '@progress/kendo-data-query'; import { GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; import { AuthenticationService } from '../../_services/authentication.service'; import { RootService } from '../../_services/root.service'; import { DropDownsHR } from '../../_helpers/dropdowns.class'; import { VacancyPositionService } from './vacancy-position.service'; import { aggregateBy } from '@progress/kendo-data-query'; @Component({ selector: 'app-vacancy-position', templateUrl: './vacancy-position.component.html', styles: [] }) export class VacancyPositionComponent implements OnInit { public loading = true; public vacancyReport: any[] = []; public vacancyReportGrandTotal: any = {}; public vacancyDynamicReport: any[] = []; public vacancyDynamicReportGrandTotal: any[] = []; public vacancyTotal: any; public vacancyAggregates: any[] = []; public gridView: GridDataResult; //sorting variable public multiple = false; public allowUnsort = true; public sort: SortDescriptor[] = []; public showFilters: boolean = false; public hfmisCode: string = '0'; public hfTypeCodes: any[] = []; public dropDowns: DropDownsHR = new DropDownsHR(); public selectedFiltersModel: any; public currentUser: any; constructor(private _rootService: RootService, private _vacancyPositionService: VacancyPositionService, private _authenticationService: AuthenticationService) { } ngOnInit() { this.currentUser = this._authenticationService.getUser(); this.getReport(this.currentUser.HfmisCode); this.loadDropdownValues(); } public onSearch = () => { } private loadDropdownValues = () => { this.getDivisions(this.currentUser.HfmisCode); this.getDistricts(this.currentUser.HfmisCode); this.getTehsils(this.currentUser.HfmisCode); this.getHFTypes(); } private getReport = (code: string) => { this._rootService.getVacancyReport(code).subscribe((res: any) => { this.vacancyReport = res; this.setVacancyAggregates(); /* this.vacancyReport = res.reportModel; this.vacancyReportGrandTotal.Sanctioned = res.GrandTotalSanctioned; this.vacancyReportGrandTotal.Working = res.GrandTotalWorking; this.vacancyReportGrandTotal.Profiles = res.GrandTotalProfiles; this.vacancyReportGrandTotal.Vacant = res.GrandTotalVacant; console.log(res); */ this.gridView = { data: this.vacancyReport, total: this.vacancyReport.length }; this.loading = false; }, err => { this.handleError(err); } ); } private getDivisions = (code: string) => { this.dropDowns.divisions = []; this.dropDowns.divisionsData = []; this._rootService.getDivisions(code).subscribe((res: any) => { this.dropDowns.divisions = res; this.dropDowns.divisionsData = this.dropDowns.divisions.slice(); }, err => { this.handleError(err); } ); } private getDistricts = (code: string) => { this.dropDowns.districts = []; this.dropDowns.districtsData = []; this._rootService.getDistricts(code).subscribe((res: any) => { this.dropDowns.districts = res; this.dropDowns.districtsData = this.dropDowns.districts.slice(); }, err => { this.handleError(err); } ); } private getTehsils = (code: string) => { this.dropDowns.tehsils = []; this.dropDowns.tehsilsData = []; this._rootService.getTehsils(code).subscribe((res: any) => { this.dropDowns.tehsils = res; this.dropDowns.tehsilsData = this.dropDowns.tehsils.slice(); }, err => { this.handleError(err); } ); } private getHFTypes = () => { this._rootService.getHFTypes().subscribe((res: any) => { this.dropDowns.hfTypes = res; this.dropDowns.hfTypesData = this.dropDowns.hfTypes.slice(); }, err => { this.handleError(err); } ); } public dropdownValueChanged = (value, filter) => { if (!value || !value.Code) { return; } if (filter == 'division') { this.hfmisCode = value.Code; this.resetDrops(filter); this.getDistricts(value.Code); this.getTehsils(value.Code); } if (filter == 'district') { this.hfmisCode = value.Code; this.resetDrops(filter); this.getTehsils(value.Code); } if (filter == 'tehsil') { this.hfmisCode = value.Code; } if (filter == 'hfType') { this.hfmisCode += value.Code; } } private getHFCatgetCategoryegory = () => { } public sortChange(sort: SortDescriptor[]): void { if (sort[0].field == 'asd') { return; } this.sort = sort; this.sortData(); } private sortData() { this.gridView = { data: orderBy(this.vacancyReport, this.sort), total: this.vacancyReport.length }; } private resetDrops = (filter: string) => { if (filter == 'division') { this.selectedFiltersModel.district = { Name: 'Select District', Code: '0' }; this.selectedFiltersModel.tehsil = { Name: 'Select Tehsil', Code: '0' }; } if (filter == 'district') { this.selectedFiltersModel.tehsil = { Name: 'Select Tehsil', Code: '0' }; } } public handleFilter = (value, filter) => { if (filter == 'division') { this.dropDowns.divisionsData = this.dropDowns.divisions.filter((s: any) => s.Name.toLowerCase().indexOf(value.toLowerCase()) !== -1); } if (filter == 'district') { this.dropDowns.districtsData = this.dropDowns.districts.filter((s: any) => s.Name.toLowerCase().indexOf(value.toLowerCase()) !== -1); } if (filter == 'tehsil') { this.dropDowns.tehsilsData = this.dropDowns.tehsils.filter((s: any) => s.Name.toLowerCase().indexOf(value.toLowerCase()) !== -1); } } private handleError(err: any) { this.loading = false; if (err.status == 403) { this._authenticationService.logout(); } } public setVacancyAggregates() { this.vacancyAggregates = [ { field: 'TotalSanctioned', aggregate: 'sum' }, { field: 'TotalWorking', aggregate: 'sum' }, { field: 'TotalVacant', aggregate: 'sum' }, { field: 'TotalProfile', aggregate: 'sum' } ]; this.vacancyTotal = aggregateBy(this.vacancyReport, this.vacancyAggregates); } public numberWithCommas(x) { if (x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } } }