import { Component, Injector, ViewChild, ViewEncapsulation, Input, Output, EventEmitter, OnInit, AfterViewInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppConsts } from '@shared/AppConsts'; import { AppComponentBase } from '@shared/common/app-component-base'; import { LocationListDto, LocationServiceProxy, PostalCodeZoneServiceProxy, CreatePostalCodeInput, CreateOrEditPostalCodeZoneDto, CreateZoneInput, PostalCodeZoneListDto, ZoneServiceProxy, ZoneListDto, PostalCodeServiceProxy } from '@shared/service-proxies/service-proxies'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { FormGroup, FormControl } from '@angular/forms'; import { finalize } from 'rxjs/operators'; import * as moment from 'moment'; import * as _ from 'lodash'; import { Location } from '@angular/common'; declare var KTWizard: any; declare var $: any; declare var KTApp: any; declare let swal: any; @Component({ templateUrl: './postal-code-zones.component.html', encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()], }) export class PostalCodeZonesComponent extends AppComponentBase { newPostalCodeZone = false; active = false; saving = false; id: any; i: any; postalCodeZones: any; zones: any; postalCodes: any; dynamicArray: Array = []; newDynamic: any = {}; locationName: string; locationTimeZone: string; zone: Array = []; //CreateZoneInput = new CreateZoneInput(); postalCode: Array = []; //CreatePostalCodeInput = new CreatePostalCodeInput(); zoneContainer: Array = []; postalCodeContainer: Array = []; postalCodeZoneInput: CreateOrEditPostalCodeZoneDto = new CreateOrEditPostalCodeZoneDto; constructor( injector: Injector, private _postalCodeZoneServiceProxy: PostalCodeZoneServiceProxy, private _locationServiceProxy: LocationServiceProxy, private _postalCodeServiceProxy: PostalCodeServiceProxy, private _zoneServiceProxy: ZoneServiceProxy, private _router: Router, private route: ActivatedRoute, private _location: Location, ) { super(injector); } ngOnInit() { this.route.paramMap.subscribe(params => { this.id = params.get("id") }); this.spinnerService.show(); this._locationServiceProxy.getLocation( this.id, undefined, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.locationName = result.items[0]['name']; this.locationTimeZone = result.items[0]['timeZone']['value']; this._postalCodeZoneServiceProxy.getPostalCodeZoneList(this.id).pipe(finalize(() => this.spinnerService.hide())).subscribe(result => { this.postalCodeZones = result.items; this.spinnerService.hide(); console.log(this.postalCodeZones); }); }); this._zoneServiceProxy.getAllZone().subscribe(result => { this.zones = result.items; //console.log(this.zones); }); this._postalCodeServiceProxy.getAllPostalCode().subscribe(result => { this.postalCodes = result.items; //console.log(this.postalCodes); }); } save(): void { var that = this; this.newPostalCodeZone = true; this.dynamicArray.forEach(function (value, i) { let newZone = new CreateZoneInput({ name: value.name }); that.zoneContainer.push(newZone); let newPostalCode = new CreatePostalCodeInput({ value: value.value, city: value.city, state: value.state, country: value.country }); that.postalCodeContainer.push(newPostalCode); }); //that.zone.push(that.zoneContainer); //that.postalCode.push(that.postalCodeContainer); let postalCodeZoneInput = new CreateOrEditPostalCodeZoneDto({ newPostalCodeZone: this.newPostalCodeZone, locationId: that.id, createPostalCodeInput: that.postalCodeContainer, createZoneInput: that.zoneContainer }); that.zoneContainer = []; that.postalCodeContainer = []; that.dynamicArray = []; //console.log(postalCodeZoneInput); that._postalCodeZoneServiceProxy.createOrEditPostalCodeZone(postalCodeZoneInput) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.saving = true; this.notify.info(this.l('SavedSuccessfully')); this.reloadPage(); }); } addRow(index) { let that = this; this.newDynamic = { name: "", value: "", city: "", state: "", country: "" }; this.dynamicArray.push(this.newDynamic); return true; } onChange(postalCodeId, index) { this._postalCodeServiceProxy.getPostalCodeEdit(postalCodeId).subscribe(result => { //this.postalCodes = result.items; this.dynamicArray[index]['city'] = result.city; this.dynamicArray[index]['state'] = result.state; this.dynamicArray[index]['country'] = result.country; }); } deleteRow(index) { this.dynamicArray.splice(index, 1); return true; } handleChange(input) { if (input.value < 0) input.value = 0; if (input.value > 100) input.value = 2; } reloadPage(): void { this.ngOnInit(); } deletePostalCodeZone(postalCodeZonesId): void { this.message.confirm( '', (isConfirmed) => { if (isConfirmed) { this._postalCodeZoneServiceProxy.deletePostalCodeZone(postalCodeZonesId) .subscribe(() => { this.reloadPage(); this.notify.success(this.l('SuccessfullyDeleted')); }); } } ); } createOrEdit(url: any, id?: number) { this.message.confirm( '', (isConfirmed) => { if (isConfirmed) { if (id != null) { var myurl = `${url}/${id}`; } else { var myurl = `${url}`; } console.log(myurl); this._router.navigateByUrl(myurl); } } ); } goBack() { this._location.back(); } }