const textarea = document.querySelector('.textarea-table');
function requestTable() {
const cart = JSON.parse(localStorage.getItem('cart')) || {};
const ids = Object.keys(cart);
let tableItem = '';
let totalSumma = 0;
let tableTotalPrice = '';
const renderTable = () => {
for (let i in ids) {
const keys = ids[i];
const articul = cart[keys].articul;
const name = cart[keys].name;
const size = cart[keys].size;
const color = cart[keys].color
const quantity = cart[keys].quantity;
const totalprice = cart[keys].totalprice;
tableItem += `
<tr>
<td>${name}</td>
<td>${articul}</td>
<td>${size}</td>
<td style="background-color: ${color}"></td>
<td>${quantity}</td>
<td>${totalprice} тг</td>
</tr>
`
totalSumma += totalprice;
tableTotalPrice = `
<tr>
<td colspan="6">Итоговая сумма всех товаров - ${totalSumma.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ")} тг</td>
</tr>`;
}
textarea.innerHTML = `
<table border="1" cellspacing="0" cellpadding="10">
<thead>
<tr>
<th>Название</th>
<th>Артикул</th>
<th>Размер</th>
<th>Цвет</th>
<th>Количество</th>
<th>Итоговая сумма</th>
</tr>
</thead>
<tbody>${tableItem} ${tableTotalPrice}</tbody>
</table>
`;
}
renderTable();
}
if(textarea) {
requestTable();
}