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, GetContactSettingsItemDto } 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 latError = false; longError = false; //status = false; performBlindCount: boolean = false; isDropOff: boolean = false; etaLock: boolean = false; clinicSetting: any; clinicSettingInput: Array = []; performBlindCountInput: GetContactSettingsItemDto = new GetContactSettingsItemDto(); isDropOffInput: GetContactSettingsItemDto = new GetContactSettingsItemDto(); etaLockInput: GetContactSettingsItemDto = new GetContactSettingsItemDto(); 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("Yes"); $("#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(){ } validateLongInput(inputNumber) { if(inputNumber % 1 != 0) { var numbers = inputNumber.toString().split('.'); var preDecimal = numbers[0]; var postDecimal = numbers[1]; if(inputNumber > 0) { if (preDecimal.length>3) { return true; } else { return false; } } else { if (preDecimal.length>4) { return true; } else { return false; } } } else { if(inputNumber.toString().length > 4) { return true; } else { return false; } } } validateLatInput(inputNumber) { if(inputNumber % 1 != 0) { var numbers = inputNumber.toString().split('.'); var preDecimal = numbers[0]; var postDecimal = numbers[1]; if(inputNumber > 0) { if (preDecimal.length>2) { return true } else { return false } } else { if (preDecimal.length>3) { return true } else { return false } } } else { if(inputNumber.toString().length > 3) { return true; } else { return false; } } } getClinic(){ // this.spinnerService.show() if (!this.id) { this.clinic = new CreateOrEditClinicDto(); this.clinic.id = this.id; this.clinic.active = true; this.clinic.clinicNotesVisibleToDriver = 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; this.clinicSetting = result.clinic.contactSetting; for (var val of this.clinicSetting) { if(val.code == 'PerformBlindCount') { if(val.value == 'true') { this.performBlindCount = true; } else { this.performBlindCount = false; } } else if(val.code == 'IsDropOff') { if(val.value == 'true') { this.isDropOff = true; } else { this.isDropOff = false; } } else if(val.code == 'EtaLock') { if(val.value == 'true') { this.etaLock = true; } else { this.etaLock = false; } } } //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.latitude = this.clinic.clinicLatitude; this.addressForm.longitude = this.clinic.clinicLongitude; this.addressForm.formattedAddress = this.address.formattedAddress; $("#selectedCountry").text( resultPostalCode.country); this.sampleCountry = resultPostalCode.country setTimeout(function(){ $('.kt-select2').select2(); // console.log('trigerred') }, 1500); }); } }); } this.spinnerService.hide() } save(): void { if(this.edit == true) { this.longError = this.validateLongInput(this.addressForm.longitude); this.latError = this.validateLatInput(this.addressForm.latitude); } if(this.longError != true && this.latError != true) { this.saving = true; this.etaLockInput.code = 'EtaLock'; this.etaLockInput.value = this.etaLock.toString(); /*this.isDropOffInput.code = 'IsDropOff'; this.isDropOffInput.value = this.isDropOff.toString(); this.performBlindCountInput.code = 'PerformBlindCount'; this.performBlindCountInput.value = this.performBlindCount.toString();*/ if(this.edit == true) { this.clinic.contactSetting.push(this.etaLockInput); //this.clinic.contactSetting.push(this.isDropOffInput); //this.clinic.contactSetting.push(this.performBlindCountInput); } else { //let etaLockInputCreate = {code: 'EtaLock', value: this.etaLock.toString()}; //let isDropOffInputCreate = {code: 'IsDropOff', value: this.etaLock.toString()}; //let performBlindCountInputCreate = {code: 'PerformBlindCount', value: this.etaLock.toString()}; this.clinicSettingInput[0] = this.etaLockInput; //this.clinicSettingInput[1] = this.isDropOffInput; //this.clinicSettingInput[2] = this.performBlindCountInput; this.clinic.contactSetting = this.clinicSettingInput; } // 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.address.formattedAddress = this.addressForm.formattedAddress; 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.value = this.addressForm.value; this.postalCodeInput.country = this.addressForm.country; this.clinic.clinicLatitude = this.addressForm.latitude; this.clinic.clinicLongitude = this.addressForm.longitude; // this.postalCodeInput.country = $('#countrySelectInput').text(); this.clinic.address = this.address; this._postalCodeServiceProxy.createPostalCode(this.postalCodeInput) .pipe() .subscribe(result => { this.clinic.address.postalCodeId = result 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(); } }