`;
return;
}
const mostCommonType = lastSearchResults.reduce((acc, method) => {
acc[method.type] = (acc[method.type] || 0) + 1;
return acc;
}, {});
const topType = Object.keys(mostCommonType).reduce((a, b) => mostCommonType[a] > mostCommonType[b] ? a : b, '');
const resultsHtml = lastSearchResults.map(method => `
`).join('');
resultsArea.innerHTML = `
`;
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderConfig = () => {
const configContent = document.getElementById('content-data-configuration');
if (!configContent) return;
const methodRows = appData.methods.map((method, index) => `
`).join('');
configContent.innerHTML = `
`;
attachConfigListeners();
};
const attachDashboardListeners = () => {
document.getElementById('find-methods-btn').addEventListener('click', handleFindMethods);
};
const attachConfigListeners = () => {
document.getElementById('add-method-btn').addEventListener('click', () => {
appData.methods.push({ name: '', type: '', description: '', countries: [], industries: [], popularity: '' });
renderConfig();
});
document.getElementById('save-data-btn').addEventListener('click', handleConfigUpdate);
document.querySelectorAll('.remove-method-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const index = parseInt(e.currentTarget.dataset.index);
appData.methods.splice(index, 1);
renderConfig();
});
});
};
const handleFindMethods = () => {
const country = document.getElementById('country-select').value;
const industry = document.getElementById('industry-select').value;
appData.lastSearchCriteria = { country, industry };
appData.lastSearchResults = appData.methods.filter(method => {
const countryMatch = !country || method.countries.includes(country);
const industryMatch = !industry || method.industries.includes(industry);
return countryMatch && industryMatch;
});
renderResults();
};
const handleConfigUpdate = () => {
const newMethods = [];
document.querySelectorAll('.method-row').forEach(row => {
newMethods.push({
name: row.querySelector('[data-field="name"]').value,
type: row.querySelector('[data-field="type"]').value,
description: row.querySelector('[data-field="description"]').value,
countries: row.querySelector('[data-field="countries"]').value.split(',').map(c => c.trim()).filter(Boolean),
industries: row.querySelector('[data-field="industries"]').value.split(',').map(i => i.trim()).filter(Boolean),
popularity: row.querySelector('[data-field="popularity"]').value
});
});
appData.methods = newMethods;
alert('Data saved! The finder options will be updated.');
renderFinderDashboard();
document.getElementById('results-area').innerHTML = ''; // Clear old results
};
const generatePdf = () => {
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content-area');
const pdfHeader = document.getElementById('pdf-header');
pdfHeader.classList.remove('hidden');
html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false })
.then(canvas => {
pdfHeader.classList.add('hidden');
const imgData = canvas.toDataURL('image/jpeg', 0.95);
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
let heightLeft = imgHeight;
let position = 0;
pdf.addImage(imgData, 'JPEG', 0, position, pdfWidth, imgHeight);
heightLeft -= pdfHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(imgData, 'JPEG', 0, position, pdfWidth, imgHeight);
heightLeft -= pdfHeight;
}
pdf.save('Alternative-Payment-Methods.pdf');
loadingOverlay.style.display = 'none';
}).catch(err => {
console.error("PDF generation failed:", err);
pdfHeader.classList.add('hidden');
loadingOverlay.style.display = 'none';
alert('An error occurred generating the PDF.');
});
};
// --- TAB NAVIGATION & INITIALIZATION ---
const switchTab = (tabIndex) => {
activeTabIndex = tabIndex;
document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex));
document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex));
updateNavButtons();
};
const updateNavButtons = () => {
prevTabBtn.disabled = activeTabIndex === 0;
nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1;
};
const initializeUI = () => {
const tabs = [
{ name: 'Finder Dashboard', id: 'finder-dashboard' },
{ name: 'Data Configuration', id: 'data-configuration' },
];
tabIdentifiers = tabs.map(t => t.id);
tabsContainer.innerHTML = tabs.map(tab => ``).join('');
mainContent.innerHTML = tabs.map(tab => ``).join('');
tabs.forEach((tab, index) => {
document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index));
});
renderFinderDashboard();
renderConfig();
switchTab(0);
lucide.createIcons();
};
initializeUI();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});
${method.name}
${method.popularity}${method.type}
${method.description}
Alternative Payment Methods Report
Criteria: ${lastSearchCriteria.country || 'All Countries'}, ${lastSearchCriteria.industry || 'All Industries'}
Methods Found
${lastSearchResults.length}
Most Popular Type
${topType}
${resultsHtml}
${methodRows}
