${formatCurrency(r.cost)}
${tags}
`;
}).join('');
resultsContainer.innerHTML = `
Available Rates
${resultsHtml}
`;
lucide.createIcons();
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderConfiguration = () => {
const configContent = document.getElementById('content-config');
if (!configContent) return;
const carriersHtml = appData.carriers.map((carrier, cIndex) => `
`).join('');
configContent.innerHTML = `
Carrier Rate Configuration
`;
document.getElementById('save-config-btn').addEventListener('click', handleConfigSave);
};
const handleConfigSave = (e) => {
e.preventDefault();
document.querySelectorAll('#config-form input').forEach(input => {
const cIndex = input.dataset.carrier;
const sIndex = input.dataset.service;
const field = input.dataset.field;
appData.carriers[cIndex].services[sIndex][field] = parseFloat(input.value);
});
appData.handlingFee.type = document.getElementById('handling-fee-type').value;
appData.handlingFee.value = parseFloat(document.getElementById('handling-fee-value').value);
alert('Configuration saved!');
};
const generatePdf = () => {
if (!calculationResult) return;
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
let y = 40;
pdf.setFontSize(18).setFont('helvetica', 'bold').text('Shipping Cost Analysis Report', pdf.internal.pageSize.getWidth() / 2, y, { align: 'center' });
y += 30;
pdf.setFontSize(12).setFont('helvetica', 'bold').text('Shipment Details', 40, y);
y += 15;
pdf.autoTable({
startY: y,
theme: 'plain',
body: [
['Origin ZIP:', calculationInputs.originZip, 'Weight:', `${calculationInputs.weight} lbs`],
['Destination ZIP:', calculationInputs.destZip, 'Dimensions:', `${calculationInputs.length}x${calculationInputs.width}x${calculationInputs.height} in`],
]
});
y = pdf.autoTable.previous.finalY + 30;
pdf.autoTable({
startY: y,
head: [['Carrier', 'Service', 'Est. Delivery', 'Cost']],
body: calculationResult.results.map(r => [r.carrier, r.service, r.deliveryTime, formatCurrency(r.cost)]),
theme: 'grid',
headStyles: { fillColor: [13, 148, 136] } // Teal header
});
pdf.save(`Shipping-Cost-Report.pdf`);
loadingOverlay.style.display = 'none';
};
// --- TAB NAVIGATION & INITIALIZATION ---
const switchTab = (tabIndex) => {
activeTabIndex = tabIndex;
document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex));
document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex));
updateNavButtons();
};
const updateNavButtons = () => {
prevTabBtn.disabled = activeTabIndex === 0;
nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1;
};
const initializeUI = () => {
const tabs = [
{ name: 'Rate Calculator', id: 'calculator' },
{ name: 'Rate Configuration', id: 'config' }
];
tabIdentifiers = tabs.map(t => t.id);
tabsContainer.innerHTML = tabs.map(tab => `
`).join('');
mainContent.innerHTML = tabs.map(tab => `
`).join('');
tabs.forEach((tab, index) => {
document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index));
});
renderCalculator();
renderConfiguration();
switchTab(0);
lucide.createIcons();
};
initializeUI();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});