import { Component, OnInit, ViewChild } from '@angular/core'; import { CoustomerContrectViewMode, ContrectAccessoriesViewModel } from '../coustomer-class'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { StoreService } from '../../store/store.services'; import { NotificationService } from '../../../_services/notification.service'; import { ActivatedRoute, Router } from '@angular/router'; import { CoustomerServices } from '../coustome-services'; import { LocalService } from '../../../_services/local.service'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; import { NgForm } from '@angular/forms'; @Component({ selector: 'app-contract', templateUrl: './contract.component.html', }) export class ContractComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public contrect: CoustomerContrectViewMode; public accessaories = new ContrectAccessoriesViewModel(); public accountList: any[] = []; public itemCategoriesList: any[] = []; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public saving = false; constructor(public _storeService: StoreService, public _notificationService: NotificationService, private activatedRoute: ActivatedRoute, private router: Router, private _coustomerServices: CoustomerServices, public _localService: LocalService) { super(); } ngOnInit() { this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this.getAccount(' ', 'CCS'); this.getContrectDetail(accId); } else { this.contrect = new CoustomerContrectViewMode(); this.contrect.Status = true; this.contrect.ContrectDate = new Date(); this.contrect.Accessories = []; this.contrect.ContractType = 'Customer'; } }); this.handleSearchEvents(); this.getItemCategory(); } public getContrectDetail(id: number) { this._coustomerServices.getContretcById(id).subscribe((res: any) => { if (res) { this.contrect = res.con; this.contrect.ContrectDate = new Date(res.con.ContrectDate) this.contrect.StartDate = new Date(res.con.StartDate) this.contrect.EndDate = new Date(res.con.EndDate) } }, err => { }); } public onSubmit() { this.saving = true; let con: any = Object.assign({}, this.contrect); if (this.contrect.StartDate) con.StartDate = this.contrect.StartDate.toDateString(); if (this.contrect.EndDate) con.EndDate = this.contrect.EndDate.toDateString(); this._coustomerServices.saveContrct(con).subscribe((res: any) => { this._notificationService.notify('success', 'Save Successfully!'); this.contrect = new CoustomerContrectViewMode(); this.contrect.Status = true; this.contrect.ContrectDate = new Date(); this.contrect.Accessories = []; this.saving = false; }, err => { this._notificationService.notify('danger', err.error); }); } public ClearForm() { } public getAccount(x: string, event: string) { debugger; this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event == 'CCS') this.accountList = res; } }, err => { this._notificationService.notify('danger', err.msg); }); } public getItemCategory() { this._storeService.getItemCategory().subscribe((res: any) => { if (res) { this.itemCategoriesList = res; } }, err => { this._notificationService.notify('danger', err.msg); }); } public AddToCart() { this.contrect.Accessories.push(this.accessaories); this.accessaories = new ContrectAccessoriesViewModel(); } public RemoveFromCart(e) { this.contrect.Accessories.splice(e, 1); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { debugger; if (x.filter == 'Customer') { this.getAccount(x.event, 'CCS'); } } }); } }