import wixData from 'wix-data'; import wixLocation from 'wix-location'; import { sendLeadFollowUpEmail } from 'backend/email'; const answers = [0, 0, 0, 0, 0, 0, 0]; function getScore() { return answers.reduce((sum, value) => sum + value, 0); } function getCategory(score) { if (score >= 20) return 'high'; if (score >= 11) return 'mid'; if (score > 0) return 'low'; return 'unscored'; } function getTierContent(score) { const category = getCategory(score); if (category === 'high') { return { title: 'You need structure now.', message: 'Your score says the issue is not information. It is execution. Earned is built for you, and coaching is the next move if you are done wasting time.', primaryLabel: 'Buy Earned Now', secondaryLabel: 'Apply for Coaching', primaryLink: 'https://a.co/d/0hpQyJ2q', secondaryLink: '/coaching', redirectLink: '/coaching' }; } if (category === 'mid') { return { title: 'You are close, but inconsistent.', message: 'You know enough to do better, but your habits are still beating your goals. Earned will sharpen your standard and help you stop starting over.', primaryLabel: 'Get the Book', secondaryLabel: 'See BernardFit', primaryLink: 'https://a.co/d/0hpQyJ2q', secondaryLink: '/', redirectLink: '/earned/mid' }; } if (category === 'low') { return { title: 'You are not all the way serious yet.', message: 'That can change fast. Start with Earned. Let the book challenge the way you think before you ask for bigger results.', primaryLabel: 'Start With Earned', secondaryLabel: 'Visit BernardFit', primaryLink: 'https://a.co/d/0hpQyJ2q', secondaryLink: '/', redirectLink: '/earned/low' }; } return { title: 'Score yourself honestly.', message: 'Answer the questions. Your score updates live and shows you where you really stand.', primaryLabel: 'Buy Earned', secondaryLabel: 'Visit BernardFit', primaryLink: 'https://a.co/d/0hpQyJ2q', secondaryLink: '/', redirectLink: '/earned' }; } function updateVisualState() { const score = getScore(); const category = getCategory(score); const tier = getTierContent(score); $w('#scoreText').text = String(score); $w('#categoryText').text = category.toUpperCase(); $w('#resultTitle').text = tier.title; $w('#resultMessage').text = tier.message; $w('#primaryCtaButton').label = tier.primaryLabel; $w('#secondaryCtaButton').label = tier.secondaryLabel; $w('#primaryCtaButton').link = tier.primaryLink; $w('#secondaryCtaButton').link = tier.secondaryLink; } function selectAnswer(questionIndex, value) { answers[questionIndex] = value; updateVisualState(); } function wireQuestion(questionNumber, questionIndex) { for (let value = 1; value <= 5; value += 1) { $w(`#q${questionNumber}_${value}`).onClick(() => { selectAnswer(questionIndex, value); }); } } $w.onReady(function () { wireQuestion(1, 0); wireQuestion(2, 1); wireQuestion(3, 2); wireQuestion(4, 3); wireQuestion(5, 4); wireQuestion(6, 5); wireQuestion(7, 6); updateVisualState(); $w('#submitButton').onClick(async () => { const firstName = $w('#firstNameInput').value?.trim(); const email = $w('#emailInput').value?.trim().toLowerCase(); const score = getScore(); const category = getCategory(score); const tier = getTierContent(score); if (!firstName || !email) { $w('#resultMessage').text = 'Enter your first name and email before submitting.'; return; } if (category === 'unscored') { $w('#resultMessage').text = 'Answer the questions before submitting. Human honesty is apparently still required.'; return; } $w('#submitButton').disable(); $w('#submitButton').label = 'Submitting...'; try { await wixData.insert('EarnedLeads', { firstName, email, score, category, answers: JSON.stringify(answers), source: 'earned-page', submittedAt: new Date() }); await sendLeadFollowUpEmail({ firstName, email, score, category }); wixLocation.to(tier.redirectLink); } catch (error) { console.error('Lead submission failed:', error); $w('#resultMessage').text = 'Something broke during signup. Check your collection permissions and backend email settings.'; $w('#submitButton').enable(); $w('#submitButton').label = 'Join + Get Routed'; } }); });