function postJoinEventRequest(eventId, extras) { extras = extras || {}; return new Promise(function(resolve, reject) { if (!eventId || eventId <= 0) { reject(new Error('Geçersiz etkinlik ID.')); return; } const params = new URLSearchParams(); params.set('event_id', String(eventId)); if (extras.katilim_yatirim_tutar != null && extras.katilim_yatirim_tutar !== '') { params.set('katilim_yatirim_tutar', String(extras.katilim_yatirim_tutar)); } if (extras.katilim_yatirim_tarihi != null && extras.katilim_yatirim_tarihi !== '') { params.set('katilim_yatirim_tarihi', String(extras.katilim_yatirim_tarihi)); } const xhr = new XMLHttpRequest(); xhr.open('POST', 'ajax/join_event.php', true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function() { if (xhr.readyState !== 4) return; if (xhr.status !== 200) { reject(new Error('Sunucu hatası.')); return; } try { resolve(JSON.parse(xhr.responseText)); } catch (e) { reject(e); } }; xhr.send(params.toString()); }); } function showJoinResponse(response) { if (typeof showAlert === 'undefined') { if (response.success) { alert(response.message); setTimeout(function() { location.reload(); }, 1200); } else { alert(response.message); if (response.redirect) { setTimeout(function() { window.location.href = response.redirect; }, 600); } } return; } if (response.success) { showAlert({ type: 'success', title: 'Başarılı', message: response.message, onConfirm: function() { location.reload(); } }); setTimeout(function() { location.reload(); }, 1600); } else if (response.redirect) { showAlert({ type: 'warning', title: 'Uyarı', message: response.message, messageClass: 'text-start', showCancel: false, onConfirm: function() { window.location.href = response.redirect; } }); } else { showAlert({ type: 'error', title: 'Hata', message: response.message || 'İşlem başarısız.' }); } } function openEventJoinExtraForm(eventId, needBoth) { let fields = ''; if (needBoth) { fields += '' + '

Hesap gereksinimleri tamam. Katılmak için aşağıdaki yatırım bilgilerini doldurun.

' + '
' + '
' + '
' + '' + '
' + '' + '' + '
' + '
' + '' + '' + '
' + '

Tutarı ondalıklı girebilirsiniz. Tarih için takvimden seçim yapın.

'; } const customContent = '' + '
' + '
' + '' + '
' + '

Yatırım bilgisi

' + '
' + '
' + fields + '
' + ''; const overlay = showAlert({ customContent: customContent, maxWidth: 'min(100%, 480px)', showCancel: false, confirmText: '' }); setTimeout(function() { const root = overlay.querySelector('.nirvana-alert-container'); if (!root) return; const closeBtn = root.querySelector('#evJoinAlertClose'); if (closeBtn) { closeBtn.addEventListener('click', function() { closeNirvanaAlert(closeBtn); }); } const cancelBtn = root.querySelector('#evJoinExtraCancel'); const okBtn = root.querySelector('#evJoinExtraOk'); if (cancelBtn) { cancelBtn.addEventListener('click', function() { closeNirvanaAlert(overlay); }); } if (okBtn) { okBtn.addEventListener('click', function() { const extras = {}; if (needBoth) { const v = root.querySelector('#evJoinYatTutar'); const d = root.querySelector('#evJoinYatTarih'); extras.katilim_yatirim_tutar = v ? String(v.value).trim() : ''; extras.katilim_yatirim_tarihi = d ? String(d.value).trim() : ''; } okBtn.disabled = true; postJoinEventRequest(eventId, extras) .then(function(res) { closeNirvanaAlert(overlay); showJoinResponse(res); }) .catch(function() { okBtn.disabled = false; if (typeof showAlert !== 'undefined') { showAlert({ type: 'error', title: 'Hata', message: 'Bağlantı kurulamadı.' }); } }); }); } }, 10); } function handleJoinEvent(eventId) { const meta = window.EVENT_JOIN_META || {}; const id = parseInt(eventId || meta.event_id, 10) || 0; if (!id) { if (typeof showAlert !== 'undefined') { showAlert({ type: 'error', title: 'Hata', message: 'Geçersiz etkinlik ID.' }); } else { alert('Geçersiz etkinlik ID.'); } return; } const needY = !!meta.need_yatirim_bilgisi; if (needY) { if (typeof showAlert === 'undefined') { alert('Bu etkinlik için yatırım bilgileri gerekli; sayfa uyarı bileşenini yükleyemedi.'); return; } openEventJoinExtraForm(id, true); return; } postJoinEventRequest(id, {}) .then(function(res) { showJoinResponse(res); }) .catch(function() { if (typeof showAlert !== 'undefined') { showAlert({ type: 'error', title: 'Hata', message: 'Katılım işlemi sırasında bir hata oluştu.' }); } else { alert('Katılım işlemi sırasında bir hata oluştu.'); } }); } function initEventJoin() { const joinBtn = document.getElementById('joinEventBtn'); const scrollToJoinBtn = document.getElementById('scroll-to-join'); if (joinBtn && !joinBtn.hasAttribute('data-listener-added')) { joinBtn.setAttribute('data-listener-added', 'true'); joinBtn.addEventListener('click', function(e) { e.preventDefault(); const evId = this.getAttribute('data-event-id'); handleJoinEvent(evId); }); } if (scrollToJoinBtn && !scrollToJoinBtn.hasAttribute('data-listener-added')) { scrollToJoinBtn.setAttribute('data-listener-added', 'true'); scrollToJoinBtn.addEventListener('click', function(e) { e.preventDefault(); const evId = this.getAttribute('data-event-id'); if (evId) { handleJoinEvent(evId); } else { const contentSection = document.querySelector('.cekilis-content-wrapper'); if (contentSection) { contentSection.scrollIntoView({ behavior: 'smooth', block: 'start' }); } } }); } } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initEventJoin); } else { initEventJoin(); }