import { Component, OnInit } from '@angular/core'; import { PageChangeEvent } from '@progress/kendo-angular-grid'; import { orderBy, SortDescriptor } from '@progress/kendo-data-query'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { NotificationService } from '../../../_services/notification.service'; import { EmployeeServices } from '../employee-services'; import { LocalService } from '../../../_services/local.service'; import { ItemViewModel } from '../../store/store-model'; import { EventTypeViewModel, EventMastViewModel, CommentMastViewModel } from '../employee-class'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { StoreService } from '../../store/store.services'; @Component({ selector: 'app-event-log', templateUrl: './event-log.component.html' }) export class EventLogComponent implements OnInit { public kGrid: KGridHelper = new KGridHelper(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public saving = false; public deleting = false; public vm: EventMastViewModel = new EventMastViewModel(); public commentVm: CommentMastViewModel = new CommentMastViewModel(); public createOrUpdateDialog = false; public createOrUpdateCommentDialog = false; public deleteConfirmationDialog = false; public deletingComment = false; public employeeAccountList: any[] = []; public eventList: any[] = []; public eventCommentList: any = []; public _isEdit = false; constructor(public _notificationService: NotificationService, public _employeeServices: EmployeeServices, public _storeService: StoreService, public _localService: LocalService) { } ngOnInit() { this.initializeProps(); this.getEventLogs(); this.getEventLogTypes(); this.getAccount(' ', 'E'); this.handleSearchEvents(); } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; } public getEventLogs() { this.kGrid.loading = true; this._employeeServices.getEventList().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) ); } public getEventLogTypes() { this._employeeServices.getEventTypeList().subscribe( (response: any) => { this.eventList = response; }, err => this.handleError(err) ); } CreateOrUpdateDialog() { this.vm = new EventMastViewModel(); this._isEdit = false; this.createOrUpdateDialog = true; } GetEventCommentList(id) { this._employeeServices.getEventCommentList(id).subscribe((res: any) => { console.log(res); this.eventCommentList = res; }, (err) => { }); } CreateOrUpdateCommentDialog(item) { this.commentVm.Fk_EventMast = item; this.GetEventCommentList(item.Id); this.createOrUpdateCommentDialog = true; } OnSubmit() { this.saving = true; this._employeeServices.saveEventMaster(this.vm).subscribe((res) => { this._notificationService.notify('success', 'Saved Successfully'); this.saving = false; this.close(); this.getEventLogs(); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } EditDialog(item) { this.vm = item; this.vm.StartDate = new Date(item.StartDate); this.vm.EndDate = new Date(item.EndDate); this._isEdit = true; this.createOrUpdateDialog = true; } DeleteDialog(item) { this.vm = item; this.deleteConfirmationDialog = true; } OnDelete() { this.deleting = true; this._employeeServices.deleteEventType(this.vm.Id).subscribe((res) => { this._notificationService.notify('success', 'Deleted Successfully'); this.deleting = false; this.close(); this.getEventLogs(); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } OnSubmitComment() { this.saving = true; var eventMastid = this.commentVm.Fk_EventMast.Id; this._employeeServices.saveEventComment(this.commentVm).subscribe((res) => { this._notificationService.notify('success', 'Saved Successfully'); this.saving = false; // this.close(); this.GetEventCommentList(eventMastid); this.commentVm = new CommentMastViewModel(); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } OnDeleteComment(item) { this.deletingComment = true; var eventMastid = item.Fk_EventMast.Id; this._employeeServices.deleteEventComment(item.Id).subscribe((res) => { this._notificationService.notify('success', 'Deleted Successfully'); this.deletingComment = false; this.GetEventCommentList(eventMastid); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } public close() { this.createOrUpdateDialog = false; this.deleteConfirmationDialog = false; this.createOrUpdateCommentDialog = false; } 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.getEventLogs(); } 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.getEventLogs(); } 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.debitAccountList = 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) { debugger; if (x.filter === 'employee') { this.getAccount(x.event, 'E'); } if (x.filter === 'empEvent') { this.getEventLogTypes(); } if (x.filter === 'DebitAccounts') { this.getAccount(x.event, 'CaB'); } } }); } }