Potential Profit
$${opp.profit.toFixed(2)} / unit
`;
resultsContainer.appendChild(card);
});
downloadPdfBtn.disabled = false;
};
// --- CORE LOGIC ---
const findArbitrage = () => {
const productsBySku = productPrices.reduce((acc, item) => {
acc[item.sku] = acc[item.sku] || [];
acc[item.sku].push(item);
return acc;
}, {});
const opportunities = [];
for (const sku in productsBySku) {
const items = productsBySku[sku];
if (items.length < 2) continue;
const lowest = items.reduce((prev, curr) => (prev.price < curr.price) ? prev : curr);
const highest = items.reduce((prev, curr) => (prev.price > curr.price) ? prev : curr);
if (highest.price > lowest.price) {
opportunities.push({
sku: sku,
name: items[0].name,
lowest: lowest,
highest: highest,
profit: highest.price - lowest.price
});
}
}
return opportunities.sort((a, b) => b.profit - a.profit);
};
// --- UI & EVENT HANDLERS ---
const switchTab = (tabId) => {
currentTab = tabId;
Object.values(tabPanes).forEach(pane => pane.classList.add('hidden'));
tabPanes[tabId].classList.remove('hidden');
Object.values(tabButtons).forEach(btn => btn.classList.replace('tab-active', 'tab-inactive'));
tabButtons[tabId].classList.replace('tab-inactive', 'tab-active');
updateNavButtons();
};
const navigateTabs = (direction) => {
const currentIndex = tabs.indexOf(currentTab);
const newIndex = direction === 'next' ? currentIndex + 1 : currentIndex - 1;
if (newIndex >= 0 && newIndex < tabs.length) {
switchTab(tabs[newIndex]);
}
};
const updateNavButtons = () => {
const currentIndex = tabs.indexOf(currentTab);
prevBtn.disabled = currentIndex === 0;
nextBtn.disabled = currentIndex === tabs.length - 1;
prevBtn.classList.toggle('opacity-50', prevBtn.disabled);
nextBtn.classList.toggle('opacity-50', nextBtn.disabled);
};
const handleFindArbitrage = () => {
findArbitrageBtnSpinner.classList.remove('hidden');
findArbitrageBtnText.textContent = 'Analyzing...';
findArbitrageBtn.disabled = true;
setTimeout(() => {
const opportunities = findArbitrage();
renderArbitrageResults(opportunities);
findArbitrageBtnSpinner.classList.add('hidden');
findArbitrageBtnText.textContent = 'Find Arbitrage Opportunities';
findArbitrageBtn.disabled = false;
}, 500); // Simulate processing
};
const handlePdfDownload = () => {
const opportunities = findArbitrage();
if(opportunities.length === 0) return;
// Build clean HTML for PDF
let pdfHtml = `
Product Price Arbitrage Report
| Product (SKU) |
Buy From (Supplier / Price) |
Highest Price (Supplier / Price) |
Profit/Unit |
${opportunities.map(opp => `
${opp.name} ${opp.sku} |
${opp.lowest.supplier} $${opp.lowest.price.toFixed(2)} |
${opp.highest.supplier} $${opp.highest.price.toFixed(2)} |
$${opp.profit.toFixed(2)} |
`).join('')}
`;
pdfRenderContainer.innerHTML = pdfHtml;
html2canvas(pdfRenderContainer, { scale: 2 }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save('Wholesale-Arbitrage-Report.pdf');
});
};
// --- EVENT LISTENERS ---
window.switchTab = switchTab;
window.navigateTabs = navigateTabs;
findArbitrageBtn.addEventListener('click', handleFindArbitrage);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
addPriceBtn.addEventListener('click', () => {
const newPriceEntry = {
id: nextId++,
sku: 'NEW-SKU',
name: 'New Product',
supplier: 'New Supplier',
price: 0.00
};
productPrices.push(newPriceEntry);
renderConfigTable();
});
configTableBody.addEventListener('input', e => {
if (e.target.classList.contains('config-input')) {
const id = parseInt(e.target.closest('tr').dataset.id);
const prop = e.target.dataset.prop;
const item = productPrices.find(p => p.id === id);
if (item) {
const value = e.target.type === 'number' ? parseFloat(e.target.value) || 0 : e.target.value;
item[prop] = value;
}
}
});
configTableBody.addEventListener('click', e => {
if (e.target.classList.contains('remove-row-btn')) {
const idToRemove = parseInt(e.target.closest('tr').dataset.id);
productPrices = productPrices.filter(p => p.id !== idToRemove);
renderConfigTable();
}
});
// --- INITIALIZATION ---
renderConfigTable();
updateNavButtons();
switchTab('dashboard');
});