// ======================================== // CHECKOUT PAGE LOGIC // ======================================== let currentStepNum = 1; let paymentMethod = 'cash'; document.addEventListener('DOMContentLoaded', () => { if (getCart().length === 0) { window.location.href = 'cart.html'; return; } // Auth Check if (!isLoggedIn()) { showToast("Majburiy", "To'lovni amalga oshirish yoki buyurtma berish uchun iltimos tizimga kiring", "info"); setTimeout(() => { window.location.href = 'index.html'; // In a real app we might open modal directly, but index is safer to initialize auth state }, 2000); return; } // Pre-fill user data const user = getUser(); if (user.name) document.getElementById('userName').value = user.name; if (user.phone) document.getElementById('userPhone').value = user.phone; if (user.email) document.getElementById('userEmail').value = user.email; // Payment method selection document.querySelectorAll('[data-payment]').forEach(el => { el.addEventListener('click', () => { document.querySelectorAll('[data-payment]').forEach(e => e.classList.remove('active')); el.classList.add('active'); paymentMethod = el.dataset.payment; }); }); // Load store location info for step 2 loadStoreLocation(); }); function loadStoreLocation() { const settings = JSON.parse(localStorage.getItem('mtextile_settings')) || {}; const addressBox = document.getElementById('storeAddressText'); const mapBox = document.getElementById('storeMapContainer'); if (settings.address && addressBox) { addressBox.textContent = settings.address; } if (settings.mapIframe && mapBox) { mapBox.innerHTML = settings.mapIframe; mapBox.style.display = 'block'; const iframe = mapBox.querySelector('iframe'); if (iframe) { iframe.style.width = '100%'; iframe.style.height = '100%'; iframe.style.border = 'none'; } } } function goToStep(step) { document.querySelectorAll('.checkout-step').forEach(el => el.classList.remove('active')); document.getElementById('step' + step).classList.add('active'); document.querySelectorAll('.progress-step').forEach(el => { const s = parseInt(el.dataset.step); el.classList.remove('active', 'done'); if (s < step) el.classList.add('done'); if (s === step) el.classList.add('active'); }); currentStepNum = step; window.scrollTo({ top: 0, behavior: 'smooth' }); } function nextStep(from) { if (from === 1 && !validateStep1()) return; if (from === 2 && !validateStep2()) return; if (from === 3) buildSummary(); goToStep(from + 1); } function prevStep(from) { goToStep(from - 1); } function validateStep1() { let valid = true; const name = document.getElementById('userName').value.trim(); const phone = document.getElementById('userPhone').value.trim(); const email = document.getElementById('userEmail').value.trim(); clearErrors(); if (!name || name.length < 2) { showFieldError('userName', 'nameError', 'Ismingizni kiriting (kamida 2 harf)'); valid = false; } // Strict UZ phone validation const phoneRegex = /^(?:\+?998)[ \-]?\d{2}[ \-]?\d{3}[ \-]?\d{2}[ \-]?\d{2}$/; if (!phone || !phoneRegex.test(phone.replace(/\s/g, ''))) { showFieldError('userPhone', 'phoneError', 'Faqat O\'zbekiston raqami (+998...) kiritilishi shart'); valid = false; } if (email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { showFieldError('userEmail', 'emailError', 'To\'g\'ri email kiriting'); valid = false; } if (valid) saveUser({ name, phone, email }); return valid; } function validateStep2() { // Store pickup requires no address input from the user. return true; } function showFieldError(inputId, errorId, msg) { document.getElementById(inputId).classList.add('error'); document.getElementById(errorId).textContent = msg; } function clearErrors() { document.querySelectorAll('.form-input.error').forEach(el => el.classList.remove('error')); document.querySelectorAll('.form-error').forEach(el => el.textContent = ''); } function buildSummary() { const user = getUser(); const cart = getCart(); const totals = getCartTotal(); const payNames = { cash: '💵 Naqd pul', card: '💳 Plastik karta', click: '📱 Click/Payme' }; let itemsHTML = ''; cart.forEach(item => { const p = getProductById(item.productId); if (!p) return; itemsHTML += `