// ============ CARREGAR CARRINHO DIRETO DO LOCALSTORAGE ============
function carregarCarrinhoPage() {
console.log('Carregando carrinho (versao simplificada)...');
var carrinho = JSON.parse(localStorage.getItem('carrinho')) || [];
var container = document.getElementById('carrinhoContainer');
if (!container) {
console.error('Container do carrinho nao encontrado!');
return;
}
if (carrinho.length === 0) {
container.innerHTML =
'
' +
'
' +
'

' +
'
' +
'
' +
'
' + item.nome + '
' +
'
' + preco.toLocaleString('pt-BR') + ' MZN
' +
nomeCor +
'
' +
'
' +
'' +
'' + quantidade + '' +
'' +
'
' +
'
' + itemSubtotal.toLocaleString('pt-BR') + ' MZN
' +
'
' +
'
';
});
// Buscar cidade do localStorage
var cidadeEstimada = localStorage.getItem('cidade_entrega') || 'Maputo/Matola';
var custoEstimado = parseInt(localStorage.getItem('custo_entrega')) || 0;
// Calcular frete
var multiplicador = 1;
if (totalItens <= 1) multiplicador = 1;
else if (totalItens <= 3) multiplicador = 1;
else if (totalItens <= 6) multiplicador = 2;
else if (totalItens <= 10) multiplicador = 3;
else multiplicador = 3;
var totalFrete = multiplicador * custoEstimado;
var html =
'' +
'
' +
'Subtotal (' + totalItens + ' item' + (totalItens > 1 ? 's' : '') + ')' +
'' + subtotal.toLocaleString('pt-BR') + ' MZN' +
'
' +
'
' +
'Estimativa de frete' +
'' +
cidadeEstimada + (custoEstimado > 0 ? ' (+' + custoEstimado + ' MZN/item)' : ' (Grátis)') +
'' +
'
';
if (totalFrete > 0) {
html +=
'
' +
'Grupo: ' + multiplicador + 'x ' + custoEstimado + ' MZN' +
'' + totalFrete.toLocaleString('pt-BR') + ' MZN' +
'
';
} else {
html +=
'
' +
'Entrega Grátis' +
'✨ 0 MZN' +
'
';
}
html +=
'
' +
'Total estimado' +
'' + (subtotal + totalFrete).toLocaleString('pt-BR') + ' MZN' +
'
' +
'
' +
'O valor do frete sera confirmado no checkout' +
'
' +
'
Finalizar Compra' +
'
';
container.innerHTML = html;
console.log('Carrinho renderizado com sucesso!');
// Eventos dos botoes
document.querySelectorAll('.btn-aumentar').forEach(function(btn) {
btn.addEventListener('click', function() {
var id = parseInt(this.dataset.id);
alterarQuantidadeCarrinho(id, 1);
});
});
document.querySelectorAll('.btn-diminuir').forEach(function(btn) {
btn.addEventListener('click', function() {
var id = parseInt(this.dataset.id);
alterarQuantidadeCarrinho(id, -1);
});
});
document.querySelectorAll('.btn-remover').forEach(function(btn) {
btn.addEventListener('click', function() {
var id = parseInt(this.dataset.id);
removerDoCarrinho(id);
});
});
}
// ============ EXPORTAR ============
window.carregarCarrinhoPage = carregarCarrinhoPage;
// Executar quando a pagina carregar
if (document.readyState === 'complete' || document.readyState === 'interactive') {
carregarCarrinhoPage();
} else {
document.addEventListener('DOMContentLoaded', carregarCarrinhoPage);
}