import { Component, OnInit } from '@angular/core'; import { KGridHelper } from '../../_helpers/k-grid.class'; import { DropDownsHR } from '../../_helpers/dropdowns.class'; import { Subject } from 'rxjs/Subject'; import { RootService } from '../../_services/root.service'; import { ApplicationFtsService } from '../../modules/application-fts/application-fts.service'; import { AuthenticationService } from '../../_services/authentication.service'; import { debounceTime } from 'rxjs/operators/debounceTime'; import { PageChangeEvent } from '@progress/kendo-angular-grid'; import { FileTrackingSystemService } from '../file-tracking-system.service'; @Component({ selector: 'app-inbox', templateUrl: './inbox.component.html', styles: [] }) export class InboxComponent implements OnInit { public kGrid: KGridHelper = new KGridHelper(); public dropDowns: DropDownsHR = new DropDownsHR(); public inputChange: Subject; public dateNow: string = ''; public searchQuery: string = ''; public office_Id: number = 0; public officeName: number = 0; public recieveFiles: boolean = false; public savingFileMovement: boolean = false; public acknowledgeDialogOpened: boolean = false; public fileMoveMaster: any; public selectedApplications: any[] = []; public selectedApplicationsIndex: number[] = []; constructor(private _rootService: RootService, private _fileTrackingSystemService: FileTrackingSystemService, private _applicationFtsService: ApplicationFtsService, private _authenticationService: AuthenticationService) { } ngOnInit() { this.kGrid.firstLoad = 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.getApplications(); }); this.initializeProps(); this.loadDropDownValues(); this.getApplications(); } private initializeProps() { let today = new Date(); let dd: any = today.getDate(), mm: any = today.getMonth() + 1, yyyy = today.getFullYear(); if (dd < 10) dd = '0' + dd; if (mm < 10) mm = '0' + mm; this.dateNow = dd + '/' + mm + '/' + yyyy; this.kGrid.loading = false; this.kGrid.pageSize = 50; this.kGrid.pageSizes = [20,50, 100]; } private loadDropDownValues() { this.getInboxOffices(); } private getInboxOffices() { this.selectedApplicationsIndex = []; this._fileTrackingSystemService.getOffices(true).subscribe((response: any) => { this.dropDowns.inboxOfficers = response; }, err => { this.handleError(err); }); } private getApplications() { this.kGrid.loading = true; this.selectedApplicationsIndex = []; this._fileTrackingSystemService.getApplications(this.kGrid.skip, this.kGrid.pageSize, this.searchQuery, this.office_Id, true).subscribe( (response: any) => { this.kGrid.data = []; this.kGrid.data = response.List; this.kGrid.dataOrigional = response.List; this.kGrid.totalRecords = response.Count; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; this.kGrid.firstLoad = false; if (this.office_Id != 0) { this.recieveFiles = true; } else { this.recieveFiles = false; } }, err => this.handleError(err) ); } public generateSlip = () => { this.selectedApplications = []; this.selectedApplicationsIndex.forEach(index => { this.selectedApplications.push(this.kGrid.data[index]); }); setTimeout(() => { this.openWindow(); }, 600); } public saveFileMovement() { this.savingFileMovement = true; this._applicationFtsService.submitFileMovement(this.selectedApplications).subscribe((x: any) => { console.log(x); this.fileMoveMaster = x; if (this.fileMoveMaster.Id) { this.savingFileMovement = false; this.closeWindow(); this.getInboxOffices(); this.printApplication(); } }, err => { this.handleError(err); }); } public dropdownValueChanged = (value, filter) => { if (filter == 'office') { this.office_Id = value.Id; this.officeName = value.Name; this.selectedApplicationsIndex = []; this.getApplications(); } } public pageChange(event: PageChangeEvent): void { this.kGrid.skip = event.skip; this.getApplications(); } public changePagesize(value: any) { this.kGrid.pageSize = +value; this.kGrid.skip = 0; this.getApplications(); } public closeWindow() { this.acknowledgeDialogOpened = false; } public dialogAction(action: string) { if (action == 'yes') { this.saveFileMovement(); } else { this.closeWindow(); } } public openWindow() { this.acknowledgeDialogOpened = true; } private handleError(err: any) { this.savingFileMovement = false; this.kGrid.loading = false; if (err.status == 403) { this._authenticationService.logout(); } } public dashifyCNIC(cnic: string) { if (!cnic) return ''; 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]; } public numberWithCommas(x) { if (x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } } printApplication() { let html = document.getElementById('formPrint').innerHTML; var mywindow = window.open('', 'PRINT', 'height=600,width=900'); if (mywindow) { mywindow.document.write(` Application`); mywindow.document.write(''); mywindow.document.write(''); mywindow.document.write(html); mywindow.document.write(` `); mywindow.document.write(''); this.dropDowns.selectedFiltersModel.inboxOfficers = { Name: 'Select Office', Id: 0 }; this.dropdownValueChanged(this.dropDowns.selectedFiltersModel.inboxOfficers, 'office'); this.getApplications(); /* mywindow.document.close(); // necessary for IE >= 10 mywindow.focus(); // necessary for IE >= 10 mywindow.print(); mywindow.close(); */ } } }