import { Component, OnInit, ViewChild } from '@angular/core'; import { VehicleOnRentViewModel, ItemViewModel } from '../store-model'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { StoreService } from '../store.services'; import { NotificationService } from '../../../_services/notification.service'; import { LocalService } from '../../../_services/local.service'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; import { NgForm } from '@angular/forms'; @Component({ selector: 'app-vehicle-rent', templateUrl: './vehicle-rent.component.html', }) export class VehicleRentComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public vehicleOnRent = new VehicleOnRentViewModel(); public saving = false; public nowDate = new Date(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public accountList = []; public itemList = []; public recevingPrintList: any[] = []; constructor(public _storeService: StoreService, public _notificationService: NotificationService, private _localService: LocalService) { super(); } ngOnInit() { this.handleSearchEvents(); this.vehicleOnRent.FK_Suplier_Acc = new ItemViewModel(); } public onSubmit() { this.saving = true; let emp: any = Object.assign({}, this.vehicleOnRent); emp.ReciveDate = this.vehicleOnRent.ReciveDate.toDateString(); this.pushtoPrintList(emp); this._storeService.saveRentedVechel(emp).subscribe((res: any) => { this._notificationService.notify("success", "Receiving Save Successfully"); this.saving = false; this.vehicleOnRent = new VehicleOnRentViewModel(); this.vehicleOnRent.FK_Suplier_Acc = new ItemViewModel(); }, err => { this._notificationService.notify("danger", err.error); //this.InitialzedProp(); this.saving = false; }); } public pushtoPrintList(e) { this.recevingPrintList.push(e); } public getAccount(x: string, event: string) { debugger; this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { this.accountList = res } }, err => { this._notificationService.notify("danger", err.msg); }); } public getItem(x: string, y: string) { this._storeService.getVehcielForRent(x, y).subscribe((res: any) => { if (res) { this.itemList = res } }, err => { this._notificationService.notify("danger", err.msg); }); } public dropdownValueChanged = (value, filter) => { if (!value) { return; } if (filter == 'FK_Suplier_Acc') { this.vehicleOnRent.FK_Suplier_Acc.Id = this.accountList.find(x => x.Name == value).Id; } if (filter == "itemCode") { debugger; this.vehicleOnRent.Item_Code = this.itemList.find(x => x.Code == value).Code; this.vehicleOnRent.Item_Name = this.itemList.find(x => x.Code == value).Name; this.vehicleOnRent.Fk_Item_Id = this.itemList.find(x => x.Code == value).Id; } } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { debugger; if (x.filter == "FK_Suplier_Acc") { this.getAccount(x.event, "SCS"); } if (x.filter == "itemCode") { this.getItem(x.event, "V"); if (x.event == "") { this.vehicleOnRent.Item_Name = null; } } } }); } }