// Importing the libraries import { Component, OnInit } from '@angular/core'; import { DynamicDialogRef, DynamicDialogConfig, DialogService } from 'primeng/api'; // Importing the services // Importing the core classes import { CookieService } from 'ngx-cookie-service'; import { DashboardServicesService } from '../../../shared/services/dashboard-services.service'; import { ICountry } from '../../../shared/core/ICountry'; import { IState } from '../../../shared/core/IState'; import { ICompanyDetails } from '../../../shared/core/ICompanyDetails'; @Component({ selector: 'app-add-client', templateUrl: './add-client.component.html', styleUrls: ['./add-client.component.css'], providers: [DashboardServicesService] }) export class AddClientComponent implements OnInit { // Variables to hold the variable names 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; // Disable button disableSaveButton = true; // saving icon showLoading:boolean = false; constructor( public ref: DynamicDialogRef, public config: DynamicDialogConfig, public dashboardServicesService: DashboardServicesService, public dialogService: DialogService, private cookieService: CookieService) { } ngOnInit() { // Get the company details this.dashboardServicesService.getAllCountries().subscribe( data => { this.countries = data; } ); } // 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: 1, IsMainContact: 1, LeadStatus: 0, Owner: '', Phone: this.clientPhone }, ContactPositions: { ContactPositionsId: 0, Description: this.clientJobTitle, Title: '' }, StaffID: +this.cookieService.get('LoggedUserId') }; this.showLoading = true; // Inserting the record to the this.dashboardServicesService.insertNewClientDetails(companyDetails).subscribe( data => { this.showLoading = false; // console.log(data); this.cancelOrganisation(); } ); } // Validate fields validateFields() { // if (this.clientFirstName !== '' && this.clientLastName !== '' && this.clientPhone !== '' && this.clientEmail !== '' // && this.clientJobTitle !== '' && this.clientIndustry !== '' && this.clientCompany !== '' // && this.clientCompanyCode !== '' && this.clientAddress1 !== '' && this.clientCity !== '' && this.clientState !== 0 // && this.clientCountry !== 0 && this.clientPostalCode !== '') { // this.disableSaveButton = false; // } else { // this.disableSaveButton = true; // } if (this.clientFirstName !== '' && this.clientLastName !== '' && this.clientPhone !== '' && this.clientEmail !== '' && this.clientCompany !== '' && this.clientCompanyCode !== '' && 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(); this.dialogService.dialogComponentRef.destroy(); } // 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.dashboardServicesService.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; // k = event.keyCode; (Both can be used) // return((k > 64 && k < 91) || (k > 96 && k < 123) || k === 8 || k === 32 || (k >= 48 && k <= 57)); // Special Charactor validation 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); } }