import { Component, OnInit } from '@angular/core'; import { AuthenticationService } from '../../../_services/authentication.service'; import { RootService } from '../../../_services/root.service'; import { StockService } from '../stock.service'; import { NotificationService } from '../../../_services/notification.service'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { Subject } from 'rxjs/Subject'; import { debounceTime } from 'rxjs/operators/debounceTime'; import { SortDescriptor } from '@progress/kendo-data-query/dist/npm/sort-descriptor'; import { orderBy } from '@progress/kendo-data-query/dist/npm/array.operators'; import { PageChangeEvent } from '@progress/kendo-angular-grid/dist/es2015/data/change-event-args.interface'; import { Subscription } from 'rxjs/Subscription'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-activity-log', templateUrl: './activity-log.component.html', styles: [] }) export class ActivityLogComponent implements OnInit { public kGrid: KGridHelper = new KGridHelper(); public HFList: any[] = []; public inputChange: Subject; public searchQuery: string = ''; public typeId: number = 0; public statusId: number = 0; public searchSubcription: Subscription = null; public searchEvent = new Subject(); public currentUser: any; public showHFName = true; public HFL = false; public action = false; public HF: any; public Division: any[] = []; public SelectedDivision: any; public Districts: any[] = []; public SelectedDistricts: any; public Tehsils: any[] = []; public SelectedTehsils: any; public HFType: any[] = []; public SelectedHFType: any; constructor(private _stockService: StockService, private _rootService: RootService, private _authenticationService: AuthenticationService, public _notificationService: NotificationService, private _localService: LocalService) { } ngOnInit() { debugger; this.currentUser = this._authenticationService.getUser(); console.log(this.currentUser.HFName) if (this.currentUser != null) { if (this.currentUser.RoleName == "HF") { this.showHFName = false; } if (this.currentUser.RoleName == "Admin" || this.currentUser.RoleName == "CEO") { this.HFL = true; } if (this.currentUser.UserName != 'CCR@gmail.com') { this.action = true; } } this.inputChange = new Subject(); this.inputChange.pipe(debounceTime(300)).subscribe((query) => { this.searchQuery = query; if (this.searchQuery.length <= 2 && this.searchQuery.length != 0) { return; } this.getStockDtl(); }); this.loadHFTypes(); this.loadDivision(); this.initializeProps(); this.handleSearchEvents(); this.getStockDtl(); } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; this.kGrid.hfId = '0'; this.kGrid.hfType = ''; this.kGrid.geoLvlCode = ''; this.kGrid.stockId = null; } public loadDivision() { this.Division = []; this._rootService.getDivisions(this._authenticationService.getUserHfmisCode()) .subscribe((x: any) => { if (x) { this.Division = x; } }); } public loadDistrict(divCode: string) { debugger this.Districts = []; this._rootService.getDistrictsOnChang(divCode) .subscribe((x: any) => { if (x) { this.Districts = x; } }); } public loadTehsils(disCode: string) { this.Tehsils = []; this._rootService.getTehsilsOnChang(disCode) .subscribe((x: any) => { if (x) { this.Tehsils = x; } }); } public loadHFTypes() { this.HFType = []; this._rootService.getHFTypes() .subscribe((x: any) => { if (x) { this.HFType = x; } }); } public getStockDtl() { this.kGrid.loading = true; this._stockService.getStockDtl(this.kGrid.skip, this.kGrid.pageSize, this.searchQuery, this.kGrid.hfId, this.kGrid.geoLvlCode, this.kGrid.hfType, this.kGrid.stockId).subscribe( (response: any) => { console.log(response); this.kGrid.data = []; this.kGrid.data = response.List; this.kGrid.totalRecords = response.Count; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; }, err => this.handleError(err) ); } getHfList(geoLvl: string, hfTypeCode: string) { this._rootService.getHfList(geoLvl, hfTypeCode).subscribe( (res: any) => { if (res) { this.HFList = res; } }, err => { } ); } onBinClick(sid: number) { this._stockService.removeStockById(sid).subscribe((res: any) => { if (res) { this._notificationService.notify("success", res); this.getStockDtl(); } }, err => { }); } public dropdownValueChanged = (value, filter) => { console.log(value); if (!value) { return; } if (filter == 'Div') { this.kGrid.geoLvlCode = value.Code; this.loadDistrict(value.Code); this.SelectedDistricts = null; this.SelectedTehsils = null; this.HF = null; this.kGrid.hfId = '0'; } if (filter == 'Dis') { this.kGrid.geoLvlCode = value.Code; this.loadTehsils(value.Code); this.SelectedTehsils = null; this.HF = null; this.kGrid.hfId = '0'; } if (filter == 'teh') { this.kGrid.geoLvlCode = value.Code; this.getHfList(value.Code, '0'); this.HF = null; this.SelectedHFType = null; this.kGrid.hfId = '0'; } if (filter == 'hft') { this.getHfList(this.kGrid.geoLvlCode, value.Code); this.HF = null; this.kGrid.hfType = value.Code; this.kGrid.hfId = '0'; } if (filter == 'hf') { this.kGrid.hfId = value.Id; } } ClearFilter() { this.initializeProps(); this.getStockDtl(); this.SelectedDivision = null; this.SelectedDistricts = null; this.SelectedTehsils = null; this.HF = null; this.SelectedHFType = null; this.kGrid.stockId = null; } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { //console.log(x); if (x) { if (x.filter == "hf") { } } }); } public sortChange(sort: SortDescriptor[]): void { if (sort[0].field == 'asd') { return; } this.kGrid.sort = sort; this.sortData(); } private sortData() { this.kGrid.gridView = { data: orderBy(this.kGrid.data, this.kGrid.sort), total: this.kGrid.totalRecords }; } public pageChange(event: PageChangeEvent): void { this.kGrid.skip = event.skip; this.getStockDtl(); } public changePagesize(value: any) { this.kGrid.pageSize = +value; this.kGrid.skip = 0; this.getStockDtl(); } private handleError(err: any) { this.kGrid.loading = false; if (err.status == 403) { this._authenticationService.logout(); } } ngOnDestroy(): void { this.searchSubcription.unsubscribe(); } }