const AMENITY_ICONS = { has_ac: { icon: 'fa-snowflake', label: 'Кондиционер' }, has_tv: { icon: 'fa-tv', label: 'ТВ' }, has_fridge: { icon: 'fa-sink', label: 'Холодильник' }, has_wifi: { icon: 'fa-wifi', label: 'WiFi' }, has_kettle: { icon: 'fa-mug-hot', label: 'Чайник' }, has_hairdryer: { icon: 'fa-wind', label: 'Фен' }, has_shower: { icon: 'fa-shower', label: 'Душ в номере' }, has_sea_view: { icon: 'fa-water', label: 'Вид на море' } }; let cachedRooms = []; async function loadRoomsPublic() { try { const res = await fetch('/api/rooms'); if (!res.ok) throw new Error('Failed to load rooms'); cachedRooms = await res.json(); renderRoomsPublic(cachedRooms); updateRoomPrices(cachedRooms); } catch (err) { console.error('Error loading rooms:', err); document.getElementById('roomsEmpty').style.display = 'block'; } } function renderRoomsPublic(rooms) { const grid = document.getElementById('roomsGrid'); const empty = document.getElementById('roomsEmpty'); if (!rooms || rooms.length === 0) { grid.innerHTML = ''; empty.style.display = 'block'; return; } empty.style.display = 'none'; grid.innerHTML = rooms.map((room, index) => { const amenities = Array.isArray(room.amenities) ? room.amenities : []; const floors = Array.isArray(room.floors) ? room.floors : []; const imageSrc = room.image_path || 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 250"%3E%3Crect fill="%23274151" width="400" height="250"/%3E%3Ctext fill="%2364748b" font-family="sans-serif" font-size="16" x="50%25" y="50%25" text-anchor="middle" dy=".3em"%3EФото%3C/text%3E%3C/svg%3E'; const fullImageSrc = imageSrc.startsWith('uploads') ? '/' + imageSrc : imageSrc; const amenitiesHtml = amenities.map(a => { const info = AMENITY_ICONS[a]; if (!info) return ''; return ` ${info.label}`; }).join(''); const floorsStr = floors.length > 0 ? 'Этажи ' + floors.join(', ') : ''; const extraBedsHtml = room.extra_beds > 0 ? `
Возможно +${room.extra_beds} доп. мест (${room.extra_bed_price} ₽)
` : ''; const featuredClass = room.type === 'Семейный' ? ' featured' : ''; return `
${room.name}
${room.type}

${escapeHtml(room.name)}

${escapeHtml(room.description || '')}

${room.area_sqm || 20} м² ${floorsStr ? ` ${floorsStr}` : ''} ${amenities.includes('has_shower') ? ' Душ в номере' : ''}
${amenitiesHtml}
${extraBedsHtml}
`; }).join(''); initRoomBookingHandlers(); } function updateRoomPrices(rooms) { const prices = {}; const maxGuests = {}; rooms.forEach(room => { prices[room.type] = room.price_per_night; maxGuests[room.type] = room.max_guests; }); if (window.updateRoomPricesData) { window.updateRoomPricesData(prices, maxGuests); } } function initRoomBookingHandlers() { document.querySelectorAll('.btn-book').forEach(btn => { btn.addEventListener('click', function() { const roomId = this.getAttribute('data-room-id'); const roomType = this.getAttribute('data-room-type'); const roomName = this.getAttribute('data-room-name'); const maxGuests = parseInt(this.getAttribute('data-max-guests')) || 4; document.getElementById('selectedRoom').value = roomType; document.getElementById('selectedRoomId').value = roomId; updateGuestOptionsDynamic(roomType, maxGuests); hidePriceInfo(); }); }); } function updateGuestOptionsDynamic(roomType, maxGuests) { const guestsSelect = document.querySelector('[name="guests"]'); if (!guestsSelect) return; let options = []; for (let i = 1; i <= maxGuests; i++) { let text = i === 1 ? '1 гость' : (i < 5 ? `${i} гостя` : `${i} гостей`); options.push(``); } guestsSelect.innerHTML = options.join(''); } function escapeHtml(text) { if (!text) return ''; const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } document.addEventListener('DOMContentLoaded', () => { loadRoomsPublic(); });