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 { ClinicsServiceProxy, CreateOrEditClinicDto, 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 { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { finalize } from 'rxjs/operators'; import {Location} from '@angular/common'; import * as moment from 'moment'; import * as _ from 'lodash'; import { AddressFormComponent } from '@app/shared/layout/form/address-form.component'; declare var KTWizard: any; declare var $: any; declare var KTApp: any; declare let swal: any; @Component({ templateUrl: './create-or-edit-clinic.component.html', encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()], styleUrls: ['./create-or-edit-clinic.component.less'], }) export class CreateEditClinicComponent extends AppComponentBase { @Input('selectedCountry') selectedCountry: string; @Input('selectedCity') selectedCity: string; @Input('selectedState') selectedState: string; //@Input('postalCodeId') selectedPostalCodeId: number; @Input('postalCodeValue') selectedPostalCodeValue: string; @ViewChild(AddressFormComponent, {static:false}) addressForm; active = false; saving = false; id: any; edit = false; clinic: CreateOrEditClinicDto = new CreateOrEditClinicDto(); address: CreateAddressInput = new CreateAddressInput(); postalCodeInput: CreatePostalCodeInput = new CreatePostalCodeInput(); constructor( injector: Injector, private _clinicsServiceProxy: ClinicsServiceProxy, private _addressService: AddressServiceProxy, private _emailService: EmailServiceProxy, private _postalCodeService: PostalCodeServiceProxy, private _addressTypeService: AddressTypeServiceProxy, private _location:Location, private router: Router, private route: ActivatedRoute, private _postalCodeServiceProxy: PostalCodeServiceProxy, ) { super(injector); } filteredCountries: any; filteredCities: any; filteredStates: any; filteredPostal: any; filteredAddressType: any; selectedPostalCode: string; selectedAddressType: any; selectedPostalCodeId: number; dynamicPostal: Array = []; newDynamic: any = {}; sampleCountry: string //status = false; filterChange = false stateFeature: boolean; ngOnInit() { this.stateFeature = abp.features.isEnabled('App.StateFeature'); this.route.paramMap.subscribe(params => { this.id = params.get("id") }); // $('.kt-select2').select2(); this.getClinic(); var that = this; // $(".kt-select2").select2().select2("val", that.addressForm.country); let maxcount = 1000; this.filterAddressTypes(maxcount); $(document).ready(() => { //ktwizard('kt_wizard_v2', this.options); //this.KTWizard2(); $("#visibleToDriver").text("No"); $("#Clinic_ClinicNotesVisibleToDriver").click(function(){ //var check = $("input[type='checkbox']").is(":checked"); if ($('#Clinic_ClinicNotesVisibleToDriver').is(":checked")) { $("#visibleToDriver").text("Yes"); } else { $("#visibleToDriver").text("No"); } }); $("#addressTypeSelectInput").change(function () { this.selectedAddressType = $("#addressTypeSelectInput option:selected").text(); $("#selectedAddressType").text(this.selectedAddressType); }); $("#countrySelectInput").change(function () { this.selectedCountry = $("#countrySelectInput option:selected").text(); $("#selectedCountry").text(this.selectedCountry); }); var KTWizard2=function(){ var e,r,i; return{init:function(){var t;KTUtil.get("kt_wizard_v2"),e=$("#kt_form"), (i=new KTWizard("kt_wizard_v2", {startStep:1,clickableSteps:!0})).on("beforeNext",function(e){!0 !==r.form()&&e.stop()}), i.on("beforePrev",function(e){!0!==r.form()&&e.stop()}),i.on("change",function(e){KTUtil.scrollTop()}), r=e.validate({ignore:":hidden", rules:{companyName:{required:!0}, accountNumber:{required:!0}, Address1:{required:!0}, postalSelectInput:{required:!0}, countrySelectInput:{required:!0}, stateSelectInput:{required:!0}, citySelectInput:{required:!0}}, invalidHandler:function(e,r){KTUtil.scrollTop(),swal.fire({title:"",text:"There are some errors in your submission. Please correct them.", type:"error",confirmButtonClass:"btn btn-secondary"})}, submitHandler:function(e){}})}}}(); jQuery(document).ready(function(){KTWizard2.init()}); $('.kt-select2').select2(); }); } ngAfterViewInit(){ } getClinic(){ // this.spinnerService.show() if (!this.id) { this.clinic = new CreateOrEditClinicDto(); this.clinic.id = this.id; this.clinic.active = true; //this.spinnerService.hide(); } else { this.edit = true; let that = this; this._clinicsServiceProxy.getClinicForEdit(this.id).subscribe(result => { this.clinic = result.clinic; this.address = result.clinic.address; //console.log(result.clinic.clinicLatitude) this.selectedPostalCodeId = result.clinic.address.postalCodeId if(this.selectedPostalCodeId != 0) { this._postalCodeService.getPostalCodeEdit(this.selectedPostalCodeId).subscribe(resultPostalCode => { this.address.postalCodeId = resultPostalCode.id this.addressForm.state = resultPostalCode.state; this.addressForm.city = resultPostalCode.city; this.addressForm.value = resultPostalCode.value; this.addressForm.country = resultPostalCode.country; this.addressForm.formattedAddress = this.address.addressLine1 + ' ' + resultPostalCode.city + ', ' + resultPostalCode.state + ' ' + resultPostalCode.value + ', ' + resultPostalCode.country; $("#selectedCountry").text( resultPostalCode.country); this.sampleCountry = resultPostalCode.country setTimeout(function(){ $('.kt-select2').select2(); // console.log('trigerred') }, 1500); }); } }); } this.spinnerService.hide() } save(): void { this.saving = true; // this.address.postalCodeId = Number((document.getElementById('postalSelectInput')).value); if(this.address.postalCodeId == 0 || this.address.postalCodeId == null) { this.address.postalCodeId = this.selectedPostalCodeId; } if (Number((document.getElementById('addressTypeSelectInput')).value) == 0) { this.address.addressTypeId = null; } else { this.address.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value); } 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; } console.log(this.addressForm); this.postalCodeInput.value = this.addressForm.value; this.postalCodeInput.country = this.addressForm.country; // this.postalCodeInput.country = $('#countrySelectInput').text(); console.log(this.postalCodeInput); this.clinic.address = this.address; this._postalCodeServiceProxy.createPostalCode(this.postalCodeInput) .pipe() .subscribe(result => { this.clinic.address.postalCodeId = result console.log(this.clinic); this._clinicsServiceProxy.createOrEdit(this.clinic) .pipe(finalize(() => { this.saving = false;})) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.router.navigate(['/app/main/laboratory/clinics']); },error=> { this.saving = false }); },error=> { this.saving = false }); } filterPostal(): void { //this.spinnerService.show(); let that = this; this._postalCodeService.getDistinctPostalCode( undefined ).subscribe(result => { //this.filteredPostal = result.items; let length = result.items.length; //console.log(result.items); for (var i = 0, len = result.items.length; i < len; i++) { let status = false; if(result.items[i].id === that.selectedPostalCodeId) { status = true; } //console.log(result.items[i].id + " " + that.selectedPostalCodeId); //if (i === length - 1){ //that.spinnerService.hide(); //} that.newDynamic = {id: result.items[i].id, value: result.items[i].value, status:status}; that.dynamicPostal.push(that.newDynamic); } }); } filterPostalDefault(id): void { this.spinnerService.show(); let that = this; this._postalCodeService.getDistinctPostalCode( id ).subscribe(result => { //this.filteredPostal = result.items; let length = result.items.length; //console.log(result.items); for (var i = 0, len = result.items.length; i < len; i++) { let status = false; if(result.items[i].id === that.selectedPostalCodeId) { status = true; that.newDynamic = {id: result.items[i].id, value: result.items[i].value, status:status}; that.dynamicPostal.push(that.newDynamic); that.spinnerService.hide(); break; } } }); } 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']); }); } filterAddressTypes(maxcount): void { this._addressTypeService.getAddressType( undefined, undefined, undefined, undefined, maxcount, undefined ).subscribe(result => { this.filteredAddressType = result.items; }); } goBack(){ this._location.back(); } }