import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { AddressesServiceProxy, CreateOrEditAddressDto, UpdateAddressInput, PostalCodeServiceProxy, CreatePostalCodeInput } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { AddressFormComponent } from '@app/shared/layout/form/address-form.component'; @Component({ selector: 'createOrEditAddressModal', templateUrl: './create-or-edit-address-modal.component.html' }) export class CreateOrEditAddressModalComponent extends AppComponentBase { @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @ViewChild(AddressFormComponent, {static:false}) addressForm; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; address: CreateOrEditAddressDto = new CreateOrEditAddressDto(); postalCodeInput: CreatePostalCodeInput = new CreatePostalCodeInput(); address2: UpdateAddressInput = new UpdateAddressInput(); postalCodeValue = ''; geocodeLatitude = ''; geocodeLongitude = ''; addressTypeName : any; postalCodeId : number; geocodeId : number; postalCodeCountry : string; stateFeature: boolean constructor( injector: Injector, private _addressesServiceProxy: AddressesServiceProxy, private _postalCodeServiceProxy: PostalCodeServiceProxy ) { super(injector); } ngOnInit(){ // $('.kt-select2').select2(); this.stateFeature = abp.features.isEnabled('App.StateFeature'); } onShown(){ $('.kt-select2').select2(); } show(addressId?: number): void { // $('.kt-select2').select2(); if (!addressId) { this.address = new CreateOrEditAddressDto(); this.address.id = addressId; this.postalCodeValue = ''; this.geocodeLatitude = ''; this.geocodeLongitude = ''; this.addressTypeName = ''; this.address.addressTypeId = null; this.active = true; this.modal.show(); } else { console.log(addressId) this._addressesServiceProxy.getAddressForEdit(addressId).subscribe(result => { this.address = result.address; this.address.latitude = result.geocodeLatitude; this.address.longitude = result.geocodeLongitude; this.postalCodeId = this.address.postalCodeId; this.postalCodeValue = result.postalCodeValue; this.geocodeId = result.geocodeId // this.geocodeLatitude = result.geocodeLatitude; // this.geocodeLongitude = result.geocodeLongitude; this.addressTypeName = result.addressTypeName; this._postalCodeServiceProxy.getPostalCodeEdit(this.postalCodeId) .pipe() .subscribe(result => { this.addressForm.city = result.city this.addressForm.state = result.state this.addressForm.value = result.value this.addressForm.country = result.country }) // this._postalCodeServiceProxy.getAllPostalCodeAutoComplete(undefined,result.address.postalCodeId) // .pipe() // .subscribe(result => { // this.addressForm.postalComplete = result.items // }) this.active = true; this.modal.show(); }); } } save(): void { // this.saving = true; this.address = this.addressForm.address this.postalCodeInput.city = this.addressForm.city; this.postalCodeInput.state = this.addressForm.state; this.postalCodeInput.value = this.addressForm.value; this.postalCodeInput.country = this.addressForm.country; this.address.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value); this.address.latitude = Number((document.getElementById('Address_GeocodeLatitude')).value); this.address.longitude = Number((document.getElementById('Address_GeocodeLongitude')).value); console.log(this.address.postalCodeId) console.log(this.postalCodeInput) console.log(this.postalCodeInput.state) this._postalCodeServiceProxy.createPostalCode(this.postalCodeInput) .pipe() .subscribe(result => { this.address.postalCodeId = result this._addressesServiceProxy.createOrEdit(this.address) .pipe(finalize(() => { this.saving = false;})) .subscribe(( ) => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); }); } setPostalCodeIdNull() { this.address.postalCodeId = null; this.postalCodeValue = ''; } setGeocodeIdNull() { this.address.geocodeId = null; this.geocodeLatitude = ''; } setAddressTypeIdNull() { this.address.addressTypeId = null; this.addressTypeName = ''; } close(): void { this.active = false; this.modal.hide(); } }