Profit Margin
${profitMargin.toFixed(1)}%
Break-Even Point
${breakEvenUnits} sales / month
to cover ${formatCurrency(appData.config.monthlyCosts)} in fixed costs
`;
renderCostChart();
attachCalcListeners();
};
const renderCostChart = () => {
const ctx = document.getElementById('cost-breakdown-chart').getContext('2d');
if (charts.cost) charts.cost.destroy();
const { costBreakdown, revenue } = appData.results;
charts.cost = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['Product', 'Shipping', 'Ads', 'Fees', 'Returns', 'Net Profit'],
datasets: [{
data: Object.values(costBreakdown),
backgroundColor: ['#60a5fa', '#38bdf8', '#f87171', '#fbbf24', '#fca5a5', '#34d399']
}]
},
options: { responsive: true, maintainAspectRatio: false, plugins: { title: { display: true, text: `Cost & Profit Breakdown of ${formatCurrency(revenue)} Sale` }, legend: { position: 'bottom' } } }
});
};
const renderDataConfig = () => {
const configContent = document.getElementById('content-data-configuration');
if (!configContent) return;
configContent.innerHTML = `
Business Cost Assumptions
`;
attachConfigListeners();
};
// --- EVENT HANDLERS & LOGIC ---
const attachCalcListeners = () => {
const handler = () => {
appData.inputs.productCost = parseFloat(document.getElementById('product-cost').value) || 0;
appData.inputs.shippingCost = parseFloat(document.getElementById('shipping-cost').value) || 0;
appData.inputs.retailPrice = parseFloat(document.getElementById('retail-price').value) || 0;
appData.inputs.adSpendPerSale = parseFloat(document.getElementById('ad-spend').value) || 0;
calculateProfit();
renderCalculator();
};
document.querySelectorAll('.calc-input').forEach(input => input.addEventListener('input', handler));
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const attachConfigListeners = () => {
document.querySelectorAll('.config-input').forEach(input => {
input.addEventListener('change', e => {
const field = e.target.dataset.field;
appData.config[field] = parseFloat(e.target.value) || 0;
appData.results = null; // force recalculation
renderCalculator();
});
});
};
const generatePdf = () => {
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
document.getElementById('pdf-generated-date').textContent = new Date().toLocaleString();
const pdfHeader = document.getElementById('pdf-header');
pdfHeader.classList.remove('hidden');
const fullContent = document.getElementById('pdf-content-area');
html2canvas(fullContent, { scale: 2, useCORS: true, logging: false, windowWidth: 1200 })
.then(canvas => {
pdfHeader.classList.add('hidden');
const imgData = canvas.toDataURL('image/jpeg', 0.9);
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, imgHeight);
pdf.save('Dropshipping-Profit-Report.pdf');
loadingOverlay.style.display = 'none';
}).catch(err => {
console.error("PDF generation failed:", err);
pdfHeader.classList.add('hidden');
loadingOverlay.style.display = 'none';
alert('An error occurred generating the PDF.');
});
};
// --- 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: 'Profit Calculator', id: 'profit-calculator' },
{ name: 'Data Configuration', id: 'data-configuration' }
];
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();
renderDataConfig();
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); });
});