// main.js – Pepe Deals // Carrega offers.json, renderiza os cards em carrossel horizontal // e deixa pronto para o filtro por cidade. document.addEventListener('DOMContentLoaded', () => { const offersContainer = document.getElementById('offersContainer'); const citySearchInput = document.getElementById('citySearch'); let allOffers = []; // --- Utilidades --- // Pega o código do estado (FL, IL, etc.) a partir de "location" function getStateCode(location) { if (!location) return ''; const parts = location.split(','); const lastPart = parts[parts.length - 1].trim(); const match = lastPart.match(/\b([A-Z]{2})\b/); return match ? match[1] : ''; } function formatCurrency(value) { if (typeof value !== 'number') return ''; return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2 }).format(value); } function createCard(offer) { const stateCode = getStateCode(offer.location); const minRent = formatCurrency(offer.monthly_rent_min); const maxRent = offer.monthly_rent_max ? formatCurrency(offer.monthly_rent_max) : null; const savings = offer.monthly_savings && offer.monthly_savings > 0 ? formatCurrency(offer.monthly_savings) : null; const card = document.createElement('div'); card.className = 'listing-card'; card.innerHTML = `
💰 ${offer.discount_type}
${offer.location}
${offer.rooms_bathrooms}
${minRent} ${maxRent ? ` - ${maxRent}` : ''} / mo
${ savings ? `Savings: ${savings}
` : '' } View Deal Report ScamNo deals found for this search.
`; offersContainer.appendChild(empty); return; } list.forEach(offer => { const card = createCard(offer); offersContainer.appendChild(card); }); } // --- Carrega JSON --- fetch('offers.json') .then(response => { if (!response.ok) { throw new Error('Failed to load offers.json'); } return response.json(); }) .then(data => { // filtra só ativos allOffers = (data || []).filter(o => o.is_active !== false); renderOffers(allOffers); }) .catch(error => { console.error(error); offersContainer.innerHTML = `Failed to load offers. Please check the offers.json file.