import { Component, ViewChild, Injector, Output, Input, EventEmitter, OnInit, AfterViewInit} from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { RouteStopsServiceProxy, CreateOrEditRouteStopDto, CreatePostalCodeInput } from '@shared/service-proxies/service-proxies'; import { AddressServiceProxy, CreateAddressInput } from '@shared/service-proxies/service-proxies'; import { EmailServiceProxy, CreateEmailInput, AddressTypeServiceProxy, PostalCodeServiceProxy, } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { RouteStopContactLookupTableModalComponent } from './routeStop-contact-lookup-table-modal.component'; import { AddressFormComponent } from '@app/shared/layout/form/address-form.component'; @Component({ selector: 'createOrEditRouteStopModal', templateUrl: './create-or-edit-routeStop-modal.component.html' }) export class CreateOrEditRouteStopModalComponent extends AppComponentBase { @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @ViewChild('routeStopContactLookupTableModal', { static: true }) routeStopContactLookupTableModal: RouteStopContactLookupTableModalComponent; @ViewChild(AddressFormComponent, {static:false}) addressForm; @Output() modalSave: EventEmitter = new EventEmitter(); @Input('selectedCountry') selectedCountry: string; @Input('selectedCity') selectedCity: string; @Input('selectedState') selectedState: string; @Input('postalCodeId') selectedPostalCodeId: number; @Input('postalCodeValue') selectedPostalCodeValue: string; active = false; saving = false; routeStop: CreateOrEditRouteStopDto = new CreateOrEditRouteStopDto(); address: CreateAddressInput = new CreateAddressInput(); postalCodeInput: CreatePostalCodeInput = new CreatePostalCodeInput(); contactAddress = ''; constructor( injector: Injector, private _addressService: AddressServiceProxy, private _emailService: EmailServiceProxy, // private _postalCodeService: PostalCodeServiceProxy, private _postalCodeServiceProxy: PostalCodeServiceProxy, private _routeStopsServiceProxy: RouteStopsServiceProxy ) { super(injector); } filteredCountries: any; filteredCities: any; filteredStates: any; filteredPostal: any; filteredAddressType: any; selectedPostalCode: string; selectedPostalCodeA: string; selectedAddressType: any; postalCodeValue: any; postalCodeId:number; filterChange = false; stateFeature: boolean; ngOnInit(){ this.stateFeature = abp.features.isEnabled('App.StateFeature'); } onShown(){ $('.kt-select2').select2(); } show(routeStopId?: number): void { // this.getAllDropDown(); this.spinnerService.show(); if (!routeStopId) { this.routeStop = new CreateOrEditRouteStopDto(); this.routeStop.id = routeStopId; this.address.addressLine1=''; this.address.addressLine2=''; //this.contactAddress = ''; this.active = true; this.modal.show(); this.spinnerService.hide(); } else { this._routeStopsServiceProxy.getRouteStopForEdit(routeStopId).subscribe(result => { this.routeStop = result.routeStop; this.address = this.routeStop.address; this.postalCodeId = result.routeStop.address.postalCodeId // this.selectedPostalCodeId = this.address.postalCodeId; 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.postalCodeValue = result.country this.spinnerService.hide(); }); this.active = true; this.modal.show(); //this.contactAddress = result.contactAddress; // this._postalCodeService.getPostalCodeEdit(this.selectedPostalCodeId).subscribe(resultPostalCode => { // this.selectedCountry = resultPostalCode.country; // this.selectedState = resultPostalCode.state; // this.selectedCity = resultPostalCode.city; // this.selectedPostalCode = resultPostalCode.value; // console.log(this.selectedPostalCode); // this.filterCountries(this.selectedPostalCode) // this.filterStates(this.selectedPostalCode) // this.filterCities(this.selectedPostalCode) // this.spinnerService.hide(); // }); }); } } save(): void { this.saving = true; // this.address.postalCodeId = Number((document.getElementById('postalSelectInput')).value); this.address = this.addressForm.address this.address.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value); this.routeStop.address = this.address; this.postalCodeInput.city = this.addressForm.city // this.postalCodeInput.state = this.addressForm.state if(!this.stateFeature){ this.postalCodeInput.state = ""; } else{ this.postalCodeInput.state = this.addressForm.state; } this.postalCodeInput.country = this.addressForm.country this.postalCodeInput.value = this.addressForm.value console.log(this.routeStop); this._postalCodeServiceProxy.createPostalCode(this.postalCodeInput) .pipe() .subscribe(result => { this.address.postalCodeId = result this._routeStopsServiceProxy.createOrEdit(this.routeStop) .pipe(finalize(() => { this.saving = false;})) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); }) } //openSelectContactModal() { //this.routeStopContactLookupTableModal.id = this.routeStop.contactId; //this.routeStopContactLookupTableModal.displayName = this.contactAddress; //this.routeStopContactLookupTableModal.show(); //} //setContactIdNull() { //this.routeStop.contactId = null; //this.contactAddress = ''; //} //getNewContactId() { //this.routeStop.contactId = this.routeStopContactLookupTableModal.id; //this.contactAddress = this.routeStopContactLookupTableModal.displayName; //} getAllDropDown() { this.clearFields(); let maxcount = 1000; let that = this; // this.filterPostal(); // jQuery(document).ready(function () { // $("#postalSelectInput").change(function () { // that.selectedPostalCodeA = $("#postalSelectInput option:selected").text(); // that.selectedPostalCode = $.trim(that.selectedPostalCodeA) // that.filterChange = true; // that.filterCountries(that.selectedPostalCode); // that.filterStates(that.selectedPostalCode); // that.filterCities(that.selectedPostalCode) // }); // }); } clearFields() { this.routeStop.email = null; this.routeStop.contactPerson = null; this.routeStop.routeStop = null; this.address.addressLine1 = null; this.address.addressLine2 = null; this.address.postalCodeId = null; this.selectedPostalCodeValue = null; this.selectedPostalCode = null; this.selectedPostalCodeId = null; this.selectedCountry = null; this.selectedState = null; this.selectedCity = null; } close(): void { this.active = false; this.modal.hide(); } // filterPostal(): void { // this._postalCodeService.getDistinctPostalCode( // undefined // ).subscribe(result => { // this.filteredPostal = result.items; // }); // } filterCountries(postalCode): void { this._postalCodeServiceProxy.getDistinctCountry( postalCode, undefined ).subscribe(result => { this.filteredCountries = result.items; //$("#selectedCountry").text(this.filteredCountries[0]['country']); }); } // filterStates(postalCode): void { // this._postalCodeService.getDistinctState( // postalCode, // undefined // ).subscribe(result => { // this.filteredStates = result.items; // //$("#selectedState").text(this.filteredStates[0]['state']); // }); // } // filterCities(postalCode): void { // this._postalCodeService.getDistinctCity( // postalCode, // undefined // ).subscribe(result => { // this.filteredCities = result.items; // //$("#selectedCity").text(this.filteredCities[0]['city']); // }); // } }