LICENSEE
${data.licenseeName}
`;
}
/**
* Displays a specific tab based on index 'n'.
* Globally scoped for HTML onclick attributes.
*/
function showTab(n) {
// Ensure elements are loaded before manipulating them
if (!tabs || !tabButtons || !prevBtn || !nextBtn) return;
tabs.forEach((tab, index) => {
tab.style.display = (index === n) ? 'block' : 'none';
});
tabButtons.forEach((btn, index) => {
btn.classList.toggle('active', index === n);
});
prevBtn.disabled = (n === 0);
nextBtn.style.display = (n === (tabs.length - 1)) ? 'none' : 'inline-block';
if (n === tabs.length - 1) {
generateAgreementPreview();
}
currentTab = n;
}
/**
* Handles Next/Previous button clicks.
* Globally scoped for HTML onclick attributes.
*/
function navigateTabs(n) {
if(!tabs || !tabs[currentTab]) return;
tabs[currentTab].style.display = 'none';
currentTab = currentTab + n;
if (currentTab >= tabs.length) {
currentTab = tabs.length - 1;
}
if (currentTab < 0) {
currentTab = 0;
}
showTab(currentTab);
}
document.addEventListener('DOMContentLoaded', function() {
// Assign DOM elements to the globally scoped variables
tabs = document.querySelectorAll('.tab-content');
tabButtons = document.querySelectorAll('.tab-button');
prevBtn = document.getElementById('prevBtn');
nextBtn = document.getElementById('nextBtn');
const downloadPdfBtn = document.getElementById('downloadPdfBtn');
const agreementTermSelect = document.getElementById('agreementTerm');
const feeTypeSelect = document.getElementById('feeType');
// --- LOCAL HELPER FUNCTIONS ---
function handleTermChange() {
const endDateContainer = document.getElementById('endDateContainer');
if (!endDateContainer) return;
if (agreementTermSelect.value === 'Fixed') {
endDateContainer.style.display = 'block';
} else {
endDateContainer.style.display = 'none';
}
}
function updateFeeDetails() {
const feeDetailsContainer = document.getElementById('feeDetailsContainer');
if (!feeDetailsContainer || !feeTypeSelect) return;
const feeType = feeTypeSelect.value;
let html = '';
if (feeType === 'One-time Payment') {
html = `
`;
} else if (feeType === 'Royalty') {
html = `
`;
} else if (feeType === 'Recurring Fee') {
html = `
`;
}
feeDetailsContainer.innerHTML = html;
}
async function generatePdf() {
const loader = document.getElementById('pdf-loader');
const downloadBtn = document.getElementById('downloadPdfBtn');
if (!loader || !downloadBtn) return;
loader.classList.remove('hidden');
downloadBtn.disabled = true;
const content = document.getElementById('pdf-content-area');
await new Promise(resolve => setTimeout(resolve, 100));
try {
const canvas = await html2canvas(content, { scale: 2, useCORS: true, logging: false });
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'letter' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const margin = 40;
const imgProps = pdf.getImageProperties(imgData);
const imgWidth = pdfWidth - (margin * 2);
const imgHeight = (imgProps.height * imgWidth) / imgProps.width;
let heightLeft = imgHeight;
let position = margin;
pdf.addImage(imgData, 'PNG', margin, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - (margin * 2));
while (heightLeft > 0) {
position = heightLeft - imgHeight + margin;
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - (margin * 2));
}
pdf.save('Licensing-Agreement.pdf');
} catch (error) {
console.error("Error generating PDF:", error);
} finally {
loader.classList.add('hidden');
downloadBtn.disabled = false;
}
}
// --- EVENT LISTENERS ---
if(agreementTermSelect) agreementTermSelect.addEventListener('change', handleTermChange);
if(feeTypeSelect) feeTypeSelect.addEventListener('change', updateFeeDetails);
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', generatePdf);
}
// --- INITIALIZATION ---
showTab(currentTab);
updateFeeDetails();
const effectiveDateEl = document.getElementById('effectiveDate');
if(effectiveDateEl) effectiveDateEl.valueAsDate = new Date();
});