console.log("[SK] Script loading"); window.__callAPI = async function(f) { try { const r = await fetch('/api/contact', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(f) }); const json = await r.json(); console.log('[SK] API Response:', json); return json; } catch(e) { console.error('[SK] API Error:', e); return {success: false, message: e.message}; } }; console.log('[SK] __callAPI defined'); document.addEventListener('click', async function(e) { if (!e.target.textContent.includes('상담')) return; console.log('[SK] Button clicked!'); const form = document.querySelector('form'); if (!form) { console.log('[SK] No form'); return; } const allInputs = form.querySelectorAll('input'); console.log('[SK] All inputs:', allInputs.length); for (let i = 0; i < allInputs.length; i++) { console.log(`[SK] Input ${i}:`, allInputs[i].type, allInputs[i].placeholder, '=', allInputs[i].value); } const nonCheckboxInputs = Array.from(allInputs).filter(inp => inp.type !== 'checkbox'); console.log('[SK] Non-checkbox inputs:', nonCheckboxInputs.length); const name = nonCheckboxInputs[0]?.value?.trim() || ''; const phone = nonCheckboxInputs[1]?.value?.trim() || ''; console.log('[SK] Name:', name ? 'OK' : 'EMPTY', 'Phone:', phone ? 'OK' : 'EMPTY'); if (!name || !phone) { console.log('[SK] Missing fields'); return; } const checkbox = form.querySelector('input[type="checkbox"]'); if (!checkbox?.checked) { console.log('[SK] Not agreed'); return; } console.log('[SK] Calling API'); e.preventDefault(); e.stopPropagation(); const result = await window.__callAPI({ name: name, phone: phone, amount: form.querySelector('select')?.value || '', content: form.querySelectorAll('select')[1]?.value || '', message: form.querySelector('textarea')?.value || '', agree: true, timestamp: new Date().toISOString() }); if (result.success) { alert(result.message || '완료'); form.reset(); } }, true); // Block clicks outside form/buttons document.addEventListener('click', function(e) { const form = document.querySelector('form'); if (!form) return; const isInForm = e.target.closest('form'); const isBtnWithConsult = e.target.textContent.includes('상담'); if (!isInForm && !isBtnWithConsult) { e.preventDefault(); e.stopPropagation(); } }, true); console.log('[SK] Event listeners attached');