// 예시 텍스트 설정 function setExample(element) { document.getElementById('searchInput').value = element.textContent; } // 쿼리 파싱 및 분석 async function parseQuery() { const input = document.getElementById('searchInput').value.trim(); if (!input) { alert('검색할 내용을 입력해주세요!'); return; } // UI 상태 변경 document.getElementById('loadingSection').style.display = 'block'; document.getElementById('resultSection').style.display = 'none'; document.getElementById('searchBtn').disabled = true; try { // /api/search 엔드포인트 호출 (파싱 + 실거래가 조회) const response = await fetch('/api/search', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ text: input }) }); if (!response.ok) { throw new Error('서버 오류가 발생했습니다.'); } const data = await response.json(); displayResults(data); } catch (error) { alert('오류: ' + error.message); console.error('Error:', error); } finally { document.getElementById('loadingSection').style.display = 'none'; document.getElementById('searchBtn').disabled = false; } } // 결과 표시 function displayResults(data) { const resultContent = document.getElementById('resultContent'); let html = ''; // 파싱된 정보 요약 if (data.parsed) { html += '
'; html += '

🔍 검색 조건

'; html += '
'; if (data.parsed.property_type) { html += `${data.parsed.property_type}`; } if (data.parsed.transaction_type) { html += `${data.parsed.transaction_type}`; } if (data.parsed.location) { html += `${data.parsed.location}`; } if (data.parsed.region_name) { html += `${data.parsed.region_name}`; } html += '
'; html += '
'; } // 실거래가 목록 if (data.listings && data.listings.length > 0) { html += '
'; html += `

📊 실거래가 정보 (${data.count}건)

`; html += '
'; // 최대 20개만 표시 const displayCount = Math.min(data.listings.length, 20); for (let i = 0; i < displayCount; i++) { const item = data.listings[i]; html += createListingCard(item); } if (data.listings.length > 20) { html += `

... 외 ${data.listings.length - 20}건

`; } html += '
'; html += '
'; } else { html += '
'; html += '

😔 검색 결과가 없습니다.

'; if (!data.parsed.region_code) { html += '

지역 정보를 더 구체적으로 입력해주세요.

'; } else { html += '

다른 조건으로 검색해보세요.

'; } html += '
'; } resultContent.innerHTML = html; document.getElementById('resultSection').style.display = 'block'; } // 실거래가 카드 생성 function createListingCard(item) { let html = '
'; // 제목 (아파트명 또는 주소) const title = item['아파트'] || item['법정동'] || '정보 없음'; html += `

${title}

`; // 거래 정보 html += '
'; // 거래금액 if (item['거래금액']) { const price = item['거래금액'].replace(/,/g, '').trim(); const priceNum = parseInt(price); const priceText = priceNum >= 10000 ? `${(priceNum / 10000).toFixed(1)}억` : `${priceNum.toLocaleString()}만원`; html += `
거래금액 ${priceText}
`; } // 보증금 (전세/월세) if (item['보증금액']) { const deposit = item['보증금액'].replace(/,/g, '').trim(); const depositNum = parseInt(deposit); const depositText = depositNum >= 10000 ? `${(depositNum / 10000).toFixed(1)}억` : `${depositNum.toLocaleString()}만원`; html += `
보증금 ${depositText}
`; } // 월세 if (item['월세금액'] && item['월세금액'] !== '0') { html += `
월세 ${item['월세금액']}만원
`; } // 면적 if (item['전용면적']) { const area = parseFloat(item['전용면적']); const pyeong = (area / 3.3).toFixed(1); html += `
면적 ${area}㎡ (${pyeong}평)
`; } // 층 if (item['층']) { html += `
${item['층']}층
`; } // 거래일 if (item['년'] && item['월'] && item['일']) { html += `
거래일 ${item['년']}.${item['월']}.${item['일']}
`; } // 건축년도 if (item['건축년도']) { const age = new Date().getFullYear() - parseInt(item['건축년도']); html += `
건축년도 ${item['건축년도']}년 (${age}년)
`; } // 주소 if (item['법정동'] && item['지번']) { html += `
주소 ${item['법정동']} ${item['지번']}
`; } html += '
'; html += '
'; return html; } // Enter 키로 검색 document.addEventListener('DOMContentLoaded', function() { document.getElementById('searchInput').addEventListener('keypress', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); parseQuery(); } }); });