/** * WebWizard Frontend Logic */ const App = { state: { userId: localStorage.getItem('ww_user_id'), currentStep: 1, totalSteps: 8, project: null, categories: [], selection: { category: null, subcategory: null, customDescription: '' } }, async init() { console.log('Initializing WebWizard...'); if (!this.state.userId) { await this.initSession(); } await this.loadCategories(); this.bindEvents(); // If we don't have a project, try to load existing or create one if (!this.state.project) { await this.loadLastProject(); } this.showStep(this.state.currentStep); }, async loadLastProject() { try { const response = await this.apiCall('listProjects'); if (response.success && response.data.length > 0) { // Load the most recent project const lastProject = response.data[0]; const statusResponse = await this.apiCall('getProjectStatus', {}, lastProject.project_id); if (statusResponse.success) { this.state.project = statusResponse.data; this.state.currentStep = this.state.project.current_step || 1; console.log('Loaded existing project:', this.state.project.project_id); this.syncSelectionWithProject(); } } else { await this.createProject(); } } catch (error) { console.error('Failed to load projects:', error); await this.createProject(); } }, syncSelectionWithProject() { if (this.state.project && this.state.project.wizard_data) { const wd = this.state.project.wizard_data; // Step 1 if (wd.business_category) { const bc = wd.business_category; this.state.selection.category = bc.group; this.state.selection.subcategory = bc.subcategory; this.state.selection.customDescription = bc.custom_description || ''; if (this.state.selection.category) { this.selectCategory(this.state.selection.category); this.state.selection.subcategory = bc.subcategory; this.renderSubcategories(this.state.selection.category); } } // Step 2 if (wd.identity) { document.getElementById('business-name').value = wd.identity.business_name || ''; document.getElementById('business-tagline').value = wd.identity.tagline || ''; document.getElementById('business-description').value = wd.identity.description || ''; } if (wd.contact) { document.getElementById('contact-email').value = wd.contact.email || ''; document.getElementById('contact-phone').value = wd.contact.phone || ''; document.getElementById('contact-address').value = wd.contact.address || ''; document.getElementById('contact-city').value = wd.contact.city || ''; document.getElementById('contact-facebook').value = wd.contact.socials?.facebook || ''; document.getElementById('contact-instagram').value = wd.contact.socials?.instagram || ''; } } }, async initSession() { try { const response = await this.apiCall('initSession'); if (response.success) { this.state.userId = response.data.user_id; localStorage.setItem('ww_user_id', this.state.userId); console.log('Session initialized:', this.state.userId); } } catch (error) { console.error('Failed to initialize session:', error); alert('Chyba pri inicializácii session.'); } }, async createProject() { try { const response = await this.apiCall('createProject'); if (response.success) { this.state.project = response.data; console.log('Project created:', this.state.project.project_id); } } catch (error) { console.error('Failed to create project:', error); } }, async loadCategories() { try { const response = await this.apiCall('getCategories'); if (response.success) { this.state.categories = response.data.categories; this.renderCategories(); } } catch (error) { console.error('Failed to load categories:', error); } }, async apiCall(action, payload = {}, projectId = null) { const body = { action, project_id: projectId || (this.state.project ? this.state.project.project_id : null), payload }; const headers = { 'Content-Type': 'application/json' }; if (this.state.userId) { headers['X-User-ID'] = this.state.userId; } const response = await fetch('/ajax.php', { method: 'POST', headers, body: JSON.stringify(body) }); const result = await response.json(); if (!result.success) { throw new Error(result.error.message || 'API Error'); } return result; }, bindEvents() { document.getElementById('btn-next').addEventListener('click', () => this.nextStep()); document.getElementById('btn-prev').addEventListener('click', () => this.prevStep()); document.getElementById('custom-description').addEventListener('input', (e) => { this.state.selection.customDescription = e.target.value; }); // Step 2 validation listeners ['business-name', 'gdpr-consent', 'contact-email', 'contact-phone'].forEach(id => { const el = document.getElementById(id); if (el) { el.addEventListener('input', () => this.updateUI()); el.addEventListener('change', () => this.updateUI()); } }); }, renderCategories() { const container = document.getElementById('category-list'); container.innerHTML = ''; const icons = { gastro: '🍕', beauty: '✨', crafts: '🛠️', professional: '💼', other: '❓' }; this.state.categories.forEach(cat => { const card = document.createElement('div'); card.className = 'category-card'; if (this.state.selection.category === cat.id) card.classList.add('selected'); card.innerHTML = `