// 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 = `
${stateCode}

💰 ${offer.discount_type}

${offer.building_name}

${offer.location}

${offer.rooms_bathrooms}

${minRent} ${maxRent ? ` - ${maxRent}` : ''} / mo

${ savings ? `

Savings: ${savings}

` : '' } View Deal Report Scam
`; return card; } function renderOffers(list) { offersContainer.innerHTML = ''; if (!list || list.length === 0) { const empty = document.createElement('div'); empty.className = 'col-12 text-center listing-card'; empty.style.width = '100%'; empty.style.border = 'none'; empty.style.boxShadow = 'none'; empty.innerHTML = `

No 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.

`; }); // --- Filtro por cidade --- if (citySearchInput) { citySearchInput.addEventListener('input', e => { const term = e.target.value.trim().toLowerCase(); if (!term) { renderOffers(allOffers); return; } const filtered = allOffers.filter(offer => { const city = (offer.city || '').toLowerCase(); const location = (offer.location || '').toLowerCase(); return city.includes(term) || location.includes(term); }); renderOffers(filtered); }); } });