`;
dashboard.container.appendChild(itemCard);
});
}
};
const saveAndRender = () => {
localStorage.setItem('priceAlertItems', JSON.stringify(trackedItems));
renderItems();
};
const loadItems = () => {
const storedItems = localStorage.getItem('priceAlertItems');
if (storedItems) {
trackedItems = JSON.parse(storedItems);
}
renderItems();
};
const addAlert = () => {
const url = wizard.inputs.url.value.trim();
const price = parseFloat(wizard.inputs.price.value).toFixed(2);
const email = wizard.inputs.email.value.trim();
if (!url || !price || !email || price <= 0) {
alert('Please fill all fields with valid data.'); // Using alert as a simple notification for this tool
return;
}
trackedItems.push({ url, price, email });
saveAndRender();
// Reset form
wizard.inputs.url.value = '';
wizard.inputs.price.value = '';
wizard.inputs.email.value = '';
navigateWizard(wizard.step3, wizard.step1);
};
const handleDownloadPDF = () => {
if(trackedItems.length === 0) return;
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const timestamp = new Date().toLocaleString();
const pageHeight = doc.internal.pageSize.getHeight();
const pageWidth = doc.internal.pageSize.getWidth();
const margin = 40;
let yPos = margin;
doc.setFont('helvetica', 'bold');
doc.setFontSize(20);
doc.setTextColor('#ffffff');
doc.setFillColor(30, 41, 59); // slate-800
doc.rect(0, 0, pageWidth, pageHeight, 'F');
doc.text('Price Alert Summary', pageWidth / 2, yPos, { align: 'center' });
yPos += 25;
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
doc.setTextColor('#94a3b8'); // slate-400
doc.text(`Generated on: ${timestamp}`, pageWidth / 2, yPos, { align: 'center' });
yPos += 50;
trackedItems.forEach(item => {
const domain = new URL(item.url).hostname.replace('www.', '');
if (yPos > pageHeight - 60) {
doc.addPage();
doc.setFillColor(30, 41, 59);
doc.rect(0, 0, pageWidth, pageHeight, 'F');
yPos = margin;
}
doc.setFont('helvetica', 'bold');
doc.setFontSize(12);
doc.setTextColor('#e2e8f0'); // slate-200
doc.text(`Product from: ${domain}`, margin, yPos);
yPos += 18;
doc.setFont('courier', 'normal');
doc.setFontSize(10);
doc.setTextColor('#94a3b8');
doc.text(`Target Price: $${item.price}`, margin + 10, yPos);
yPos += 15;
doc.text(`Email Alert: ${item.email}`, margin + 10, yPos);
yPos += 30;
});
doc.save('price-alert-summary.pdf');
};
// Event Listeners
wizard.nav.next1.addEventListener('click', () => navigateWizard(wizard.step1, wizard.step2));
wizard.nav.prev2.addEventListener('click', () => navigateWizard(wizard.step2, wizard.step1));
wizard.nav.next2.addEventListener('click', () => navigateWizard(wizard.step2, wizard.step3));
wizard.nav.prev3.addEventListener('click', () => navigateWizard(wizard.step3, wizard.step2));
wizard.nav.submit.addEventListener('click', addAlert);
dashboard.pdfButton.addEventListener('click', handleDownloadPDF);
dashboard.container.addEventListener('click', (e) => {
if(e.target.classList.contains('remove-btn')) {
const index = parseInt(e.target.dataset.index, 10);
trackedItems.splice(index, 1);
saveAndRender();
}
});
// Initial Load
loadItems();
});
