import { Component, OnInit, ViewChild } from '@angular/core'; import { NotificationService } from '../../../_services/notification.service'; import { StoreService } from '../store.services'; import { ItemDefiningViewModel, ItemViewModel, CatlogViewModel } from '../store-model'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; 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-items-defining', templateUrl: './items-defining.component.html', }) export class ItemsDefiningComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public itemDefining = new ItemDefiningViewModel(); public ItemCodeValid: any; public itemCategoriesList: any[] = []; public brandsList: any[] = []; public colorList: any[] = []; public cityList: any[] = []; public itemNameList: any[] = []; public modelYearList: any[] = []; public engPower: any[] = []; public garageList: any[] = []; public ramlist: any[] = []; public internalMamList: any[] = []; public sizeList: any[] = []; public insuredByList: any[] = []; public planBanfitsList: any[] = []; public accountList: any[]; public cusaccountList: any[]; public supAccount: any[]; public catlog = new CatlogViewModel(); public catCodeValid: any; public catlogCodeValid: any; public categoriesList: any[] = []; public catlogList: any[] = []; public openDilogConfirmation = false; public openDilog = false; public openDilogCat = false; public billAccount: any[]; public noewDate = new Date(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public showdublicate = false; public saving = false; constructor(private activatedRoute: ActivatedRoute, public _notificationService: NotificationService, public _storeService: StoreService, public _localService: LocalService) { super(); } ngOnInit() { this.itemDefining.FK_Associat_Account = new ItemViewModel(); this.itemDefining.FK_Associat_Coustomer = new ItemViewModel(); this.itemDefining.FK_Billing_Account = new ItemViewModel(); this.itemDefining.Status = true; this.itemDefining.Item_Type = 'Product'; this.noewDate = new Date(); this.noewDate.setHours(this.noewDate.getHours() + 5); this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this.getItem(accId); this.gatMataDataForEdit(); } else { this.itemDefining.Exp_Date = new Date(); this.validateItemCode('-1'); } }); this.getItemCategory(); this.getCategory(); this.handleSearchEvents(); } public gatMataDataForEdit() { this.getCatlogs('Item Name', ''); this.getCatlogs('Brand', ''); this.getCatlogs('Color', ''); this.getCatlogs('City', ''); this.getCatlogs('Measure', ''); this.getCatlogs('Item Name', ''); this.getCatlogs('Model_Year', ''); this.getCatlogs('Bike_Engg_Power', ''); this.getCatlogs('Bike_Garage', ''); this.getCatlogs('Mobile_Ram', ''); this.getCatlogs('Mobile_InternalMemory', ''); this.getCatlogs('Size', ''); this.getCatlogs('Bike_Insured_By', ''); this.getCatlogs('Sim_Plan_Banifits', ''); this.getAccount('', 'Exp'); this.getAccount('', 'CCS'); this.getAccount('', 'S'); } public getItem(id: number) { this._storeService.getItemById(id).subscribe((res: any) => { if (res) { console.log(res); this.itemDefining = res.item; this.itemDefining.Exp_Date = new Date(res.item.Exp_Date); this.itemDefining.Contract_Expire_Date = res.item.Contract_Expire_Date !== null ? new Date(res.item.Contract_Expire_Date) : null; this.ItemCodeValid = true; } }, err => { }); } public onSubmit() { if (this.itemDefining.FK_ItemCategoryId.Id == 0) { this._notificationService.notify('danger', 'Select Item Type'); return; } this.saving = true; let emp: any = Object.assign({}, this.itemDefining); if (this.itemDefining.Exp_Date) emp.Exp_Date = this.itemDefining.Exp_Date.toDateString(); this._storeService.saveItemDefining(emp).subscribe((res: any) => { this._notificationService.notify('success', 'Save Successfully'); this.itemDefining.Code = ''; this.showdublicate = true; this.saving = false; this.itemDefining.FK_Associat_Account = new ItemViewModel(); this.itemDefining.FK_Associat_Coustomer = new ItemViewModel(); this.validateItemCode('-1'); }, err => { this._notificationService.notify('danger', err); }); } public ClearForm() { this.itemDefining = new ItemDefiningViewModel(); this.showdublicate = false; } public validateItemCode(x) { debugger; this._storeService.validateItemCode(x).subscribe((res: any) => { if (res.msg == 'Valid') { this.ItemCodeValid = true; this.itemDefining.Code = res.code; } else { this.ItemCodeValid = false; } }, err => { this._notificationService.notify('danger', err); }); } public getItemCategory() { this._storeService.getItemCategory().subscribe((res: any) => { if (res) { this.itemCategoriesList = res; } }, err => { this._notificationService.notify('danger', err.msg); }); } public getCatlogs(x: string, event: string) { this.brandsList = []; this._storeService.getFilterdCatlogs(x, event).subscribe((res: any) => { if (res) { if (x == 'Brand') this.brandsList = res; if (x == 'Color') this.colorList = res; if (x == 'City') this.cityList = res; if (x == 'Item Name') this.itemNameList = res; if (x == 'Model_Year') this.modelYearList = res; if (x == 'Bike_Engg_Power') this.engPower = res; if (x == 'Bike_Garage') this.garageList = res; if (x == 'Mobile_Ram') this.ramlist = res; if (x == 'Mobile_InternalMemory') this.internalMamList = res; if (x == 'Size') this.sizeList = res; if (x == 'Bike_Insured_By') this.insuredByList = res; if (x == 'Sim_Plan_Banifits') this.planBanfitsList = res; } }, err => { this._notificationService.notify('danger', err.msg); }); } public getAccount(x: string, event: string) { debugger; this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event == 'Exp') this.accountList = res; if (event == 'CCS') this.cusaccountList = res; if (event == 'S') this.supAccount = res; } }, err => { this._notificationService.notify('danger', err.msg); }); } public dropdownValueChanged = (value, filter) => { if (!value) { return; } if (filter == 'account') { this.itemDefining.FK_Associat_Account.Id = this.accountList.find(x => x.Name == value).Id; } if (filter == 'cutomer') { this.itemDefining.FK_Associat_Coustomer.Id = this.cusaccountList.find(x => x.Name == value).Id; } } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); this.brandsList = []; if (x) { debugger; if (x.event.length > 1) x.event = x.event.replace(/\s/g, ''); if (x.filter == 'code') { this.validateItemCode(x.event); } if (x.filter == 'Brand') { this.getCatlogs('Brand', x.event); } if (x.filter == 'Color') { this.getCatlogs('Color', x.event); } if (x.filter == 'City') { this.getCatlogs('City', x.event); } if (x.filter == 'Measure') { this.getCatlogs('Measure', x.event); } if (x.filter == 'Item Name') { this.getCatlogs('Item Name', x.event); } if (x.filter == 'Model_Year') { this.getCatlogs('Model_Year', x.event); } if (x.filter == 'Bike_Engg_Power') { this.getCatlogs('Bike_Engg_Power', x.event); } if (x.filter == 'Bike_Garage') { this.getCatlogs('Bike_Garage', x.event); } if (x.filter == 'Mobile_Ram') { this.getCatlogs('Mobile_Ram', x.event); } if (x.filter == 'Mobile_InternalMemory') { this.getCatlogs('Mobile_InternalMemory', x.event); } if (x.filter == 'Size') { this.getCatlogs('Size', x.event); } if (x.filter == 'Bike_Insured_By') { this.getCatlogs('Bike_Insured_By', x.event); } if (x.filter == 'Sim_Plan_Banifits') { this.getCatlogs('Sim_Plan_Banifits', x.event); } if (x.filter == 'Contract_Expire_Date') { this.getCatlogs('Contract_Expire_Date', x.event); } if (x.filter == 'account') { this.getAccount(x.event, 'Exp'); } if (x.filter == 'cutomer') { this.getAccount(x.event, 'CCS'); } if (x.filter == 'FK_Billing_Account') { this.getAccount(x.event, 'S'); } } }); } handleFilter(value) { //this.data = this.source.filter((s) => s.text.toLowerCase().indexOf(value.toLowerCase()) !== -1); this.getCatlogs('Item Name', value); } // cata loggg-------- public validateCatCode(x) { this._storeService.validateCatCode(x).subscribe((res: any) => { debugger; if (res.msg == "Valid") { this.catCodeValid = true; } else { this.catCodeValid = false; } }, err => { this._notificationService.notify("danger", err); }); } public getCategory() { this._storeService.getCategory().subscribe((res: any) => { if (res) { this.categoriesList = res; } }, err => { this._notificationService.notify("danger", err.msg); }); } public OpenDileogForCatlog() { this.catlog = new CatlogViewModel(); this.catlog.FK_CatrgoryID = new ItemViewModel(); if (this.catlog.Code == null) { this._storeService.validateCatlogCode('-1').subscribe((res: any) => { debugger; if (res.msg == "Valid") { this.catlogCodeValid = true; this.catlog.Code = res.code; } else { this.catlogCodeValid = false; } }, err => { this._notificationService.notify("danger", err); }); } this.openDilogCat = true; } public closeCat(status) { this.catlog = new CatlogViewModel(); this.catlog.FK_CatrgoryID = new ItemViewModel(); this.catlog.FK_CatrgoryID.Id = null; this.catlog.FK_CatrgoryID.Name = "Select Type"; this.openDilogCat = false; this.openDilogConfirmation = false; } public saveCatlog() { if (!this.catlogCodeValid && !this.catlog.Id) { this._notificationService.notify("danger", "Dublicate Code Not Allowed"); return; } this._storeService.saveCatlog(this.catlog).subscribe((res: any) => { this._notificationService.notify("success", "Catalog Save Successfully"); this.catlog = new CatlogViewModel(); this.catlog.FK_CatrgoryID = new ItemViewModel(); this.catlog.FK_CatrgoryID.Id = null; this.catlog.FK_CatrgoryID.Name = "Select Type"; this.openDilogCat = false; }, err => { this._notificationService.notify("danger", err.msg); }); } }