import { Component, OnInit } from '@angular/core'; import { DynamicDialogRef, DynamicDialogConfig, DialogService } from 'primeng/api'; import { CookieService } from 'ngx-cookie-service'; import { ClientDetails } from '../../../shared/core/IClientDetails'; import { ContactService } from '../../../shared/service/contact.service'; import { ICompanyDetails } from '../../../shared/core/ICompanyDetails'; import { ICountry } from '../../../shared/core/ICountry'; import { IState } from '../../../shared/core/IState'; @Component({ selector: 'app-contact-create', templateUrl: './contact-create.component.html', styleUrls: ['./contact-create.component.css'] }) export class ContactCreateComponent implements OnInit { // Variables to hold the variable names staffId: any; clientFirstName = ''; clientLastName = ''; clientPhone = ''; clientFax = ''; clientEmail = ''; clientJobTitle = ''; clientIndustry = ''; clientCompany = ''; clientCompanyCode = ''; clientAddress1 = ''; clientAddress2 = ''; clientCity = ''; clientState = 0; clientCountry = 0; clientPostalCode = ''; // Variable to hold the countries countries: ICountry[] = []; clientSelectedCountry: ICountry; states: IState[] = []; clientSelectedState: IState; selectedCompanyObj = ''; selectedCompanyObjDropdown: ClientDetails; ClientDetails: ClientDetails[] = []; filteredCountriesSingle: any[]; companyDetails: ICompanyDetails; // Disable button disableSaveButton = true; disableTheDefaultfields = false; constructor( public contactCreateComponent: DynamicDialogRef, public config: DynamicDialogConfig, public dialogService: DialogService, private cookieService: CookieService, private contactService: ContactService ) { // Assing logged userId this.staffId = this.cookieService.get('LoggedUserId'); } ngOnInit() { // Get the company details this.contactService.getAllCountries().subscribe( data => { this.countries = data; } ); } // ------------------- Company auto fill section --------------------------// filterCompanySingle(event) { const query = event.query; // Get all companies this.contactService.getAllClientsByStaffId(this.staffId).subscribe( data => { this.filteredCountriesSingle = this.filterCompanies(query, data); } ); } filterCompanies(query, companies: ClientDetails[]): any[] { // in a real application, make a request to a remote url with the query and return filtered results, for demo we filter at client side const filtered: ClientDetails[] = []; let isSelected = false; // tslint:disable-next-line: prefer-for-of for (let i = 0; i < companies.length; i++) { const country = companies[i]; if (country.ClientName.toLowerCase().indexOf(query.toLowerCase()) === 0) { filtered.push(country); isSelected = true; } } if (isSelected === true) { this.disableTheDefaultfields = true; } else { this.disableTheDefaultfields = false; } return filtered; } onselectCompany(event) { this.selectedCompanyObjDropdown = event; // Get all the company related details this.contactService.getAllCompanyDetailsByID(this.selectedCompanyObjDropdown.ClientDetailsId).subscribe( data => { console.log(data); this.companyDetails = data; this.clientCompany = this.selectedCompanyObjDropdown.ClientName; this.clientCompanyCode = this.companyDetails.Code; this.clientAddress1 = this.companyDetails.Address; this.clientAddress2 = this.companyDetails.Address2; this.clientCity = this.companyDetails.CityName; this.clientState = this.companyDetails.State.StateId; this.clientCountry = this.companyDetails.Country.CountryId; this.clientPostalCode = this.companyDetails.PostalCode; this.clientSelectedCountry = this.countries.filter(country => country.CountryId === this.clientCountry)[0]; this.contactService.getAllStates(this.clientCountry).subscribe( result => { this.states = result; this.clientSelectedState = this.states.filter(state => state.StateId === this.clientState)[0]; this.validateFields(); } ); } ); } isEqualTo(element, index, array) { return (element >= 10); } onInputCompany() { if (this.selectedCompanyObjDropdown) { if (!(this.selectedCompanyObj === this.selectedCompanyObjDropdown.ClientName)) { this.disableTheDefaultfields = false; // Clear the fields this.clientCompanyCode = ''; this.clientAddress1 = ''; this.clientAddress2 = ''; this.clientCity = ''; this.clientState = 0; this.clientCountry = 0; this.clientPostalCode = ''; this.clientCompanyCode = ''; this.clientSelectedCountry = null; this.clientSelectedState = null; } else { this.selectedCompanyObj = this.selectedCompanyObjDropdown.ClientName; } } } // ------------------- End of Company auto fill section --------------------------// // Save the new contact and company saveClient() { const companyDetails: ICompanyDetails = { CompanyId: 0, CompanyName: this.clientCompany, Code: this.clientCompanyCode, Website: '', Industry: this.clientIndustry, Phone: this.clientPhone, Owner: '', Type: '', Address: this.clientAddress1, Address2: this.clientAddress2, CityName: this.clientCity, PostalCode: this.clientPostalCode, NoOfEmployee: 0, TimeZone: '', Description: '', Logo: '', Country: { CountryId: this.clientCountry, CountryName: this.clientSelectedCountry.CountryName }, State: { StateId: this.clientState, StateName: this.clientSelectedState.StateName }, ContactDetails: { ContactDetailsId: 0, FirstName: this.clientFirstName, LastName: this.clientLastName }, CompanyContacts: { Address: '', CompanyContactsId: 0, CompanyDetailsId: 0, ContactDetailsId: 0, ContactPositionsId: 0, ContactStatusId: 0, Email: this.clientEmail, EmailSignature: '', Fax: this.clientFax, IsDefaultCompany: true, IsMainContact: true, LeadStatus: false, Owner: '', Phone: this.clientPhone }, ContactPositionsList: { PositionId: 0, Positions: this.clientJobTitle, Title: '' }, StaffID: this.staffId }; // Inserting the record to the this.contactService.insertNewClientDetails(companyDetails).subscribe( data => { console.log(data); this.cancelOrganisation(); } ); } // Validate fields validateFields() { if (this.clientFirstName !== '' && this.clientLastName !== '' && this.clientPhone !== '' && this.clientEmail !== '' && this.clientAddress1 !== '' && this.clientCity !== '' && this.clientState !== 0 && this.clientCountry !== 0 && this.clientPostalCode !== '') { this.disableSaveButton = false; } else { this.disableSaveButton = true; } } // Close the current popup cancelOrganisation() { this.dialogService.dialogComponentRef.instance.close(); } // On change country onChangeCountry(event) { if (this.clientSelectedCountry) { this.clientCountry = this.clientSelectedCountry.CountryId; // Update the states this.getAllStates(this.clientCountry); } else { this.clientCountry = 0; } this.validateFields(); } // Get all the states based on the country ID getAllStates(countryID: number) { this.contactService.getAllStates(countryID).subscribe( data => { this.states = data; } ); } // On change state onChangeState(event) { if (this.clientSelectedState) { this.clientState = this.clientSelectedState.StateId; // Update the states } else { this.clientState = 0; } this.validateFields(); } // Charactor validation omit_special_char(event) { let k; k = event.charCode; return (!(k >= 27 && k <= 31) && !(k >= 33 && k <= 47) && !(k >= 48 && k <= 64) && !(k >= 91 && k <= 96) && !(k >= 123 && k <= 127)); } // Allow only numbers allow_numbers_char(event) { let k; k = event.charCode; return (k >= 48 && k < 58); } }