import { Component, OnInit, OnDestroy } from '@angular/core'; import { Subject } from 'rxjs/Subject'; import { debounceTime } from 'rxjs/operators/debounceTime'; import { RootService } from '../../_services/root.service'; import { DropDownsHR } from '../../_helpers/dropdowns.class'; import { OrderService } from './order.service'; import { AuthenticationService } from '../../_services/authentication.service'; import { Subscription } from 'rxjs/Subscription'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-order', templateUrl: './order.component.html', styles: [] }) export class OrderComponent implements OnInit, OnDestroy { public value: any; public loaded: boolean = false; public loadedContent: boolean = false; public controlPlaceHolders: boolean[] = [false]; public thumbPlaceHolders: boolean[] = [false, false]; public inputChange: Subject; public dropDowns: DropDownsHR = new DropDownsHR(); public orders: any[] = []; public orderTypes: any[] = []; public totalOrders: number = 0; public orderType_Id: number = 0; public currentPage: number = 1; public pageSize: number = 20; public searchQuery: string = ''; private subscription: Subscription; public applicationTypeWindow: any = { dialogOpened: false, data: null, orderTypes: [], } constructor(private _rootService: RootService, private _authService: AuthenticationService, private route: ActivatedRoute, private _orderService: OrderService, private _authenticationService: AuthenticationService) { } ngOnInit() { this.inputChange = new Subject(); this.inputChange.pipe(debounceTime(800)).subscribe((query) => { this.searchQuery = query; if (!query) { this.loadOrders(); return; } }); this.fetchParams(); this.loadOrderTypes(); this.loadOrders(); } private fetchParams() { this.subscription = this.route.params.subscribe( (params: any) => { if (params.hasOwnProperty('otid')) { this.orderType_Id = +params['otid']; } if (params.hasOwnProperty('currentPage') && +params['currentPage']) { this.currentPage = +params['currentPage']; } } ); } public loadOrders = () => { this.loadedContent = false; let user = this._authService.getUser(); let districtCode = user.HfmisCode.length == 1 ? '0' : user.UserName; this._orderService.getOrders({ Query: this.searchQuery }, districtCode, this.dropDowns.selectedFiltersModel.orderType.Id, this.currentPage, this.pageSize).subscribe((data: any) => { this.orders = data.esrlist; this.totalOrders = data.totalRecords; this.loaded = true; this.loadedContent = true; }, err => { console.log(err); }); } public loadOrderTypes = () => { this._rootService.getOrderTypes().subscribe((data: any) => { this.orderTypes = data; this.dropDowns.orderTypes = data; this.dropDowns.orderTypesData = this.dropDowns.orderTypes; if (this.orderType_Id && this.orderType_Id != 0) { let selectOrderType = data.find(x => x.Id == this.orderType_Id); this.dropDowns.selectedFiltersModel.orderType = { Name: selectOrderType.Name, Id: selectOrderType.Id }; } }); } public onSearch = () => { this.loadOrders(); } private handleError(err: any) { this.loadedContent = true; if (err.status == 403) { this._authenticationService.logout(); } } public closeWindow() { this.applicationTypeWindow.dialogOpened = false; } public openWindow(dataItem) { this.applicationTypeWindow.data = dataItem; this.applicationTypeWindow.dialogOpened = true; this._rootService.getOrderTypes().subscribe(data => { this.applicationTypeWindow.orderTypes = data; this.applicationTypeWindow.dialogOpened = true; }); } ngOnDestroy() { this.subscription.unsubscribe(); } 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]; } }