import { Component, OnInit } from '@angular/core'; import { CategoryViewModel, CatlogViewModel, ItemViewModel } from '../store-model'; import { NotificationService } from '../../../_services/notification.service'; import { StoreService } from '../store.services'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { SortDescriptor, orderBy } from '@progress/kendo-data-query'; import { PageChangeEvent } from '@progress/kendo-angular-grid'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-catlog', templateUrl: './catlog.component.html' }) export class CatlogComponent implements OnInit { public openDilog = false; public openDilogCat = false; public category = new CategoryViewModel(); public catlog = new CatlogViewModel(); public catCodeValid: any; public catlogCodeValid: any; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public categoriesList: any[] = []; public catlogList: any[] = []; public openDilogConfirmation = false; public kGrid: KGridHelper = new KGridHelper(); public deleting = false; public deletedDocId: number; constructor(public _notificationService: NotificationService, public _storeService: StoreService, public _localService: LocalService) { } ngOnInit() { this.initializeProps(); this.getCategory(); this.catlog.FK_CatrgoryID = new ItemViewModel(); this.catlog.FK_CatrgoryID.Id = null; this.catlog.FK_CatrgoryID.Name = "Select Type"; this.getCatlogs(); this.handleSearchEvents(); } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; } public OpenDileogForCategory() { this.openDilog = true; } public close(status) { this.openDilog = false; } public onSubmit(status) { if (!this.catCodeValid) { this._notificationService.notify("danger", "Dublicate Code Not Allowed"); return; } this.openDilog = false; this._storeService.saveCategory(this.category).subscribe((res: any) => { this._notificationService.notify("success", "Category Add Successfully"); this.getCategory(); }, err => { this._notificationService.notify("danger", err.msg); }); this.category = new CategoryViewModel(); } public validateCatCode(x) { this._storeService.validateCatCode(x).subscribe((res: any) => { debugger; if (res.msg == "Valid") { this.catCodeValid = true; } else { this.catCodeValid = false; } }, err => { this._notificationService.notify("danger", err); }); } public getCategory() { this._storeService.getCategory().subscribe((res: any) => { if (res) { this.categoriesList = res; } }, err => { this._notificationService.notify("danger", err.msg); }); } public OpenDileogForCatlog() { if (this.catlog.Code == null) { this._storeService.validateCatlogCode('-1').subscribe((res: any) => { debugger; if (res.msg == "Valid") { this.catlogCodeValid = true; this.catlog.Code = res.code; } else { this.catlogCodeValid = false; } }, err => { this._notificationService.notify("danger", err); }); } this.openDilogCat = true; } public closeCat(status) { this.catlog = new CatlogViewModel(); this.catlog.FK_CatrgoryID = new ItemViewModel(); this.catlog.FK_CatrgoryID.Id = null; this.catlog.FK_CatrgoryID.Name = "Select Type"; this.openDilogCat = false; this.openDilogConfirmation = false; } public saveCatlog() { if (!this.catlogCodeValid && !this.catlog.Id) { this._notificationService.notify("danger", "Dublicate Code Not Allowed"); return; } this.openDilogCat = false; this._storeService.saveCatlog(this.catlog).subscribe((res: any) => { this._notificationService.notify("success", "Catalog Save Successfully"); this.catlog = new CatlogViewModel(); this.catlog.FK_CatrgoryID = new ItemViewModel(); this.catlog.FK_CatrgoryID.Id = null; this.catlog.FK_CatrgoryID.Name = "Select Type"; this.getCatlogs(); }, err => { this._notificationService.notify("danger", err.msg); }); } public getCatlogs() { this._storeService.getCatlog(this.kGrid.skip, this.kGrid.pageSize, this.kGrid.searchQuery, this.kGrid.hfId, this.kGrid.geoLvlCode, this.kGrid.hfType, this.kGrid.stockId).subscribe((res: any) => { debugger; if (res) { this.kGrid.data = []; this.kGrid.data = res.res; this.kGrid.totalRecords = res.tc; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; } }, err => { debugger; this._notificationService.notify("danger", err.msg); }); } public validatCatlogCode(x) { this._storeService.validateCatlogCode(x).subscribe((res: any) => { debugger; if (res.msg == "Valid") { this.catlogCodeValid = true; } else { this.catlogCodeValid = false; } }, err => { this._notificationService.notify("danger", err); }); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { if (x.filter == "code") { this.validatCatlogCode(x.event); } if (x.filter == "Categorycode") { this.validateCatCode(x.event); } } }); } public EditCategory(x: any) { this.openDilogCat = true; let data: any = Object.assign({}, x); this.catlog = data; console.log(this.catlog); } public changePagesize(value: any) { this.kGrid.pageSize = +value; this.kGrid.skip = 0; this.getCatlogs(); } 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.getCatlogs(); } public RemoveCatlog(e) { this.deletedDocId = e; this.openDilogConfirmation = true; } public ConfirmDelete() { this._storeService.RemoveCatlog(this.deletedDocId).subscribe((res: any) => { this.deletedDocId = null; this.openDilogConfirmation = false; this._notificationService.notify("success", "Delete Successful"); this.getCatlogs() }, err => { this._notificationService.notify("danger", err.error); }); } }