import { Component, OnInit } from '@angular/core'; import { Subject, Subscription } from 'rxjs'; import { StockAllocateViewModel, ItemViewModel } from '../MedStock.class'; import { RootService } from '../../../_services/root.service'; import { debounceTime } from 'rxjs/operators'; import { StockService } from '../stock.service'; import { NotificationService } from '../../../_services/notification.service'; import { fail } from 'assert'; import { AuthenticationService } from '../../../_services/authentication.service'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-allocate', templateUrl: './allocate.component.html', styles: [] }) export class AllocateComponent implements OnInit { public MedList: any[] = []; public BatchList: any[] = []; public stock: StockAllocateViewModel = new StockAllocateViewModel(); public DepList: any[] = []; public saving = false; public searchEvent = new Subject(); public searchSubcription: Subscription = null; constructor(private _authenticationService: AuthenticationService, private _rootService: RootService, private _stockService: StockService, public _notificationService: NotificationService, private _localService: LocalService) { } ngOnInit() { this.handleSearchEvents(); this.getDepList(); this.stock.MedicineId = new ItemViewModel(); } getDepList() { this._rootService.getDepList().subscribe( (res: any) => { this.DepList = res; }, err => { this.handleError(err); }) } public dropdownValueChanged = (value, filter) => { if (!value) { return; } if (filter == 'MedicineId') { this.stock.MedicineId.Id = this.MedList.find(x => x.Name == value).Id; this.getMedAvailableBatch(this.stock.MedicineId.Id); } } getMedAvailableBatch(medId: number) { this._stockService.getMedAvailableBatch(medId).subscribe((res: any) => { if (res) { this.BatchList = res; console.log(this.BatchList) } }, err => { this.handleError(err); }) } onSubmit() { if(!this.stock.MedicineId.Id) { this._notificationService.notify("danger", "Select Medicine from the list"); }else{ this.saving = true; this.removeMeta(this.stock,'$id'); this._stockService.saveAllocateStock(this.stock).subscribe( (res: any) => { if (res == "success") { this._notificationService.notify("success", "Allocate Successfully.") this.stock = new StockAllocateViewModel(); this.stock.MedicineId = new ItemViewModel(); } else { this._notificationService.notify("danger", res) } this.saving = false; }, err => { this.handleError(err); this._notificationService.notify("danger", err) }) } } fetchMedicineData(search: string) { this._rootService.getMedList(search).subscribe( (res: any) => { if (res) { this.MedList = res; } }, err => { this.handleError(err); } ); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { //console.log(x); if (x) this.fetchMedicineData(x); }); } ngOnDestroy(): void { this.searchSubcription.unsubscribe(); } public search(value: string, filter: string) { debugger; } private handleError(err: any) { if (err.status == 403) { this._authenticationService.logout(); } } removeMeta(obj : any,key) { for(let prop in obj) { if (prop === key) delete obj[prop]; else if (typeof obj[prop] === 'object') this.removeMeta(obj[prop],key); } } }