`;
}
// --- Flow Control ---
function rhif_updateStepNav() {
document.querySelectorAll('.step-item').forEach(item => {
const stepNum = parseInt(item.dataset.step);
item.classList.toggle('active', stepNum === rhif_state.step);
});
}
function rhif_showStep(stepNum) {
rhif_state.step = stepNum;
document.querySelectorAll('.rhif-tab-content').forEach(content => content.classList.remove('active'));
document.getElementById(`step-${stepNum}`).classList.add('active');
rhif_updateStepNav();
if (stepNum === 4) {
rhif_renderResult();
}
}
window.rhif_showStep = rhif_showStep;
function rhif_handleNext(currentStep) {
let form;
let characteristic;
if (currentStep === 1) {
form = document.getElementById('luster-form');
characteristic = 'luster';
} else if (currentStep === 2) {
form = document.getElementById('hardness-form');
characteristic = 'hardness';
} else if (currentStep === 3) {
form = document.getElementById('cleavage-form');
characteristic = 'cleavage';
} else {
return;
}
if (!form.checkValidity()) {
form.reportValidity();
return;
}
const selectedValue = form.querySelector(`input[name="${characteristic}"]:checked`).value;
rhif_state.characteristics[characteristic] = selectedValue;
rhif_showStep(currentStep + 1);
}
// --- PDF Export ---
function rhif_downloadPDF() {
if (typeof jspdf === 'undefined' || typeof jspdf.plugin.autotable === 'undefined') {
alert('Error: PDF library not fully loaded for export.');
return;
}
rhif_renderResult(); // Ensure final render is current
const mineral = rhif_matchMineral();
const { jsPDF } = jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
doc.setFont('sans-serif', 'normal');
const margin = 40;
const pageWidth = doc.internal.pageSize.getWidth();
let yPos = margin;
// --- Header ---
doc.setFontSize(24);
doc.setFont('sans-serif', 'bold');
doc.setTextColor('#92400e'); // Amber-900
doc.text("Mineral Identification Report", pageWidth / 2, yPos, { align: 'center' });
yPos += 35;
// --- Result ---
doc.setFontSize(20);
doc.text(`Identified Mineral: ${mineral.name}`, margin, yPos);
yPos += 25;
doc.setFontSize(14);
doc.text(`Formula: ${mineral.formula}`, margin, yPos);
yPos += 30;
// --- Characteristics Table ---
const charData = [
['Luster:', rhif_state.characteristics.luster.toUpperCase()],
['Hardness:', rhif_state.characteristics.hardness.toUpperCase()],
['Cleavage:', rhif_state.characteristics.cleavage.toUpperCase()]
];
doc.autoTable({
body: charData,
startY: yPos,
theme: 'striped',
styles: { fontSize: 10, cellPadding: 5 },
headStyles: { fillColor: [245, 158, 11], textColor: 255 },
columnStyles: { 0: { fontStyle: 'bold', textColor: '#92400e' } },
margin: { left: margin, right: pageWidth - 200 - margin },
didDrawPage: function(data) { yPos = data.cursor.y; }
});
yPos = doc.lastAutoTable.finalY + 25;
// --- Description ---
doc.setFontSize(14);
doc.text("Mineral Description:", margin, yPos);
yPos += 15;
doc.setFontSize(10);
doc.setTextColor('#4b5563');
const descLines = doc.splitTextToSize(mineral.description, contentWidth);
doc.text(descLines, margin, yPos);
yPos += (descLines.length * 12) + 20;
doc.save(`Mineral_ID_Report_${mineral.name.replace(/\s/g, '_')}.pdf`);
}
// --- Initialization ---
document.addEventListener('DOMContentLoaded', () => {
// Attach listeners to buttons
document.getElementById('step1-next-btn').addEventListener('click', () => rhif_handleNext(1));
document.getElementById('step2-next-btn').addEventListener('click', () => rhif_handleNext(2));
document.getElementById('step3-next-btn').addEventListener('click', () => rhif_handleNext(3));
document.getElementById('rhif-download-pdf').addEventListener('click', rhif_downloadPDF);
});