`;
}
analysisEl.innerHTML = html;
}
if (switchToReport) {
aiph_showTab('reportTab');
}
}
/**
* Shows a specific tab and hides others.
* @param {string} tabId - The ID of the tab pane to show.
*/
window.aiph_showTab = function(tabId) {
// Null check
if (!document.getElementById(tabId)) {
console.error(`AIPH Error: Tab content #${tabId} not found.`);
return;
}
aiph_currentTabId = tabId;
const panes = document.querySelectorAll('#aiph-container .aiph-tab-pane');
const links = document.querySelectorAll('#aiph-container .aiph-tab-link');
panes.forEach(pane => {
pane.classList.remove('aiph-active');
});
links.forEach(link => {
link.classList.remove('aiph-active');
if (link.getAttribute('onclick') === `aiph_showTab('${tabId}')`) {
link.classList.add('aiph-active');
}
});
document.getElementById(tabId).classList.add('aiph-active');
}
/**
* Navigates between tabs using Next/Previous buttons.
* @param {string} direction - 'next' or 'prev'.
*/
window.aiph_navigateTabs = function(direction) {
const currentIndex = aiph_tabIds.indexOf(aiph_currentTabId);
let newIndex;
if (direction === 'next') {
newIndex = (currentIndex + 1) % aiph_tabIds.length;
} else {
newIndex = (currentIndex - 1 + aiph_tabIds.length) % aiph_tabIds.length;
}
aiph_showTab(aiph_tabIds[newIndex]);
}
/**
* Generates and downloads a PDF of the report content.
*/
window.aiph_downloadPDF = async function() {
const pdfContent = document.getElementById('aiph-pdf-content');
const container = document.getElementById('aiph-container');
if (!pdfContent || !container) {
console.error("AIPH Error: PDF generation elements not found.");
alert("Error: Could not find content to export.");
return;
}
if (!window.html2canvas || !window.jspdf) {
console.error("AIPH Error: jsPDF or html2canvas library not loaded.");
alert("Error: PDF libraries not loaded. Please check your internet connection.");
return;
}
// Add class to hide button during capture
container.classList.add('aiph-pdf-export');
try {
const canvas = await html2canvas(pdfContent, {
scale: 2, // Improve resolution
useCORS: true,
backgroundColor: '#ffffff'
});
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'p',
unit: 'px',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgWidth = canvas.width;
const imgHeight = canvas.height;
const ratio = imgHeight / imgWidth;
let finalImgHeight = pdfWidth * ratio;
let finalImgWidth = pdfWidth;
// Check if it fits, if not, scale by height
if (finalImgHeight > pdfHeight - 20) { // -20 for margins
finalImgHeight = pdfHeight - 20;
finalImgWidth = finalImgHeight / ratio;
}
const xPos = (pdfWidth - finalImgWidth) / 2;
const yPos = 10; // Top margin
pdf.addImage(imgData, 'PNG', xPos, yPos, finalImgWidth, finalImgHeight);
pdf.save('Academic_Integrity_Report.pdf');
} catch (error) {
console.error("AIPH Error generating PDF:", error);
alert("An error occurred while generating the PDF.");
} finally {
// Remove the class to show the button again
container.classList.remove('aiph-pdf-export');
}
}
})();
