import { Component, OnInit, EventEmitter, Output, Input } from '@angular/core'; import { CommonService } from '../shared/service/common.service'; import { IPlaceLived } from '../shared/core/IPlacesLived'; import { StaffService } from '../shared/service/staff.service'; import { environment } from '../environments/environment'; import { Message } from 'primeng/api'; @Component({ selector: 'app-places-lived', templateUrl: './places-lived.component.html', styleUrls: ['./places-lived.component.css'] }) export class PlacesLivedComponent implements OnInit { //use when get existing staff obejct as a input @Input() placesLivedObject: IPlaceLived; //this is used to keep old data if in case editing cancelled, old data will be assigned tempPlaceLivedObject: IPlaceLived; //this contain auto complete result for city and country results: any; //this fields use to enable and disable editing fields showCityField: boolean = false; showHomeTownField: boolean = false; //default value for staff id defaultId = 0; //selected country flag countryFlag: string = environment.defaultImage; homeFlag: string = environment.defaultImage; //error message object for relationship component errMsgPlaceLive: Message[] = []; showNewHomeTownButton:boolean; showNewCityButton:boolean; @Output() bindPlaceLived: EventEmitter = new EventEmitter(); constructor(private commonService: CommonService) { } ngOnInit() { // debugger; this.errMsgPlaceLive = []; //keep old data until editing success this.tempPlaceLivedObject = { Id: 0, StaffId: 0, CurrentCity: "", HomeTown: "" }; //check whether current city has value, and hide and show text fields (this.placesLivedObject.CurrentCity === "") ? this.showNewCityButton = true : this.showNewCityButton = false; //check whether home town has value, and hide and show text fields (this.placesLivedObject.HomeTown === "") ? this.showNewHomeTownButton = true : this.showNewHomeTownButton = false; //check whether current city flag has value, and exchange values with country flag variable (this.placesLivedObject.CityFlag != "" || this.placesLivedObject.CityFlag != null) ? this.countryFlag = this.placesLivedObject.CityFlag : this.countryFlag = environment.defaultImage; //check whether home town flag has value, and exchange values with home flag variable (this.placesLivedObject.HomeFlag != "" || this.placesLivedObject.HomeFlag != null) ? this.homeFlag = this.placesLivedObject.HomeFlag : this.homeFlag = environment.defaultImage; if (this.countryFlag == null || this.countryFlag == "") { this.countryFlag = environment.defaultImage; } if (this.homeFlag == null || this.homeFlag == "") { this.homeFlag = environment.defaultImage; } // if(this.placesLivedObject.CurrentCity["countryName"]){ var whenCityIsObject = this.placesLivedObject.CurrentCity; this.placesLivedObject.CurrentCity = whenCityIsObject["countryName"] +"-"+whenCityIsObject["isoCode"]; } if(this.placesLivedObject.HomeTown["countryName"]){ var whenHomeIsObject = this.placesLivedObject.HomeTown; this.placesLivedObject.HomeTown = whenHomeIsObject["countryName"] +"-"+whenHomeIsObject["isoCode"]; } //flag detail also coming in current city value, so we split by - and get flag value when loading if (this.placesLivedObject && this.placesLivedObject.CurrentCity !== "" && this.placesLivedObject.CurrentCity != null && this.placesLivedObject.CurrentCity.indexOf("-") > -1) { //preparing current city var arrCity = this.placesLivedObject.CurrentCity.split('-'); if (arrCity.length > 0) { this.getCountryFlagLink(arrCity[1]); this.placesLivedObject.CityFlag = this.countryFlag; this.placesLivedObject.CurrentCity = arrCity[0]; this.tempPlaceLivedObject.CityFlag = this.countryFlag; this.tempPlaceLivedObject.CurrentCity = arrCity[0]; } else { //this.getCountryFlagLink(""); } }else { //this.showCityField = true; //this.getCountryFlagLink(""); } if (this.placesLivedObject && this.placesLivedObject.HomeTown !== "" && this.placesLivedObject.HomeTown != null && this.placesLivedObject.HomeTown.indexOf("-") > -1) { //preparing current hometown var arrHome = this.placesLivedObject.HomeTown.split('-'); if (arrHome.length > 0) { this.getHomeFlagLink(arrHome[1]); this.placesLivedObject.HomeFlag = this.homeFlag; this.placesLivedObject.HomeTown = arrHome[0]; this.tempPlaceLivedObject.HomeFlag = this.homeFlag; this.tempPlaceLivedObject.HomeTown = arrHome[0]; } else { //this.getHomeFlagLink(""); } }else { //this.showHomeTownField = true; //this.getHomeFlagLink(""); } } //this method use for auto comlplete country and city search(event) { this.commonService.getCoutryCityList(event.query).subscribe(data => { this.results = data; }); } //this method output the place live object onChangesCity() { if (this.placesLivedObject.CurrentCity) { this.getCountryFlagLink(this.placesLivedObject.CurrentCity["isoCode"]); this.placesLivedObject.CurrentCity = this.placesLivedObject.CurrentCity["countryName"]; this.placesLivedObject.CityFlag = this.countryFlag; this.bindPlaceLived.emit(this.placesLivedObject); } } //this method output the place live object onChangesHometown() { if (this.placesLivedObject.HomeTown) { this.getHomeFlagLink(this.placesLivedObject.HomeTown["isoCode"]); this.placesLivedObject.HomeTown = this.placesLivedObject.HomeTown["countryName"]; this.placesLivedObject.HomeFlag = this.homeFlag; this.bindPlaceLived.emit(this.placesLivedObject); } } //this method use to enable editing city editCity() { this.showCityField = true; this.showNewCityButton = false; } //this method use to enable editing hometown editHomeTown() { this.showHomeTownField = true; this.showNewHomeTownButton = false; } //this method use to remove value and enable editing city removeCity() { this.placesLivedObject.CurrentCity = ""; this.countryFlag = environment.defaultImage; this.showCityField = false; this.showNewCityButton = true; } //this method use to remove value and enable editing hometown removeHomeTown() { this.placesLivedObject.HomeTown = ""; this.homeFlag = environment.defaultImage; this.showHomeTownField = false; this.showNewHomeTownButton = true; } //this method use to save city modifiction saveCity() { this.errMsgPlaceLive = []; if (!this.placesLivedObject.CurrentCity) { this.errMsgPlaceLive.push({ severity: 'error', summary: 'Unable to save', detail: 'Please select from cities list!' }); return; } this.showCityField = false; this.showNewCityButton = false; this.onChangesCity(); } //this method use to save hometown modification saveHomeTown() { this.errMsgPlaceLive = []; if (!this.placesLivedObject.HomeTown) { this.errMsgPlaceLive.push({ severity: 'error', summary: 'Unable to save', detail: 'Please select from cities list!' }); return; } this.showHomeTownField = false; this.showNewHomeTownButton = false; this.onChangesHometown(); } //this method will replace older value of city when click cancel cancelCityEdit() { if(this.tempPlaceLivedObject.CurrentCity !=""){ this.placesLivedObject.CityFlag = this.tempPlaceLivedObject.CityFlag; this.placesLivedObject.CurrentCity = this.tempPlaceLivedObject.CurrentCity; this.countryFlag = this.tempPlaceLivedObject.CityFlag; this.showCityField = false; } } //this method will replace older value of hometown when click cancel cancelHomeTownEdit() { if(this.tempPlaceLivedObject.HomeTown){ this.placesLivedObject.HomeFlag = this.tempPlaceLivedObject.HomeFlag; this.placesLivedObject.HomeTown = this.tempPlaceLivedObject.HomeTown; this.homeFlag = this.tempPlaceLivedObject.HomeFlag; this.showHomeTownField = false; } } //this will call service and get country flag getCountryFlagLink(IsoCode) { if (IsoCode == "") { this.countryFlag = environment.defaultImage; } else { this.countryFlag = environment.imageLocation + IsoCode + "-128.png"; } } //this will call service and get home town flag getHomeFlagLink(IsoCode) { if (IsoCode == "") { this.homeFlag = environment.defaultImage; } else { this.homeFlag = environment.imageLocation + IsoCode + "-128.png"; } } }