import { fetchUserSales, historyProducts } from "../service/user.js"; const table = document.getElementById('historyTable'); const rowTemplate = document.getElementById('historyRowTemplate'); const cartHistoryTotal = document.getElementById('cartHistoryTotal'); function setHistoryTable(tableData) { if (!tableData || tableData.length === 0) { console.warn("No hay datos para mostrar en el historial de compras."); table.querySelector('tbody').innerHTML = 'No hay historial de compras.'; return; } table.querySelector('tbody').innerHTML = ''; // Clear existing rows tableData.forEach(item => { addHistoryRow(item); }); } function addHistoryRow(sale) { const newRow = rowTemplate.content.cloneNode(true); if (table.querySelector(".no-data")) { table.querySelector(".no-data").remove(); // Remove no-data row if it exists } newRow.querySelector('.list-element-quantity').textContent = sale.quantity; newRow.querySelector('.list-element-name').textContent = sale.productName; newRow.querySelector('.list-element-price').textContent = `$${sale.price * sale.quantity}`; addCartHistoryTotal(sale.price * sale.quantity); table.querySelector('tbody').appendChild(newRow); } function addCartHistoryTotal(price) { const currentTotal = getHistoryTotal(); const newTotal = currentTotal + Number(price); updateCartHistoryTotal(newTotal); } function updateCartHistoryTotal(total) { cartHistoryTotal.textContent = `$${total.toFixed(0)}`; } function getHistoryTotal() { return Number(cartHistoryTotal.textContent.replace(/[$,]/g, '')); } async function setupShoppingCart(userID, userToken, userName) { const name = document.querySelector('#usernameTable'); if (name) { name.textContent = `Pedidos de: ${userName}`; }else{ console.warn("No se encontrĂ³ el elemento de nombre en la tabla de historial."); } const sales = await historyProducts(userID, userToken); if (sales){ setHistoryTable(sales); } } export { setupShoppingCart, setHistoryTable, addHistoryRow, updateCartHistoryTotal, getHistoryTotal };