`;
container.innerHTML = html;
}
function wsgSwitchTab(tabId) {
document.querySelectorAll('.wsg-tab-btn').forEach(b => b.classList.remove('active'));
document.querySelectorAll('.wsg-content').forEach(c => c.classList.remove('active'));
const idx = tabId === 'builder' ? 0 : 1;
document.querySelectorAll('.wsg-tab-btn')[idx].classList.add('active');
document.getElementById('wsg-' + tabId).classList.add('active');
if (tabId === 'preview') {
wsgRenderStatement();
}
}
function wsgLoadExample() {
if(!confirm("Overwrite current inputs with example vehicle accident data?")) return;
document.getElementById('inp-case-name').value = "State v. Doe / Vehicle Collision";
document.getElementById('inp-incident-datetime').value = "2025-10-20 at approx. 14:30 EST";
document.getElementById('inp-location').value = "Intersection of Elm Street and 5th Avenue";
document.getElementById('inp-witness-name').value = "Sarah K. Thompson";
document.getElementById('inp-dob').value = "1988-11-20";
document.getElementById('inp-address').value = "45 B Street, Anytown, USA 12345";
document.getElementById('inp-narrative').value = "1. I was standing at the bus stop on the northeast corner of 5th Avenue, looking south.
2. I observed a blue sedan approach the intersection at what appeared to be a high rate of speed (approx. 45 mph).
3. The traffic light for Elm Street had already turned red approximately 3 seconds prior to the sedan entering the intersection.
4. The sedan struck the white delivery truck, which had a green light, on the passenger side door. The collision was severe.
5. The sedan driver appeared to be holding a phone at the time of impact. I immediately called emergency services.";
wsgRenderStatement();
wsgSwitchTab('preview');
}
/* --- PDF Generation --- */
async function wsgGeneratePDF() {
wsgRenderStatement(); // Final render check
const data = {
caseName: document.getElementById('inp-case-name').value || "N/A",
witnessName: document.getElementById('inp-witness-name').value || "N/A",
narrative: document.getElementById('inp-narrative').value || "[No Narrative Provided]"
};
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'mm', 'a4');
const navy = [30, 58, 138];
let y = 20;
// 1. Header (Metadata Block)
doc.setFont("times", "bold");
doc.setFontSize(16);
doc.setTextColor(0, 0, 0);
doc.text("WITNESS STATEMENT", 105, y, { align: 'center' });
y += 5;
doc.setFontSize(10);
doc.text(`In the Matter of: ${data.caseName}`, 105, y, { align: 'center' });
y += 15;
// Helper to add structured text sections
const addSection = (title, content, startY, fontSize = 12) => {
if (startY > 270) { doc.addPage(); startY = 20; }
doc.setFont("times", "bold");
doc.setFontSize(fontSize);
doc.setTextColor(0, 0, 0);
doc.text(title, 14, startY);
startY += 8;
doc.setFont("times", "normal");
doc.setFontSize(12);
doc.setTextColor(50, 50, 50);
const splitContent = doc.splitTextToSize(content, 180);
doc.text(splitContent, 14, startY);
return startY + (splitContent.length * 6) + 10; // 6pt line height
};
// 2. Witness Identity & Context
y = addSection("Witness Name:", data.witnessName, y, 10);
y = addSection("Incident Details:", `Date: ${document.getElementById('inp-incident-datetime').value} | Location: ${document.getElementById('inp-location').value}`, y, 10);
// 3. Narrative (Double-spaced look achieved by 12pt font + 12pt line height)
y = addSection("III. CHRONOLOGICAL ACCOUNT", " ", y, 14);
// Process narrative line by line to preserve numbering and line breaks
const narrativeLines = data.narrative.split('\n').filter(line => line.trim() !== '');
doc.setFontSize(12);
doc.setLineHeightFactor(2.0); // Simulate double spacing
narrativeLines.forEach(line => {
if (y > 270) { doc.addPage(); y = 20; }
const splitLine = doc.splitTextToSize(line, 180);
doc.text(splitLine, 14, y);
y += splitLine.length * 12; // Advance by line height factor
});
doc.setLineHeightFactor(1.0); // Reset line height
y += 20;
// 4. Declaration
doc.setFillColor(240, 240, 240);
doc.rect(14, y, 182, 18, 'F');
doc.setFont("times", "bold");
doc.setFontSize(10);
doc.setTextColor(0, 0, 0);
doc.text("DECLARATION OF TRUTH:", 18, y + 6);
doc.setFont("times", "normal");
doc.text("I confirm that the facts stated in this witness statement are true and accurate to the best of my knowledge and belief.", 18, y + 12);
y += 40;
// 5. Signature Block
doc.setFontSize(10);
doc.text("Signed:", 14, y);
doc.line(40, y, 190, y);
y += 10;
doc.text(`Witness Name (Printed): ${data.witnessName}`, 14, y);
doc.text("Date Signed:", 140, y);
doc.save(`WitnessStatement_${data.witnessName.replace(/\s/g, '_')}.pdf`);
}
