import { Component, OnInit, ViewChild } from '@angular/core'; import {DynamicDialogRef,DynamicDialogConfig, Message} from 'primeng/api'; import {TabViewModule} from 'primeng/tabview'; import {InputTextModule} from 'primeng/inputtext'; import { IOrganisationType } from '../shared/core/IOrganisationType'; import { IIndustry } from '../shared/core/IIndustry'; import { OrganisationService } from '../shared/service/organisation.service'; import { IOrganisation } from '../shared/core/IOrganisation'; import { CommonService } from '../shared/service/common.service'; import { ICountry } from '../shared/core/ICountry'; import { IState } from '../shared/core/IState'; import { LocalisationComponent } from '../localisation/localisation.component'; import { Router } from '@angular/router'; import { INewOrganisation } from '../shared/core/INewOrganisation'; import { PreferencesComponent } from '../preferences/preferences.component'; import { ILocalisation } from '../shared/core/ILocalisation'; @Component({ selector: 'app-new-organisation', templateUrl: './new-organisation.component.html', styleUrls: ['./new-organisation.component.css'], providers:[OrganisationService,CommonService] }) export class NewOrganisationComponent implements OnInit { @ViewChild(LocalisationComponent) localisationComponent: LocalisationComponent; @ViewChild(PreferencesComponent) preferencesComponent: PreferencesComponent; orgTypeList:IOrganisationType[]=[]; industryList:IIndustry[]=[]; organisation:IOrganisation; selectedType:IOrganisationType; selectedIndustry:IIndustry; showNewOrgType:boolean=false; orgTypeName:string=""; showNewIndustryType:boolean=false; orgIndustryName:string=""; countryList:ICountry[]=[]; stateList:IState[]=[]; selectedCountry:ICountry; selectedState:IState; editedPublicProfile: string; newPublicProfile: string; showLocalisations:boolean; organisationId:number=0; isNewOrganisation:boolean; errMsg: Message[] = []; newLocalisationList: ILocalisation; imageFile: File; filechanged: boolean; // organisationLogo:string = 'https://placehold.it/120x120'; organisationLogo:string = '/assets/images/logo.png'; disableSaveBtn:boolean = false; newOrganisationId:number = 0; constructor(private router: Router, public refer: DynamicDialogRef, public config: DynamicDialogConfig,private organisationService:OrganisationService, private commonService:CommonService) { } ngOnInit() { var organisationId = this.config.data.id; this.organisationId = this.config.data.id this.isNewOrganisation = (this.config.data.id >0) ? false : true; console.log(this.config.data.id); // initialize the organisation object this.organisation={Id:this.config.data.id,CompanyName:'',Email:'',Industry:'',Country:'',Phone:'',Staff:0,Type:'',Website:'',localisationId:0}; //initialize the address object this.organisation.Address = {Id:0,StaffId:0, StreetName:'',StreetName2:'',CountryId:0,StateId:0,City:'',PostCode:''}; //get county list this.getCountries(); //for edit organisation if(organisationId>0){ this.organisationService.getOrganisations(organisationId,null).subscribe(org=>{ var tempOrg = org.find(x=>x.Id==organisationId); tempOrg.Address={Id:0, StaffId:0, StreetName:'',StreetName2:'',CountryId:0,StateId:0,City:'',PostCode:''}; // set organisation logo preview this.organisationLogo = (tempOrg.LogoName !=null && tempOrg.LogoName!="") ? tempOrg.LogoUrl : '/assets/images/logo.png'; this.organisation = tempOrg; //get organisation address this.getOrgAddress(organisationId); //get organisation types this.getOrganisationTypes(); //get industries this.getIndustries(); //this.selectedIndustry = this.industryList.find(x=>x.Id==this.organisation.IndustryId); //this.selectedType = this.orgTypeList.find(x=>x.Id==this.organisation.TypeId); // enable localisation panel this.showLocalisations = true; }) } else{ // enable localisation panel this.showLocalisations = true; //get organisation types this.getOrganisationTypes(); //get industries this.getIndustries(); } this.errMsg = []; } //get organisation types getOrganisationTypes(){ this.organisationService.getOrganisationTypes().subscribe(types=>{ this.orgTypeList = types; if(this.organisation){ this.selectedType = this.orgTypeList.find(x=>x.Id==this.organisation.TypeId); } }) } //get industries getIndustries(){ this.organisationService.getIndustries().subscribe(industries=>{ this.industryList = industries; if(this.organisation){ this.selectedIndustry = this.industryList.find(x=>x.Id==this.organisation.IndustryId); } }) } // save new organisation type and return new list saveOrgType(){ console.log(this.orgTypeName); if(this.orgTypeName!=''){ this.organisationService.saveOrganisationType(this.orgTypeName).subscribe(types=>{ this.orgTypeList = types; this.showNewOrgType = false; this.orgTypeName = ''; }) } } //save new industry and return new list saveIndustryType(){ console.log(this.orgIndustryName); if(this.orgIndustryName!=''){ this.organisationService.saveIndustryType(this.orgIndustryName).subscribe(industries=>{ this.industryList = industries; this.showNewIndustryType = false; this.orgIndustryName = ''; }) } } //close new organisation type panel cancelOrgType(){ this.showNewOrgType = false; this.orgTypeName = ''; } //close new industry type panel cancelIndustryType(){ this.showNewIndustryType = false; this.orgIndustryName = ''; } //save new organisation saveOrganisation(){ this.disableSaveBtn = true; this.errMsg = []; if (!this.organisationProfileValidation()) { this.disableSaveBtn = false; return; } if(this.selectedType !=null && this.selectedType!= undefined && this.selectedIndustry !=null && this.selectedIndustry!= undefined && this.organisation.CompanyName!='' && this.organisation.Address.Country !=null && this.organisation.Address.Country.Id >0 && this.organisation.Address.State !=null && this.organisation.Address.State.Id > 0){ this.organisation.IndustryId = this.selectedIndustry.Id; this.organisation.Industry = this.selectedIndustry.Industry; this.organisation.TypeId = this.selectedType.Id; this.organisation.Type = this.selectedType.TypeName; var localisaiton:ILocalisation = { AllowPersonalise: this.localisationComponent.localisation.AllowPersonalise, CalendarStart:[this.localisationComponent.selectedCalendarStart], DateFormat:[this.localisationComponent.selectedDateFormat], Languages:[this.localisationComponent.selectedLanguage], TimeFormat:[this.localisationComponent.selectedTimeFormat], TimeZone:[this.localisationComponent.selectedTimeZone], Id:this.localisationComponent.localisation.Id } var newOrganisation: INewOrganisation = { Organisation: this.organisation, Localisation: localisaiton, Preference: this.preferencesComponent.preference } // upload logo if(this.filechanged){ this.organisationService.uploadFile(this.imageFile).subscribe(data=>{ if(data!=null){ this.organisation.LogoUrl = data; this.organisation.LogoName = data; } //save the organisation this.organisationService.saveOrganisation(newOrganisation).subscribe(data=>{ this.newOrganisationId = data; this.errMsg.push({severity:'success', summary:'Success!', detail:'Organisation saved successfully!'}); this.disableSaveBtn = false; }); }); } else{ //save the organisation this.organisationService.saveOrganisation(newOrganisation).subscribe(data=>{ this.newOrganisationId = data; this.errMsg.push({severity:'success', summary:'Success!', detail:'Organisation saved successfully!'}); this.disableSaveBtn = false; }); } } else{ this.errMsg.push({severity:'error', summary:'Unable to save', detail:'Please fill organisation details'}); } } // validate organisation detail organisationProfileValidation(){ if(this.organisation.CompanyName ==""){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please fill Company name!' }); return false; } // check email format else if(this.organisation.Website !="" && !this.isWebsite(this.organisation.Website)){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please check website format!' }); return false; } else if(this.organisation.Website==""){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please fill website!' }); return false; } // check email format else if(this.organisation.Email !="" && !this.isEmail(this.organisation.Email)){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please check email format!' }); return false; } else if(this.organisation.Email==""){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please fill email!' }); return false; } // check first name else if(this.organisation.FirstName ==""){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please fill first name!' }); return false; } // check last name else if(this.organisation.LastName ==""){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please fill last name!' }); return false; } // check organisation type else if(this.selectedType ==null || this.selectedType == undefined ){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please select a organisation type!' }); return false; } // check industry else if(this.selectedIndustry ==null || this.selectedIndustry == undefined ){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please fill industry!' }); return false; } // check address line1 else if(this.organisation.Address.StreetName ==""){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please fill Address line 1!' }); return false; } // check country else if(this.organisation.Address.Country ==null || this.organisation.Address.Country.Id == 0 ){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please select a country!' }); return false; } // check state else if(this.organisation.Address.State ==null || this.organisation.Address.State.Id == 0 ){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please fill State!' }); return false; } // check city else if(this.organisation.Address.City =="" ){ this.errMsg.push({ severity: 'error', summary: 'Unable to save', detail: 'Please fill city!' }); return false; } return true; } // validate email isEmail(search:string):boolean { var serchfind:boolean; var regexp = new RegExp(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/); serchfind = regexp.test(search); return serchfind } // validate website isWebsite(search:string):boolean{ var serchfind:boolean; var regexp = new RegExp(/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/); serchfind = regexp.test(search); return serchfind } //close new organisation panel cancelOrganisation(){ this.refer.close(this.newOrganisationId); //this.router.navigate(['/Organisation']); //window.location.reload(); } //get organisation address getOrgAddress(id:number){ this.organisationService.getOrgAddress(id.toString()).subscribe(address=>{ this.organisation.Address = address; // bind the selected country this.selectedCountry = this.countryList.find(x=>x.Id==this.organisation.Address.CountryId); // get state list of the selected country this.getStates(this.organisation.Address.CountryId); }) } //get country list getCountries(){ this.commonService.getCountries().subscribe(countries=>{ this.countryList = countries; }); } //get state list getStates(id:number){ this.commonService.getStates(id).subscribe(states=>{ this.stateList = states; // bind the selected state if(this.organisation.Id>0){ this.selectedState = this.stateList.find(x=>x.Id==this.organisation.Address.StateId); } }); } //get states of the selected country selectCountry(){ this.commonService.getStates(this.organisation.Address.Country.Id).subscribe(states=>{ this.stateList = states; }) } //get selected company Logo readURL(files: FileList){ this.filechanged = true; console.log(files.item(0)); var file : File; this.imageFile = files.item(0); } //getting new localisaiton detail from localisation component getNewLocalisation(event) { this.newLocalisationList = event; } }