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 } 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'; @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; @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(); contactAddress = ''; constructor( injector: Injector, private _addressService: AddressServiceProxy, private _emailService: EmailServiceProxy, private _postalCodeService: PostalCodeServiceProxy, private _routeStopsServiceProxy: RouteStopsServiceProxy ) { super(injector); } filteredCountries: any; filteredCities: any; filteredStates: any; filteredPostal: any; filteredAddressType: any; selectedPostalCode: string; selectedPostalCodeA: string; selectedAddressType: any; filterChange = false; show(routeStopId?: number): void { this.getAllDropDown(); this.spinnerService.show(); if (!routeStopId) { this.routeStop = new CreateOrEditRouteStopDto(); this.routeStop.id = routeStopId; //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.selectedPostalCodeId = this.address.postalCodeId; //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(); }); }); this.active = true; this.modal.show(); } } save(): void { this.saving = true; this.address.postalCodeId = Number((document.getElementById('postalSelectInput')).value); this.routeStop.address = this.address; console.log(this.routeStop); 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 () { $('.kt-select2').select2(); $("#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._postalCodeService.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']); }); } }