import { Component, OnInit } from '@angular/core'; import { Subject, Subscription } from 'rxjs'; import { StockTransferViewModel, 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-transfer', templateUrl: './transfer.component.html', styles: [] }) export class TransferComponent implements OnInit { public MedList: any[] = []; public BatchList: any[] = []; public stock: StockTransferViewModel = new StockTransferViewModel(); public DepList: any[] = []; public saving = false; public Division: any[] = []; public SelectedDivision: any; public Districts: any[] = []; public SelectedDistricts: any; public Tehsils: any[] = []; public SelectedTehsils: any; public SelectedFacility: any; public HFType: any[] = []; public SelectedHFType: any; public HFList: any[] = []; 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.stock.MedicineId = new ItemViewModel(); this.loadDivision(); this.loadHFTypes(); } public loadDivision() { this.Division = []; this._rootService.getDivisions(this._authenticationService.getUserHfmisCode()) .subscribe((x: any) => { if (x) { this.Division = x; } }); } public loadDistrict(divCode: string) { debugger this.Districts = []; this._rootService.getDistrictsOnChang(divCode) .subscribe((x: any) => { if (x) { this.Districts = x; } }); } public loadTehsils(disCode: string) { this.Tehsils = []; this._rootService.getTehsilsOnChang(disCode) .subscribe((x: any) => { if (x) { this.Tehsils = x; } }); } public loadHFTypes() { this.HFType = []; this._rootService.getHFTypes() .subscribe((x: any) => { if (x) { this.HFType = x; } }); } getHfList(geoLvl: string, hfTypeCode: string) { this._rootService.getHfList(geoLvl, hfTypeCode).subscribe( (res: any) => { if (res) { this.HFList = res; } }, err => { } ); } public dropdownValueChanged = (value, filter) => { console.log(value); if (!value) { return; } if (filter == 'Div') { this.HFList = []; this.loadDistrict(value.Code); this.SelectedDistricts = null; this.SelectedTehsils = null; this.SelectedFacility = null; } if (filter == 'Dis') { this.HFList = []; this.loadTehsils(value.Code); this.SelectedTehsils = null; this.SelectedFacility = null; } if (filter == 'teh') { this.HFList = []; this.getHfList(value.Code, '0'); this.SelectedFacility = null; this.SelectedHFType = null; } if (filter == 'hft') { this.getHfList(this.SelectedTehsils, value.Code); this.SelectedFacility = null; } if (filter == 'hf') { } 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.stock.HealthFacilityTo = new ItemViewModel(); this.stock.HealthFacilityTo.Id = this.SelectedFacility.Id; this.stock.HealthFacilityTo.Name = this.SelectedFacility.FullName; this.saving = true; this._stockService.saveTransferStock(this.stock).subscribe( (res: any) => { if (res == "success") { this._notificationService.notify("success", "Transfer Successfully.") this.stock = new StockTransferViewModel(); this.stock.HealthFacilityTo = new ItemViewModel(); 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(); } } }