बच्चे के भविष्य की चिंता खत्म: सीए से मामूली बचत को 1.1 करोड़ में बदलने का गुप्त तरीका
}
function initConversionTracking() {
document.querySelectorAll('.track-conv').forEach((el) => {
el.addEventListener('click', () => {
const evt = el.getAttribute('data-track-event');
if (evt) trackEvent(evt);
});
});
}
function initNewsletterForm() {
const form = document.getElementById('newsletterForm');
const emailInput = document.getElementById('newsletterEmail');
const msg = document.getElementById('newsletterMsg');
if (!form || !emailInput || !msg) return;
form.addEventListener('submit', async (e) => {
e.preventDefault();
const email = (emailInput.value || '').trim();
if (!email) {
msg.textContent = 'Please enter a valid email.';
msg.className = 'text-[11px] font-semibold text-red-600 min-h-[16px]';
return;
}
msg.textContent = 'Subscribing...';
msg.className = 'text-[11px] font-semibold text-gray-500 min-h-[16px]';
try {
const res = await fetch('/api/v1/newsletter/subscribe.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email,
source: 'article_sidebar',
name: CURRENT_USER_NAME !== 'Guest' ? CURRENT_USER_NAME : ''
})
});
const json = await res.json();
if (json && json.success) {
msg.textContent = 'Subscribed. Weekly macro brief is on the way.';
msg.className = 'text-[11px] font-semibold text-emerald-700 min-h-[16px]';
emailInput.value = '';
trackEvent('newsletter_subscribe_success', { source: 'article_sidebar' });
} else {
throw new Error(json?.message || 'Subscription failed');
}
} catch (err) {
msg.textContent = 'Could not subscribe right now. Please try again.';
msg.className = 'text-[11px] font-semibold text-red-600 min-h-[16px]';
trackEvent('newsletter_subscribe_fail', { source: 'article_sidebar' });
}
});
}
function initMeteredPaywall() {
if (IS_LOGGED_IN) return;
const gate = document.getElementById('meteredPaywall');
if (!gate) return;
const key = 'wt_meter_reads_v1';
let reads = [];
try { reads = JSON.parse(localStorage.getItem(key) || '[]'); } catch (e) { reads = []; }
reads = Array.isArray(reads) ? reads : [];
if (!reads.includes(String(ARTICLE_ID))) {
reads.push(String(ARTICLE_ID));
}
// Keep only recent 20 IDs
reads = reads.slice(-20);
localStorage.setItem(key, JSON.stringify(reads));
const freeReads = 4;
if (reads.length > freeReads) {
gate.classList.remove('hidden');
document.body.classList.add('overflow-hidden');
trackEvent('metered_paywall_shown', { reads: reads.length });
}
const continueBtn = document.getElementById('continueFreeBtn');
if (continueBtn) {
continueBtn.addEventListener('click', () => {
gate.classList.add('hidden');
document.body.classList.remove('overflow-hidden');
trackEvent('metered_paywall_continue_free', { reads: reads.length });
});
}
}
async function loadSidebarIntel() {
try {
const res = await fetch(`/api/v1/posts/index.php?limit=4&type=Financial Intelligence`);
const json = await res.json();
const posts = json.data?.posts || [];
const container = document.getElementById('sidebar-intel-feed');
if(posts.length > 0) {
container.innerHTML = posts.map(p => `
${p.title}
'); } } catch(e) { console.error('Sidebar load failed:', e); } } async function loadInteractions() { try { const res = await fetch(`/api/v1/posts/show.php?id=${ARTICLE_ID}&device_id=${DEVICE_ID}`); const json = await res.json(); if (json.success && json.data?.post) { const post = json.data.post; updateLikeButton(post.is_liked); renderComments(post.comments || []); } } catch (e) { console.error('Interactions load failed:', e); } } // Like Logic const likeBtnFloating = document.getElementById('likeBtnFloating'); const likeBtnMobile = document.getElementById('likeBtnMobile'); function attachLikeListener(btn) { if(!btn) return; btn.addEventListener('click', async () => { // Likes are allowed anonymously based on DEVICE_ID const isLiked = btn.classList.contains('active-like'); updateLikeButton(!isLiked, true); try { await fetch('/api/v1/posts/like.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ news_id: ARTICLE_ID, device_id: DEVICE_ID }) }); } catch (e) { updateLikeButton(isLiked); } }); } attachLikeListener(likeBtnFloating); attachLikeListener(likeBtnMobile); function updateLikeButton(liked, animate = false) { [likeBtnFloating, likeBtnMobile].forEach(btn => { if(!btn) return; const outlineIcon = btn.querySelector('.outline-icon') || btn.querySelector('.bi-heart'); const filledIcon = btn.querySelector('.filled-icon') || btn.querySelector('.bi-heart-fill'); const text = btn.querySelector('span'); if (liked) { btn.classList.add('active-like'); if (outlineIcon) outlineIcon.classList.add('hidden'); if (filledIcon) { filledIcon.classList.remove('hidden'); filledIcon.classList.add('block', 'text-red-500'); if(animate) { filledIcon.classList.add('heart-pop'); setTimeout(() => filledIcon.classList.remove('heart-pop'), 500); } } if(text) text.innerText = 'Liked'; } else { btn.classList.remove('active-like'); if (filledIcon) { filledIcon.classList.add('hidden'); filledIcon.classList.remove('block', 'text-red-500'); } if (outlineIcon) outlineIcon.classList.remove('hidden'); if(text) text.innerText = 'Like'; } }); } // Comment Logic window.postComment = async () => { if (!IS_LOGGED_IN) { openLoginModal(); return; } const input = document.getElementById('commentInput'); const content = input.value.trim(); if (!content) return; const list = document.getElementById('commentsContainer'); if(list.innerText.includes('No comments yet')) list.innerHTML = ''; const tempDiv = document.createElement('div'); const initial = CURRENT_USER_NAME.charAt(0).toUpperCase(); tempDiv.className = "flex gap-4 opacity-70 animate-pulse transition-all"; tempDiv.innerHTML = `
${content}
`; list.prepend(tempDiv); input.value = ''; try { await fetch('/api/v1/posts/comment.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ news_id: ARTICLE_ID, content: content, device_id: DEVICE_ID }) }); tempDiv.classList.remove('opacity-70', 'animate-pulse'); const postText = tempDiv.querySelector('.posting-text'); if (postText) { postText.innerText = 'Just now'; postText.classList.replace('text-blue-500', 'text-gray-500'); } } catch (e) { alert('Failed to comment. Please try again.'); tempDiv.remove(); } }; function renderComments(comments) { const list = document.getElementById('commentsContainer'); if (!comments || comments.length === 0) { list.innerHTML = '
No comments yet. Be the first!
'; return; } list.innerHTML = comments.map(c => `
${c.content}
`).join(''); } function shareArticle(platform) { const url = encodeURIComponent(window.location.href); const title = encodeURIComponent(document.title); switch(platform) { case 'facebook': window.open(`https://www.facebook.com/sharer/sharer.php?u=${url}`, '_blank'); break; case 'twitter': window.open(`https://twitter.com/intent/tweet?text=${title}&url=${url}`, '_blank'); break; case 'linkedin': window.open(`https://www.linkedin.com/sharing/share-offsite/?url=${url}`, '_blank'); break; case 'whatsapp': window.open(`https://api.whatsapp.com/send?text=${title} ${url}`, '_blank'); break; case 'instagram': navigator.clipboard.writeText(window.location.href); if (window.showToast) window.showToast('Link copied for Instagram!', 'success'); else alert('Link copied for Instagram!'); break; case 'copy': navigator.clipboard.writeText(window.location.href); if (window.showToast) window.showToast('Link copied!', 'info'); else alert('Link copied!'); break; default: if (navigator.share) navigator.share({ title: document.title, url: window.location.href }); else { navigator.clipboard.writeText(window.location.href); alert('Link copied!'); } } } // Professional Login Modal window.openLoginModal = function() { if(document.getElementById('authModal')) return; const modal = document.createElement('div'); modal.id = 'authModal'; modal.className = 'fixed inset-0 z-[9999] flex items-center justify-center p-4 opacity-0 transition-opacity duration-300'; modal.innerHTML = `
`; document.body.appendChild(modal); requestAnimationFrame(() => { modal.classList.remove('opacity-0'); document.getElementById('authModalContent').classList.remove('scale-95'); }); }; window.handleAjaxLogin = async function(e) { e.preventDefault(); const email = document.getElementById('ajaxLoginEmail').value; const password = document.getElementById('ajaxLoginPassword').value; const btn = document.getElementById('ajaxLoginSubmitBtn'); const err = document.getElementById('ajaxLoginError'); btn.innerHTML = ' Authenticating...'; btn.disabled = true; err.classList.add('hidden'); try { const formData = new FormData(); formData.append('action', 'login'); formData.append('email', email); formData.append('password', password); const response = await fetch('/login.php', { method: 'POST', body: formData }); if (response.url && (response.url.includes('dashboard.php') || response.url.includes('owner-dashboard.php'))) { btn.innerHTML = ' Success! Reloading...'; window.location.reload(); return; } const html = await response.text(); if (html.includes('Invalid email or password')) { throw new Error('Invalid email or password.'); } if (html.includes('System Error')) { throw new Error('System error occurred.'); } btn.innerHTML = ' Success! Reloading...'; window.location.reload(); } catch(error) { err.innerText = error.message || 'Login failed.'; err.classList.remove('hidden'); btn.innerHTML = 'Secure Login'; btn.disabled = false; } }; window.closeLoginModal = function() { const modal = document.getElementById('authModal'); if(!modal) return; modal.classList.add('opacity-0'); document.getElementById('authModalContent').classList.add('scale-95'); setTimeout(() => modal.remove(), 300); };