// app/static/js/pricing.js document.addEventListener('DOMContentLoaded', function() { const form = document.getElementById('subscription-form'); const slider = document.getElementById('duration-slider'); const durationDisplay = document.getElementById('duration-display'); const priceDisplay = document.getElementById('price-display'); const presetButtons = document.querySelectorAll('.duration-preset'); const emailInput = document.getElementById('email'); function formatDuration(hours) { if (hours < 24) return `${hours} hour${hours === 1 ? '' : 's'}`; if (hours < 168) return `${hours / 24} day${hours === 24 ? '' : 's'}`; if (hours < 720) return `${Math.floor(hours / 168)} week${hours === 168 ? '' : 's'}`; return `${Math.floor(hours / 720)} month${hours === 720 ? '' : 's'}`; } async function updatePrice(hours) { try { const response = await fetch('/api/calculate-price', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ hours: parseInt(hours) }) }); const data = await response.json(); priceDisplay.textContent = data.price; durationDisplay.textContent = formatDuration(hours); } catch (error) { console.error('Error calculating price:', error); } } async function createInvoice(duration, email) { try { const response = await fetch('/create-invoice', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ duration: parseInt(duration), email: email }) }); if (!response.ok) { throw new Error('Failed to create invoice'); } const data = await response.json(); window.location.href = data.checkout_url; } catch (error) { console.error('Error creating invoice:', error); alert('Failed to create payment invoice. Please try again.'); } } // Event listeners slider.addEventListener('input', () => updatePrice(slider.value)); presetButtons.forEach(button => { button.addEventListener('click', (e) => { const hours = e.target.dataset.hours; slider.value = hours; updatePrice(hours); }); }); form.addEventListener('submit', async (e) => { e.preventDefault(); const email = emailInput.value.trim(); if (!email) { alert('Please enter your email address'); return; } const duration = slider.value; await createInvoice(duration, email); }); // Initial price calculation updatePrice(slider.value); });