import { Component, ElementRef, OnInit } from '@angular/core'; import { File } from '@ionic-native/file/ngx'; import { Platform } from '@ionic/angular'; import { CommonService } from 'src/app/hmg-common/services/common/common.service'; import { MenuResponse } from 'src/app/hmg-common/services/menu/models/menu-response'; import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service'; import { ReportServiceService } from '../report-service.service'; import { FileOpener } from '@ionic-native/file-opener/ngx'; @Component({ selector: 'app-transaction-list', templateUrl: './transaction-list.component.html', styleUrls: ['./transaction-list.component.scss'], }) export class TransactionListComponent implements OnInit { transactionList: any = []; selectedMenu: any; template: any; direction: any; constructor(public cs: CommonService, private elementRef: ElementRef, public reports: ReportServiceService, public ts: TranslatorService, private platform: Platform, private file: File, private opener: FileOpener, ) { } ngOnInit() { this.selectedMenu = this.cs.sharedService.getSharedData(MenuResponse.SELECTED_MENU, false); this.transactionList = this.cs.sharedService.getSharedData(ReportServiceService.TRANSACTION_LIST, false); this.template = this.cs.sharedService.getSharedData(ReportServiceService.TEMPLATE, false); this.direction = TranslatorService.getCurrentDirection(); } getDate(date) { return this.cs.evaluteDate(date); } getCppOutput(transaction) { var request: any = { P_REQUEST_ID: transaction['REQUEST_ID'] }; this.cs.startLoading(); this.reports.getCppOutput(request, {}, this.ts.trPK('general', 'retry')).subscribe((result: any) => { this.saveAndOpenPdf(result.GetCcpOutputList['P_OUTPUT_FILE'], 'report.pdf'); console.log(result); this.cs.stopLoading(); }); } // saveAndOpenPdf(pdf: string, filename: string) { // const linkSource = `data:application/pdf;base64,${pdf}`; // const downloadLink = document.createElement("a"); // const fileName = "report.pdf"; // downloadLink.href = linkSource; // downloadLink.download = fileName; // downloadLink.click(); // } saveAndOpenPdf(pdf: string, filename: string) { const writeDirectory = this.platform.is('ios') ? this.file.dataDirectory : this.file.externalDataDirectory; this.file.writeFile(writeDirectory, filename, this.convertBase64ToBlob(pdf, 'data:application/pdf;base64'), { replace: true }) .then(() => { this.opener.open(writeDirectory + filename, 'application/pdf') .catch(() => { console.log('Error opening pdf file'); }); }) .catch(() => { console.error('Error writing pdf file'); }); } convertBase64ToBlob(b64Data, contentType): Blob { contentType = contentType || ''; const sliceSize = 512; b64Data = b64Data.replace(/^[^,]+,/, ''); b64Data = b64Data.replace(/\s/g, ''); const byteCharacters = window.atob(b64Data); const byteArrays = []; for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) { const slice = byteCharacters.slice(offset, offset + sliceSize); const byteNumbers = new Array(slice.length); for (let i = 0; i < slice.length; i++) { byteNumbers[i] = slice.charCodeAt(i); } const byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } return new Blob(byteArrays, { type: contentType }); } refresh(transaction) { var request: any = { P_REQUEST_ID: transaction['REQUEST_ID'] }; this.cs.startLoading(); var request: any = {}; request.P_MENU_TYPE = this.selectedMenu.MENU_TYPE; request.P_SELECTED_RESP_ID = -999; request.P_FUNCTION_NAME = this.selectedMenu.FUNCTION_NAME; request.P_DESC_FLEX_NAME = this.template.CONCURRENT_PROGRAM_NAME; this.reports.getTransaction(request, {}, this.ts.trPK('general', 'retry')).subscribe((result: any) => { this.cs.stopLoading(); this.transactionList = result['GetCcpTransactionsList_New']; }); } }