import { useState, useRef } from 'react'; import { Emailsend } from '../../../../Features/PurchaseOrder/PurchaseOrder'; import { blobToBase64, encryptObject, pdfDiv, } from '../../../../Services/Others'; import { PrintStyleFunction } from '../../../paymentpdfPage/PrintStyleFunction'; import { PDFDocument } from 'pdf-lib'; import { PublicQrCodepost } from '../../../../Features/ConfigMasterPage/ConfigMasterPage'; import { renderToStaticMarkup } from 'react-dom/server'; import { downloadFile } from '../../../../utils/downloadFile'; const MainHomeUrl = import.meta.env.ENV_MAIN_BASE_URL; const uploadApiUrl = import.meta.env.ENV_IMAGE_UPLOAD_API_URL; // Create a secure blob URL creator that works in all environments const createSecureBlobUrl = (blob) => { try { return URL.createObjectURL(blob); } catch (error) { console.warn( 'Standard blob URL creation failed, using alternative approach' ); return window.URL.createObjectURL(blob); } }; // Revoke blob URL safely const revokeSecureBlobUrl = (url) => { if (url && url.startsWith('blob:')) { try { URL.revokeObjectURL(url); } catch (error) { console.warn('Failed to revoke blob URL:', error); } } }; // Merge multiple pdf blobs into one const mergePdfBlobs = async (blobs) => { const mergedPdf = await PDFDocument.create(); for (const blob of blobs) { const pdfBytes = await blob.arrayBuffer(); const pdf = await PDFDocument.load(pdfBytes); const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices()); copiedPages.forEach((page) => mergedPdf.addPage(page)); } const mergedBytes = await mergedPdf.save(); return new Blob([mergedBytes], { type: 'application/pdf' }); }; export const MobileMultiPdfPrint = async ({ printerTemplateStyle = 'Style 13', printDatas = {}, PrintOrderDetails = [], setPrintOrderDetails = () => { }, PrintComing = '', emailId = '', dispatch, UserId = '', onProgress = () => { }, returnBlob = false, }) => { try { const style = await PrintStyleFunction(printerTemplateStyle, 'Sales'); const stylesMap = { 'Style 1': 'PrintStyle1', 'Style 2': 'PrintStyle2', 'Style 3': 'PrintStyle3', 'Style 4': 'PrintStyle4', 'Style 5': 'PrintStyle5', 'Style 6': 'PrintStyle6', 'Style 7': 'PrintStyle7', 'Style 8': 'PrintStyle8', 'Style 9': 'PrintStyle9', 'Style 10': 'PrintStyle10', 'Style 11': 'PrintStyle11', 'Style 12': 'PrintStyle12', 'Style 13': 'RePrint', A4: 'PrintStyleA4', A5: 'PrintStyleA5', A4Standard: 'A4Standard', TaxInvoice: 'TaxInvoice', }; const selectedStyle = stylesMap[printerTemplateStyle] || 'RePrint'; const Printsize = printDatas?.PageSize || 'A4'; const totalOrders = PrintOrderDetails.length; let allBlobs = []; // 🔥 LOOP EACH INVOICE (MAIN FIX) for (let i = 0; i < totalOrders; i++) { const element = document.getElementById( `${selectedStyle}-${i}` ); if (!element) { console.warn('⚠️ Element not found:', `${selectedStyle}-${i}`); continue; } // 🔥 TEMP WRAPPER (IMPORTANT) const wrapper = document.createElement('div'); wrapper.id = `temp-${i}`; wrapper.style.width = '794px'; wrapper.style.background = '#fff'; wrapper.innerHTML = ` ${style} ${element.innerHTML} `; document.body.appendChild(wrapper); try { // 👉 ONE invoice → ONE PDF const blob = await pdfDiv(wrapper.id); allBlobs.push(blob); } catch (err) { console.error('PDF error:', err); } document.body.removeChild(wrapper); // 🔥 Progress update const progress = Math.round(((i + 1) / totalOrders) * 100); onProgress(progress); // 🔥 small delay for stability (important for large data) await new Promise((r) => setTimeout(r, 5)); } // // 🔥 MERGE ALL PDFs // const combinedPdfBlob = await mergePdfBlobs(allBlobs); // const base64Pdf = await blobToBase64(combinedPdfBlob); // // 🔥 DOWNLOAD // if (PrintComing === 'download') { // const blobUrl = createSecureBlobUrl(combinedPdfBlob); // const link = document.createElement('a'); // link.href = blobUrl; // link.download = 'Receipts.pdf'; // document.body.appendChild(link); // link.click(); // setTimeout(() => { // document.body.removeChild(link); // revokeSecureBlobUrl(blobUrl); // }, 100); // } const combinedPdfBlob = await mergePdfBlobs(allBlobs); const base64Pdf = await blobToBase64(combinedPdfBlob); // 🔥 DOWNLOAD if (PrintComing === 'download') { // ✅ Convert Blob → ArrayBuffer for downloadFile const buffer = await combinedPdfBlob.arrayBuffer(); downloadFile( buffer, "Receipts.pdf", "application/pdf", (success) => { if (!success) { console.error('Failed to download the file. Please try again.'); } } ); } // 🔥 RETURN BLOB if (returnBlob && PrintComing === 'download') { setPrintOrderDetails([]); return { success: true, pdfBlob: combinedPdfBlob }; } // 🔥 WHATSAPP if (PrintComing === 'whatsapp') { const orderIdsArray = PrintOrderDetails.map((row) => row?.OrderId).filter(Boolean); if (orderIdsArray.length === 0) return; const encrypted = encryptObject(orderIdsArray); const postData = { Url: encrypted, CreatedBy: UserId, }; try { const res = await dispatch(PublicQrCodepost(postData)).unwrap(); const urlData = res?.data?.ReferenceNo || res?.ReferenceNo; if (!urlData) return; const documentUrl = `${MainHomeUrl}viewbill?code=${urlData}`; const waUrl = 'https://web.whatsapp.com/send?text=' + encodeURIComponent('Here are your receipts 👉 ' + documentUrl); window.open(waUrl, '_blank'); } catch (err) { console.error('❌ WhatsApp error:', err); } } // 🔥 EMAIL if (PrintComing === 'email') { try { const payload = { fileBlob: combinedPdfBlob, toEmail: emailId, type: 'Sales', messageTemplatesList: [], }; const res = await dispatch(Emailsend(payload))?.unwrap(); return res; } catch (err) { console.error('❌ Email error:', err); } } // 🔥 PRINT VIEW if (!['download', 'whatsapp', 'email'].includes(PrintComing)) { const blobUrl = createSecureBlobUrl(combinedPdfBlob); const newWindow = window.open('', '_blank'); if (newWindow) { newWindow.document.write(`
`); newWindow.document.close(); } else { window.location.href = blobUrl; } const intentUrl = 'intent://open?' + 'filename=' + encodeURIComponent('Receipts.pdf') + '&size=' + combinedPdfBlob.size + '&type=application/pdf' + '&data=' + encodeURIComponent(base64Pdf) + '&PrintTypeS=' + encodeURIComponent(Printsize) + '#Intent;scheme=pozoprinter;package=com.example.pozoprinter;end'; setTimeout(() => { try { window.location.href = intentUrl; } catch { console.log("some errors") } }, 500); } // 🔥 CLEANUP setPrintOrderDetails([]); return { success: true, message: 'PDF processed successfully' }; } catch (error) { console.error('MobileMultiPdfPrint error:', error); return { success: false, message: error.message }; } }; // export const MobileMultiPdfPrint = async ({ // printerTemplateStyle = 'Style 13', // printDatas = {}, // PrintOrderDetails = [], // setPrintOrderDetails = () => {}, // PrintComing = '', // emailId = '', // dispatch, // UserId = '', // onProgress = () => {}, // returnBlob = false, // }) => { // try { // const style = await PrintStyleFunction(printerTemplateStyle, 'Sales'); // const stylesMap = { // 'Style 1': 'PrintStyle1', // 'Style 2': 'PrintStyle2', // 'Style 3': 'PrintStyle3', // 'Style 4': 'PrintStyle4', // 'Style 5': 'PrintStyle5', // 'Style 6': 'PrintStyle6', // 'Style 7': 'PrintStyle7', // 'Style 8': 'PrintStyle8', // 'Style 9': 'PrintStyle9', // 'Style 10': 'PrintStyle10', // 'Style 11': 'PrintStyle11', // 'Style 12': 'PrintStyle12', // 'Style 13': 'RePrint', // A4: 'PrintStyleA4', // A5: 'PrintStyleA5', // A4Standard: 'A4Standard', // TaxInvoice: 'TaxInvoice', // }; // const selectedStyle = stylesMap[printerTemplateStyle] || 'RePrint'; // const Printsize = printDatas?.PageSize || 'A4'; // // Create or clear the container // let container = document.getElementById('print-merged'); // if (!container) { // container = document.createElement('div'); // container.id = 'print-merged'; // container.style.display = 'none'; // document.body.appendChild(container); // } else { // container.innerHTML = ''; // } // // Process in batches for large files // const BATCH_SIZE = 5; // const totalOrders = PrintOrderDetails.length; // let processedOrders = 0; // let allBlobs = []; // for (let i = 0; i < totalOrders; i += BATCH_SIZE) { // const batch = PrintOrderDetails.slice(i, i + BATCH_SIZE); // // Clear container for each batch // container.innerHTML = ''; // // Process each order in the current batch // batch.forEach((_, index) => { // const globalIndex = i + index; // const element = document.getElementById( // `${selectedStyle}-${globalIndex}` // ); // if (element) { // const cloned = element.cloneNode(true); // // Add page break between orders (except the last one) // if (globalIndex < totalOrders - 1) { // const pageBreak = document.createElement('div'); // pageBreak.style.pageBreakAfter = 'always'; // container.appendChild(cloned); // container.appendChild(pageBreak); // } else { // container.appendChild(cloned); // } // } else { // console.warn( // '⚠️ Element not found:', // `${selectedStyle}-${globalIndex}` // ); // } // }); // // Generate PDF for this batch // try { // const pdfBlob = await pdfDiv('print-merged', style); // allBlobs.push(pdfBlob); // processedOrders += batch.length; // const progress = Math.round((processedOrders / totalOrders) * 100); // onProgress(progress); // } catch (error) { // console.error('Error generating PDF batch:', error); // throw new Error( // `Failed to generate PDF for batch starting at index ${i}` // ); // } // } // // ✅ Merge all PDF blobs into one // const combinedPdfBlob = await mergePdfBlobs(allBlobs); // const base64Pdf = await blobToBase64(combinedPdfBlob); // // Handle different print actions // if (PrintComing === 'download') { // const blobUrl = createSecureBlobUrl(combinedPdfBlob); // const link = document.createElement('a'); // link.href = blobUrl; // link.download = 'Receipts.pdf'; // document.body.appendChild(link); // link.click(); // setTimeout(() => { // document.body.removeChild(link); // revokeSecureBlobUrl(blobUrl); // }, 100); // } // // Return blob if requested (after download completes) // if (returnBlob && PrintComing === 'download') { // setPrintOrderDetails([]); // if (container) { // document.body.removeChild(container); // } // return { success: true, pdfBlob: combinedPdfBlob }; // } // // if (PrintComing === "whatsapp") { // // const orderIds = PrintOrderDetails.map((row) => row?.OrderId).filter(Boolean); // // const documentUrl = `${MainHomeUrl}viewbill?code=${orderIds}`; // // const waUrl = // // "https://web.whatsapp.com/send?text=" + // // encodeURIComponent("Here are your receipts 👉 " + documentUrl); // // window.open(waUrl, "_blank"); // // } // if (PrintComing === 'whatsapp') { // const orderIdsArray = PrintOrderDetails.map((row) => row?.OrderId).filter( // Boolean // ); // if (orderIdsArray.length === 0) { // console.warn('⚠️ No order IDs found for WhatsApp sharing.'); // return; // } // // 2️⃣ Encrypt order IDs // const encrypted = encryptObject(orderIdsArray); // // 3️⃣ Prepare payload for backend // const postData = { // Url: encrypted, // CreatedBy: UserId, // Make sure UserId is passed correctly to your function // }; // try { // // 4️⃣ Call backend to get a ReferenceNo // const res = await dispatch(PublicQrCodepost(postData)).unwrap(); // console.log('Backend response:', res); // // 5️⃣ Extract ReferenceNo (adjust based on your backend response structure) // // Example: it might be res.data.ReferenceNo or res.ReferenceNo // const urlData = res?.data?.ReferenceNo || res?.ReferenceNo; // if (!urlData) { // console.error('❌ No ReferenceNo returned from backend'); // return; // } // // 6️⃣ Build the shareable URL // const documentUrl = `${MainHomeUrl}viewbill?code=${urlData}`; // // 7️⃣ Open WhatsApp Web with URL // const waUrl = // 'https://web.whatsapp.com/send?text=' + // encodeURIComponent('Here are your receipts 👉 ' + documentUrl); // window.open(waUrl, '_blank'); // } catch (err) { // console.error('❌ WhatsApp sharing failed:', err); // } // } // if (PrintComing === 'email') { // try { // const payload = { // fileBlob: combinedPdfBlob, // toEmail: emailId, // type: 'Sales', // messageTemplatesList: [], // }; // const res = await dispatch(Emailsend(payload))?.unwrap(); // console.log('✅ Email sent response:', res?.data || res); // return res; // } catch (err) { // console.error('❌ Email error:', err); // } // } // if (!['download', 'whatsapp', 'email'].includes(PrintComing)) { // const blobUrl = createSecureBlobUrl(combinedPdfBlob); // const newWindow = window.open('', '_blank'); // if (newWindow) { // newWindow.document.write(` // // //{progress}% Complete