`;
});
caseDiv.innerHTML = caseHTML;
researchLog.appendChild(caseDiv);
}
lucide.createIcons();
};
// --- FORM HANDLING ---
const resetForm = () => {
researchForm.reset();
document.getElementById('findingId').value = '';
document.getElementById('dateFound').valueAsDate = new Date();
formTitle.textContent = 'Add New Research Finding';
};
const populateForm = (id) => {
const finding = researchFindings.find(f => f.id === id);
if (finding) {
document.getElementById('findingId').value = finding.id;
document.getElementById('caseName').value = finding.caseName;
document.getElementById('citation').value = finding.citation;
document.getElementById('dateFound').value = finding.dateFound;
document.getElementById('summary').value = finding.summary;
document.getElementById('relevance').value = finding.relevance;
formTitle.textContent = 'Edit Research Finding';
switchTab(1);
}
};
researchForm.addEventListener('submit', (e) => {
e.preventDefault();
const id = document.getElementById('findingId').value;
const findingData = {
caseName: document.getElementById('caseName').value.trim(),
citation: document.getElementById('citation').value.trim(),
dateFound: document.getElementById('dateFound').value,
summary: document.getElementById('summary').value.trim(),
relevance: document.getElementById('relevance').value.trim(),
};
if (id) { // Editing
const index = researchFindings.findIndex(f => f.id == id);
researchFindings[index] = { ...researchFindings[index], ...findingData };
} else { // Adding new
findingData.id = nextId++;
researchFindings.push(findingData);
}
renderResearchLog();
resetForm();
switchTab(0);
});
// --- EVENT LISTENERS ---
tabs.forEach((tab, index) => tab.btn.addEventListener('click', () => switchTab(index)));
addNewBtn.addEventListener('click', () => { resetForm(); switchTab(1); });
cancelBtn.addEventListener('click', () => { resetForm(); switchTab(0); });
researchLog.addEventListener('click', (e) => {
const editBtn = e.target.closest('.edit-btn');
const deleteBtn = e.target.closest('.delete-btn');
if (editBtn) populateForm(parseInt(editBtn.dataset.id));
if (deleteBtn) {
const id = parseInt(deleteBtn.dataset.id);
if (confirm('Are you sure you want to delete this research finding?')) {
researchFindings = researchFindings.filter(f => f.id !== id);
renderResearchLog();
}
}
});
// --- PDF GENERATION ---
downloadPdfBtn.addEventListener('click', () => {
const { jsPDF } = window.jspdf;
const pdfExportContainer = document.getElementById('pdfExportContainer');
const groupedByCase = researchFindings.reduce((acc, finding) => {
(acc[finding.caseName] = acc[finding.caseName] || []).push(finding);
return acc;
}, {});
let exportHTML = `
Legal Research Memo
Date Generated: ${new Date().toLocaleDateString()}
`;
for (const caseName in groupedByCase) {
exportHTML += `
Case / Matter: ${caseName}
`;
groupedByCase[caseName].forEach(finding => {
exportHTML += `
Citation: ${finding.citation}
Date Found: ${finding.dateFound}
Summary: ${finding.summary}
Relevance: ${finding.relevance}
`;
});
}
exportHTML += `
`;
pdfExportContainer.innerHTML = exportHTML;
html2canvas(document.getElementById('pdfExportContent'), { scale: 2, useCORS: true }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / canvasHeight;
const imgWidth = pdfWidth - 20; // with margin
const imgHeight = imgWidth / ratio;
let heightLeft = imgHeight;
let position = 10;
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= (pdf.internal.pageSize.getHeight() - 20);
while (heightLeft > 0) {
position -= (pdf.internal.pageSize.getHeight() - 20);
pdf.addPage();
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= (pdf.internal.pageSize.getHeight() - 20);
}
pdf.save('legal_research_memo.pdf');
pdfExportContainer.innerHTML = ''; // Clean up
});
});
// --- INITIALIZE APP ---
renderResearchLog();
switchTab(0);
document.getElementById('dateFound').valueAsDate = new Date();
});