/** * quote-view.js — Quote list rendering and actions * Analog to invoice-view.js */ import { formatDate } from '../utils/helpers.js'; let quotes = []; export async function loadQuotes() { try { const response = await fetch('/api/quotes'); quotes = await response.json(); renderQuotes(); } catch (error) { console.error('Error loading quotes:', error); } } export function getQuotesData() { return quotes; } export function renderQuotes() { const tbody = document.getElementById('quotes-list'); if (!tbody) return; tbody.innerHTML = quotes.map(quote => { const total = quote.has_tbd ? `$${parseFloat(quote.total).toFixed(2)}*` : `$${parseFloat(quote.total).toFixed(2)}`; return ` ${quote.quote_number} ${quote.customer_name || 'N/A'} ${formatDate(quote.quote_date)} ${total} `; }).join(''); if (quotes.length === 0) { tbody.innerHTML = `No quotes found.`; } } export function viewPDF(id) { window.open(`/api/quotes/${id}/pdf`, '_blank'); } export async function edit(id) { if (typeof window.openQuoteModal === 'function') { await window.openQuoteModal(id); } } export async function remove(id) { if (!confirm('Are you sure you want to delete this quote?')) return; try { const response = await fetch(`/api/quotes/${id}`, { method: 'DELETE' }); if (response.ok) loadQuotes(); else alert('Error deleting quote'); } catch (error) { console.error('Error:', error); alert('Error deleting quote'); } } export async function convertToInvoice(quoteId) { if (!confirm('Convert this quote to an invoice?')) return; try { const response = await fetch(`/api/quotes/${quoteId}/convert-to-invoice`, { method: 'POST' }); if (response.ok) { const invoice = await response.json(); alert(`Invoice ${invoice.invoice_number || '(Draft)'} created successfully!`); if (window.invoiceView) window.invoiceView.loadInvoices(); if (typeof window.showTab === 'function') window.showTab('invoices'); } else { const error = await response.json(); alert(error.error || 'Error converting quote to invoice'); } } catch (error) { console.error('Error:', error); alert('Error converting quote to invoice'); } } // Expose for onclick handlers window.quoteView = { loadQuotes, renderQuotes, viewPDF, edit, remove, convertToInvoice };