import { Component, OnInit } from '@angular/core'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { Subject, Subscription } from 'rxjs'; import { StoreService } from '../../store/store.services'; import { NotificationService } from '../../../_services/notification.service'; import { ActivatedRoute, Router } from '@angular/router'; import { CoustomerServices } from '../coustome-services'; import { LocalService } from '../../../_services/local.service'; import { SortDescriptor, orderBy } from '@progress/kendo-data-query'; import { PageChangeEvent } from '@progress/kendo-angular-grid'; import { debounceTime } from 'rxjs/operators'; import { DirectCashCollectionViewModel } from '../coustomer-class'; @Component({ selector: 'app-direct-cash-collection', templateUrl: './direct-cash-collection.component.html' }) export class DirectCashCollectionComponent implements OnInit { public kGrid: KGridHelper = new KGridHelper(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public saving = false; public deleting = false; public maxDate: Date = new Date(); public createOrUpdateDialog = false; public deleteConfirmationDialog = false; public _isEdit = false; public employeeAccountList: any[] = []; public collectionAccountList: any[] = []; public payableAccountList: any[] = []; public vm: DirectCashCollectionViewModel = new DirectCashCollectionViewModel(); constructor(public _storeService: StoreService, public _notificationService: NotificationService, private activatedRoute: ActivatedRoute, private router: Router, private _coustomerServices: CoustomerServices, public _localService: LocalService) { } ngOnInit() { this.initializeProps(); this.getAccount(' ', 'E'); this.getAccount(' ', 'CaB'); this.getAccount(' ', 'COD'); this.handleSearchEvents(); this.getList(); } CreateOrUpdateDialog() { this.vm = new DirectCashCollectionViewModel(); this._isEdit = false; this.createOrUpdateDialog = true; } getList() { this.kGrid.loading = true; this._coustomerServices.GetDCCList().subscribe( (response: any) => { console.log(response); this.kGrid.data = []; this.kGrid.data = response; this.kGrid.totalRecords = response.length; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; console.log(this.kGrid.gridView); }, err => this.handleError(err) ); this.maxDate.setHours(this.maxDate.getHours() + 1); } OnSubmit() { this.saving = true; this._coustomerServices.SaveDCC(this.vm).subscribe((res) => { this._notificationService.notify('success', 'Saved Successfully'); this.saving = false; this.close(); this.getList(); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); this.saving = false; }); this.maxDate.setHours(this.maxDate.getHours() + 1); } EditDialog(item) { this.vm = item; this.vm.StartDate = new Date(item.StartDate); this.vm.EndDate = new Date(item.EndDate); this.vm.TransactionDate = new Date(item.TransactionDate); this._isEdit = true; this.createOrUpdateDialog = true; } DeleteDialog(item) { this.vm = item; this.deleteConfirmationDialog = true; } OnDelete() { this.deleting = true; this._coustomerServices.DeleteDCC(this.vm.Id).subscribe((res) => { this._notificationService.notify('success', 'Deleted Successfully'); this.deleting = false; this.close(); this.getList(); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); this.deleting = false; }); } public getFIle(event) { this.vm.file = event.target.files[0]; } public close() { this.createOrUpdateDialog = false; this.deleteConfirmationDialog = false; } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; } 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.getList(); } private handleError(err: any) { this.kGrid.loading = false; // if (err.status == 403) { // this._authenticationService.logout(); // } } onBinClick(aid: number) { alert(aid); } public changePagesize(value: any) { this.kGrid.pageSize = +value; this.kGrid.skip = 0; this.getList(); } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event === 'E') { this.employeeAccountList = res; } if (event === 'CaB') { this.collectionAccountList = res; } if (event === 'COD') { this.payableAccountList = res; } } }, err => { this._notificationService.notify('danger', err.msg); }); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { if (x.filter === 'employee') { this.getAccount(x.event, 'E'); } if (x.filter === 'payables') { this.getAccount(x.event, 'COD'); } if (x.filter === 'collection') { this.getAccount(x.event, 'CaB'); } } }); } }