import { Component, OnInit, ViewChild } from '@angular/core'; import { NotificationService } from '../../../_services/notification.service'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { DefineAccountViewModel } from '../accounts.class'; import { AccountService } from '../accounts-services'; import { StoreService } from '../../store/store.services'; import { ActivatedRoute } from '@angular/router'; import { LocalService } from '../../../_services/local.service'; import { NgForm } from '@angular/forms'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; @Component({ selector: 'app-define-account', templateUrl: './define-account.component.html', }) export class DefineAccountComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public defineAccount = new DefineAccountViewModel(); public AccCodeValid: any; public saving = false; public preHeadsList: any[]; public heads1List: any[]; public heads2List: any[]; public accNatuersList: any[]; public catlogList: any[]; constructor(private activatedRoute: ActivatedRoute, public _notificationService: NotificationService, public _accountService: AccountService, public _storeService: StoreService, private _localService: LocalService) { super(); } ngOnInit() { this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this.getAccount(accId); } else { this.validateAccCode('-1'); } }); this.getPreHeads(); this.getAccNatures(); this.handleSearchEvents(); this.defineAccount.Status = true; this.defineAccount.Balance = 0; } public getAccount(id: number) { this._accountService.getAccountById(id).subscribe((res: any) => { if (res) { this.defineAccount = res.acc; this.AccCodeValid = true; } }, err => { }); } public onSubmit() { this.saving = true; this.removeMeta(this.defineAccount, '$id'); this._accountService.saveAccount(this.defineAccount).subscribe((res: any) => { this._notificationService.notify('success', 'Account Save Successfully'); this.saving = false; this.defineAccount = new DefineAccountViewModel(); this.validateAccCode('-1'); }, err => { this._notificationService.notify('danger', err.error); this.saving = false; }); } public ClearForm() { this.defineAccount = new DefineAccountViewModel(); this.defineAccount.Status = true; this.defineAccount.Balance = 0; this.validateAccCode('-1'); } public getPreHeads() { this._accountService.getPreHeads().subscribe((res: any) => { if (res) { this.preHeadsList = res; } }, err => { }); } public getHead1(e: number) { this._accountService.getHead1(e).subscribe((res: any) => { if (res) { this.heads1List = res; } }, err => { }); } public getHead2(e: number) { this._accountService.getHead2(e).subscribe((res: any) => { if (res) { this.heads2List = res; } }, err => { }); } public getAccNatures() { this._accountService.getAccNatuers().subscribe((res: any) => { if (res) { this.accNatuersList = res; } }, err => { }); } public dropDownChange(e) { console.log(e); if (e.filter == 'ph') { this.getHead1(e.event.Id); } if (e.filter == 'h1') { this.getHead2(e.event.Id); } } public getCatlogs(x: string, event: string) { this._storeService.getFilterdCatlogs(x, event).subscribe((res: any) => { if (res) { this.catlogList = res; } }, err => { this._notificationService.notify('danger', err.msg); }); } public validateAccCode(x) { debugger; this._accountService.validateAccCode(x).subscribe((res: any) => { if (res.msg == 'Valid') { this.AccCodeValid = true; this.defineAccount.Acc_Code = res.code; } else { this.AccCodeValid = false; } }, err => { this._notificationService.notify('danger', err); }); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); debugger; if (x) { if (x.filter == 'City') { this.getCatlogs('City', x.event); } if (x.filter == 'code') { this.validateAccCode(x.event); } } }); } 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); } } }