import { Component, OnInit, OnDestroy } from '@angular/core'; import { Subject } from 'rxjs/Subject'; import { RootService } from '../../../_services/root.service'; import { Subscription } from 'rxjs/Subscription'; import { debounceTime } from 'rxjs/operators/debounceTime'; import { StockInViewModel, ItemViewModel } from '../MedStock.class'; import { StockService } from '../stock.service'; import { NotificationService } from '../../../_services/notification.service'; import { AuthenticationService } from '../../../_services/authentication.service'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-stock-in', templateUrl: './stock-in.component.html' }) export class StockInComponent implements OnInit, OnDestroy { public currentUser: any; public MedList: any[] = []; public SourceList: any[] = []; public HFList: any[] = []; public stockin: StockInViewModel = new StockInViewModel(); public stockList: StockInViewModel[] = []; public UomList: any[] = []; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public maxDate = new Date(2000, 1, 1); public nowDate = new Date(); public dateNow: string = ''; public saving = false; public posting = false; public HFL = false; constructor(private _authenticationService: AuthenticationService, private _rootService: RootService, private _stockService: StockService, public _notificationService: NotificationService, private _localService: LocalService) { } ngOnInit() { //this.fetchMedicineData("1"); this.currentUser = this._authenticationService.getUser(); if (this.currentUser != null) { if (this.currentUser.RoleName == "CEO") { this.HFL = true; } } this.handleSearchEvents(); this.fetchUomData(); this.fetchSourceData(); this.stockin.MedicineId = new ItemViewModel(); this.stockin.HFId = new ItemViewModel(); } public dropdownValueChanged = (value, filter) => { if (!value) { return; } if (filter == 'MedicineId') { this.stockin.MedicineId.Id = this.MedList.find(x => x.Name == value).Id; } if (filter == 'HFId') { this.stockin.HFId.Id = this.HFList.find(x => x.Name == value).Id; } } onSubmit() { debugger; if (!this.stockin.MedicineId.Id) { this._notificationService.notify("danger", "Select Medicine from the list"); } else { this.saving = true; this.removeMeta(this.stockin, '$id'); this._stockService.saveStock(this.stockin).subscribe( (res: any) => { this.saving = false; this.stockin = new StockInViewModel(); this.stockin.MedicineId = new ItemViewModel(); this.stockin.HFId = new ItemViewModel(); this._notificationService.notify("success", "Stock Post Successfully"); }, err => { this.posting = false; this.handleError(err); } ); this.saving = false; } } 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); } } PostStock() { this.posting = true; debugger; this.removeMeta(this.stockList, '$id'); this._stockService.saveStock(this.stockin).subscribe( (res: any) => { this.posting = false; this.stockList = []; this._notificationService.notify("success", "Stock Post Successfully"); }, err => { this.posting = false; this.handleError(err); } ); } RemoveFromList(index) { this.stockList.splice(index, 1); } fetchMedicineData(search: string) { if (search == "") search = "1"; this._rootService.getMedList(search).subscribe( (res: any) => { if (res) { this.MedList = res; } }, err => { this.handleError(err); } ); } fetchUomData() { this._rootService.getUomList().subscribe( (res: any) => { if (res) { this.UomList = res; } }, err => { this.handleError(err); } ); } fetchSourceData() { this._rootService.getSourceList().subscribe( (res: any) => { if (res) { this.SourceList = res; } }, err => { this.handleError(err); } ); } getRHCBHUs(search: string) { if (search == "") search = "1"; this._rootService.getBhuRhcList(search).subscribe( (res: any) => { if (res) { this.HFList = res; } }, err => { } ); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { //console.log(x); if (x) { if (x.filter == "med") { this.fetchMedicineData(x.event); } if (x.filter == "hf") { this.getRHCBHUs(x.event); } } }); } ngOnDestroy(): void { this.searchSubcription.unsubscribe(); } public search(value: string, filter: string) { debugger; } private handleError(err: any) { if (err.status == 403) { this._authenticationService.logout(); } } }