`;
previewContainer.innerHTML = html;
}
// --- Interaction Logic ---
// Global exposure for inline onclick events
window.removeResolution = function(index) {
state.resolutions.splice(index, 1);
renderResolutionsInputs();
renderPreview();
};
window.updateResolution = function(index, field, value) {
state.resolutions[index][field] = value;
renderPreview();
};
window.removeSignatory = function(index) {
state.signatories.splice(index, 1);
renderSignatoriesInputs();
renderPreview();
};
window.updateSignatory = function(index, field, value) {
state.signatories[index][field] = value;
renderPreview();
};
document.getElementById('srd-add-resolution').addEventListener('click', () => {
state.resolutions.push({ title: "New Resolution", content: "RESOLVED, that..." });
renderResolutionsInputs();
renderPreview();
});
document.getElementById('srd-add-signatory').addEventListener('click', () => {
state.signatories.push({ name: "", title: "Shareholder" });
renderSignatoriesInputs();
renderPreview();
});
// Form Inputs Listeners
inputName.addEventListener('input', (e) => { state.companyName = e.target.value; renderPreview(); });
inputState.addEventListener('change', (e) => { state.companyState = e.target.value; renderPreview(); });
inputDate.addEventListener('change', (e) => { state.date = e.target.value; renderPreview(); });
inputLocation.addEventListener('input', (e) => { state.location = e.target.value; renderPreview(); });
inputQuorum.addEventListener('input', (e) => { state.quorum = e.target.value; renderPreview(); });
inputType.addEventListener('change', (e) => {
state.type = e.target.value;
// Show/Hide Location and Quorum based on type
const locGroup = document.getElementById('srd-location-group');
const quorGroup = document.getElementById('srd-quorum-group');
if (state.type === 'consent') {
locGroup.style.display = 'none';
quorGroup.style.display = 'none';
} else {
locGroup.style.display = 'block';
quorGroup.style.display = 'block';
}
renderPreview();
});
// Initial Render
inputDate.value = state.date;
renderResolutionsInputs();
renderSignatoriesInputs();
renderPreview();
// --- Tab & Nav Logic ---
let currentTab = 0;
function showTab(index) {
tabs.forEach((t, i) => {
if (i === index) {
t.classList.add('active');
contents[i].classList.add('active');
} else {
t.classList.remove('active');
contents[i].classList.remove('active');
}
});
currentTab = index;
prevBtn.disabled = currentTab === 0;
nextBtn.disabled = currentTab === tabs.length - 1;
}
tabs.forEach((tab, index) => {
tab.addEventListener('click', () => showTab(index));
});
prevBtn.addEventListener('click', () => {
if (currentTab > 0) showTab(currentTab - 1);
});
nextBtn.addEventListener('click', () => {
if (currentTab < tabs.length - 1) showTab(currentTab + 1);
});
// --- PDF Download ---
document.getElementById('srd-download-pdf').addEventListener('click', () => {
const { jsPDF } = window.jspdf;
const element = document.getElementById('srd-document-preview');
html2canvas(element, {
scale: 2, // Higher quality
useCORS: true,
backgroundColor: '#ffffff'
}).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'pt', 'letter'); // Letter size for US context
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
// Calculate aspect ratio
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
// Add margin logic if needed, but full width looks best for the "paper" effect
const margin = 40;
const finalWidth = pdfWidth - (margin * 2);
const finalHeight = (imgProps.height * finalWidth) / imgProps.width;
// Logic for multi-page if content is very long (basic implementation)
if (finalHeight > pdfHeight) {
// For this tool, we will just scale to fit or let it run off.
// Advanced multi-page splitting for html2canvas is complex,
// so we default to "Scale to Fit One Page" or standard placement.
// Here we fit width and let height flow.
}
pdf.addImage(imgData, 'PNG', margin, margin, finalWidth, finalHeight);
pdf.save(`${state.companyName.replace(/ /g, '_')}_Resolution.pdf`);
});
});
});
