import { Component, OnInit } from '@angular/core'; import { DynamicDialogRef, DynamicDialogConfig, DialogService, MessageService, Message } from 'primeng/api'; import { CookieService } from 'ngx-cookie-service'; import { ContactService } from '../../../shared/service/contact.service'; import * as XLSX from 'xlsx'; import { BehaviorSubjectService } from '../../../shared/service/behavior-subject.service'; import { ExportImportContacts } from '../../../shared/core/ExportImportContacts'; @Component({ selector: 'app-contact-export', templateUrl: './contact-export.component.html', styleUrls: ['./contact-export.component.css'], providers: [DialogService, MessageService] }) export class ContactExportComponent implements OnInit { // Attributes contactInfoAttributes; selectedAll = false; displayExportSelection = true; displayExportConfirmation = false; // selectedContactInfo: ExportImportContacts[]; paramObject: any; // selected contacts from grid selectedContactIdList: string[] = []; selectedValues: any[] = []; // Export Contacts exportImportContacts: ExportImportContacts[]; // Error Message msgs: Message[] = []; // Message flag showErrorMessage: boolean; constructor( public dialogService: DialogService, private cookieService: CookieService, private contactService: ContactService, private behaviorSubjectService: BehaviorSubjectService, public contactExportComponent: DynamicDialogRef, public config: DynamicDialogConfig ) { // The attributes of the contact info object this.contactInfoAttributes = [ { name: 'Firstname', displayName: 'First Name', selected: false }, { name: 'LastName', displayName: 'Last Name', selected: false }, { name: 'Email', displayName: 'Email', selected: false }, { name: 'Address', displayName: 'Address', selected: false }, { name: 'Phone', displayName: 'Phone', selected: false }, { name: 'Mobile', displayName: 'Mobile', selected: false }, { name: 'Fax', displayName: 'Fax', selected: false }, { name: 'CompanyName', displayName: 'Company Name', selected: false }, { name: 'CompanyCode', displayName: 'Company Code', selected: false }, { name: 'CompanyWebsite', displayName: 'Company Website', selected: false } ]; this.behaviorSubjectService.contactIdListObjectBehavior.subscribe((obj: any) => { this.selectedContactIdList = obj; // console.log('Selected contact list'); // console.log(obj); // console.log(this.selectedContactIdList); }); } ngOnInit() { this.showErrorMessage = true; debugger; this.contactService.GetExportContacts(this.selectedContactIdList, +this.cookieService.get('CompanyId')).subscribe( data => { console.log('Selected Contacts'); console.log(data); this.exportImportContacts = data; } ); console.log(this.exportImportContacts); } // The on click function of select all selectAllOnClickFunction() { // this.selectedAll = !this.selectedAll; // Setting all the selected values acording to the select all status // tslint:disable-next-line: prefer-for-of // for (let i = 0; i < this.contactInfoAttributes.length; i++) { // this.contactInfoAttributes[i].Selected = this.selectedAll; // } } // The on click function of the export button exportOnClickFunction() { // console.log(this.selectedContactIdList); // Displaying in the console // console.log('The status of the selected attributes \n'); // tslint:disable-next-line: prefer-for-of // for (let i = 0; i < this.contactInfoAttributes.length; i++) { // console.log('Name : ' + this.contactInfoAttributes[i].name + ', Select Status: ' + this.contactInfoAttributes[i].Selected); // } // console.log(this.selectedContactInfo); // // Check if the user atleast have selected one col // if (this.validateUserSelectedCols()) { // // Setting the export selection section to as false // this.displayExportSelection = false; // // Setting the export confirmation section to as true // this.displayExportConfirmation = true; // this.exportToExcel(); // } else { // console.log('NotSelectedColsError'); // // const dialogRef = this.dialog.open(ConfirmationDialogComponent, { // // width: '75vh', // // data: { // // confirmationType: 'NotSelectedColsError' // // }, // // disableClose: false, // // hasBackdrop: false // // }); // } this.exportToExcel(); } // Checking if the user has selected any cols validateUserSelectedCols() { let status = false; // Iterating the contact object to see the selected items // tslint:disable-next-line: prefer-for-of for (let i = 0; i < this.contactInfoAttributes.length; i++) { // If atleast one col is selected, terminating the loop if (this.contactInfoAttributes[i].Selected === true) { status = true; break; } } return status; } // Generate the json object exportToExcel() { if (this.selectedValues.length !== 0) { this.showErrorMessage = false; // tslint:disable-next-line: prefer-const let exportList = []; // tslint:disable-next-line: prefer-for-of for (let j = 0; j < this.exportImportContacts.length; j++) { const jsonData = {}; // tslint:disable-next-line: prefer-for-of for (let i = 0; i < this.selectedValues.length; i++) { // if (this.contactInfoAttributes[i].Selected === true) { const colName = this.selectedValues[i]; if (colName === 'First Name') { jsonData[colName] = this.exportImportContacts[j].FirstName; } else if (colName === 'Last Name') { jsonData[colName] = this.exportImportContacts[j].LastName; } else if (colName === 'Email') { jsonData[colName] = this.exportImportContacts[j].Email; } else if (colName === 'Address') { jsonData[colName] = this.exportImportContacts[j].Address; } else if (colName === 'Phone') { jsonData[colName] = this.exportImportContacts[j].Phone; } else if (colName === 'Mobile') { jsonData[colName] = this.exportImportContacts[j].Mobile; } else if (colName === 'Fax') { jsonData[colName] = this.exportImportContacts[j].Fax; } else if (colName === 'Company Name') { jsonData[colName] = this.exportImportContacts[j].CompanyName; } else if (colName === 'Company Code') { jsonData[colName] = this.exportImportContacts[j].CompanyCode; } else if (colName === 'Company Website') { jsonData[colName] = this.exportImportContacts[j].CompanyWebsite; } // } } exportList.push(jsonData); } const workBook = XLSX.utils.book_new(); // create a new blank book const workSheet = XLSX.utils.json_to_sheet(exportList); XLSX.utils.book_append_sheet(workBook, workSheet, 'data'); // add the worksheet to the book XLSX.writeFile(workBook, 'ContactDetails.csv'); // initiate a file download in browser this.displayExportSelection = false; } else { this.showErrorMessage = true; // Display message this.msgs = []; this.msgs.push({ severity: 'error', summary: 'Error Message', detail: 'Export Fields Not Selected' }); } } // Each attribute on click function eachAttributeOnClickFunction(attributeName) { // Checking for the checked attribute and making it as true // tslint:disable-next-line: prefer-for-of for (let i = 0; i < this.contactInfoAttributes.length; i++) { if (this.contactInfoAttributes[i].name === attributeName) { this.contactInfoAttributes[i].Selected = !this.contactInfoAttributes[i].Selected; break; } } } // The on click function of the go back button goBackOnClickFunction() { // Setting the export selection section to as true this.displayExportSelection = true; // Setting the export confirmation section to as false this.displayExportConfirmation = false; } Cancel() { //this.dialogService.dialogComponentRef.instance.close(); this.contactExportComponent.close(); } }