`;
resultsTable.innerHTML = tableHtml;
}
// Sort Table
window.sortTable = function (n) {
const table = document.querySelector('.results-table');
if (!table) return;
let rows, switching = true, i, x, y, shouldSwitch, dir = 'asc', switchcount = 0;
while (switching) {
switching = false;
rows = table.rows;
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName('TD')[n];
y = rows[i + 1].getElementsByTagName('TD')[n];
let xVal = x.innerHTML.toLowerCase();
let yVal = y.innerHTML.toLowerCase();
if (n === 3 || n === 4 || n === 5) {
xVal = parseFloat(xVal.replace('$', '').replace('%', '')) || 0;
yVal = parseFloat(yVal.replace('$', '').replace('%', '')) || 0;
} else if (n === 7) {
xVal = new Date(xVal);
yVal = new Date(yVal);
}
if (dir === 'asc') {
if (xVal > yVal) {
shouldSwitch = true;
break;
}
} else if (dir === 'desc') {
if (xVal < yVal) {
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount++;
} else if (switchcount === 0 && dir === 'asc') {
dir = 'desc';
switching = true;
}
}
};
// Filter Results
window.filterResults = function () {
const statusFilter = document.getElementById('filter-status')?.value;
const sectorFilter = document.getElementById('filter-sector')?.value;
const filteredProjects = projects.filter(p => {
const statusMatch = statusFilter === 'all' || p.status === statusFilter;
const sectorMatch = sectorFilter === 'all' || p.sector === sectorFilter;
return statusMatch && sectorMatch;
});
displayResults(filteredProjects);
};
// Tab Navigation
window.openTab = function (tabId) {
const tabs = document.querySelectorAll('.tab-content');
const tabLinks = document.querySelectorAll('.tab-link');
tabs.forEach(tab => tab.classList.remove('active'));
tabLinks.forEach(link => link.classList.remove('active'));
const activeTab = document.getElementById(tabId);
if (activeTab) {
activeTab.classList.add('active');
const activeLink = document.querySelector(`.tab-link[onclick="openTab('${tabId}')"]`);
if (activeLink) activeLink.classList.add('active');
}
if (tabId === 'results-tab') {
displayResults(projects);
}
};
// Download PDF
window.downloadPDF = function () {
const resultsTable = document.getElementById('results-table');
const pdfContent = resultsTable.querySelector('.pdf-content');
if (!resultsTable || !pdfContent || !pdfContent.querySelector('.results-table')) {
alert('No results available to download. Please add projects first.');
return;
}
// Check if pdfMake is available
if (typeof pdfMake === 'undefined') {
alert('PDF generation library failed to load. Please check your internet connection and try again.');
console.error('pdfMake is not defined. Library may have failed to load.');
return;
}
try {
// Extract table data
const rows = [];
const headers = ['Project Name', 'Project ID', 'Type', 'Funds Raised (USD)', 'Token Price (USD)', 'ROI (%)', 'ESG Score', 'Launch Date', 'Status', 'Sector'];
rows.push(headers.map(header => ({ text: header, style: 'tableHeader' })));
const tableRows = pdfContent.querySelectorAll('.results-table tbody tr');
if (tableRows.length === 0) {
throw new Error('No table rows found in results.');
}
tableRows.forEach(row => {
const cells = row.querySelectorAll('td');
const rowData = Array.from(cells).map(cell => ({ text: cell.textContent }));
rows.push(rowData);
});
// Extract summary and recommendations
const totalInvestment = pdfContent.querySelector('.summary p:nth-child(1)')?.textContent || 'N/A';
const avgROI = pdfContent.querySelector('.summary p:nth-child(2)')?.textContent || 'N/A';
const recommendations = pdfContent.querySelector('.summary p:nth-child(4)')?.textContent || 'N/A';
// Define PDF document structure
const docDefinition = {
content: [
{ text: 'ICO/IDO Tracker Report', style: 'header' },
{ text: '\n' },
{
table: {
headerRows: 1,
widths: ['*', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto'],
body: rows
},
layout: {
fillColor: function (rowIndex) {
return (rowIndex % 2 === 0 && rowIndex > 0) ? '#f2f2f2' : null;
},
hLineColor: '#ddd',
vLineColor: '#ddd'
}
},
{ text: '\n' },
{ text: 'Summary', style: 'subheader' },
{ text: totalInvestment, style: 'body' },
{ text: avgROI, style: 'body' },
{ text: '\n' },
{ text: 'Recommendations', style: 'subheader' },
{ text: recommendations, style: 'body' }
],
styles: {
header: {
fontSize: 18,
bold: true,
alignment: 'center',
color: '#333'
},
subheader: {
fontSize: 14,
bold: true,
color: '#333'
},
tableHeader: {
bold: true,
fontSize: 12,
color: 'white',
fillColor: '#007BFF'
},
body: {
fontSize: 10,
color: '#333'
}
},
defaultStyle: {
fontSize: 10,
color: '#333'
},
pageMargins: [40, 60, 40, 60]
};
// Generate and download PDF
pdfMake.createPdf(docDefinition).download('ICO_IDO_Tracker_Report.pdf');
} catch (err) {
console.error('PDF generation failed:', err.message);
alert('Failed to generate PDF due to an internal error. Please ensure results are valid and try again.');
}
};
// Initial display
displayResults(projects);
});