import { getReceipts, getSettings, saveSettings, clearAllReceipts } from './utils/storage.js'; import { getCategoryLabel } from './utils/categories.js'; import { exportToCsv, downloadBlob } from './utils/export.js'; import type { Receipt, AppSettings } from './types.js'; const nameInput = document.getElementById('s-name') as HTMLInputElement; const currencySelect = document.getElementById('s-currency') as HTMLSelectElement; const yearInput = document.getElementById('s-year') as HTMLInputElement; const saveBtn = document.getElementById('save-settings') as HTMLButtonElement; const sumCount = document.getElementById('sum-count') as HTMLSpanElement; const sumTotal = document.getElementById('sum-total') as HTMLSpanElement; const tableWrap = document.getElementById('receipts-table-wrap') as HTMLDivElement; const toast = document.getElementById('toast') as HTMLDivElement; let currentReceipts: Receipt[] = []; async function init() { const settings = await getSettings(); nameInput.value = settings.ownerName || ''; currencySelect.value = settings.defaultCurrency || 'USD'; yearInput.value = settings.taxYear || new Date().getFullYear().toString(); await loadData(); saveBtn.addEventListener('click', async () => { const settings: AppSettings = { ownerName: nameInput.value.trim(), defaultCurrency: currencySelect.value, taxYear: yearInput.value.trim(), }; await saveSettings(settings); showToast('Settings saved'); }); document.getElementById('export-csv')!.addEventListener('click', async () => { if (currentReceipts.length === 0) { showToast('No receipts to export'); return; } const csv = exportToCsv(currentReceipts); const year = yearInput.value || new Date().getFullYear(); downloadBlob(csv, `simplescan-receipts-${year}.csv`, 'text/csv'); showToast('CSV downloaded'); }); document.getElementById('export-pdf')!.addEventListener('click', async () => { if (currentReceipts.length === 0) { showToast('No receipts to export'); return; } exportPdf(currentReceipts); }); document.getElementById('clear-all')!.addEventListener('click', async () => { if (!confirm('Are you sure? This will delete ALL receipts forever.')) return; await clearAllReceipts(); await loadData(); showToast('All receipts deleted'); }); } async function loadData() { currentReceipts = await getReceipts(); sumCount.textContent = String(currentReceipts.length); const total = currentReceipts.reduce((s, r) => s + r.amount, 0); const currency = currentReceipts[0]?.currency || 'USD'; sumTotal.textContent = formatMoney(total, currency); if (currentReceipts.length === 0) { tableWrap.innerHTML = '
No receipts yet. Add some from the extension popup.
'; return; } tableWrap.innerHTML = `| Date | Vendor | Description | Category | Amount |
|---|---|---|---|---|
| ${escapeHtml(r.date)} | ${escapeHtml(r.vendor)} | ${escapeHtml(r.title)} | ${getCategoryLabel(r.category)} | ${formatMoney(r.amount, r.currency)} |
| Date | Vendor | Description | Category | Amount | Notes |
|---|---|---|---|---|---|
| ${escapeHtml(r.date)} | ${escapeHtml(r.vendor)} | ${escapeHtml(r.title)} | ${getCategoryLabel(r.category)} | ${formatMoney(r.amount, r.currency)} | ${escapeHtml(r.notes)} |
| Total | ${formatMoney(total, currency)} | ||||